17.3.1 像素处理

发布于:2024-02-22 ⋅ 阅读:(42) ⋅ 点赞:(0)

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。

17.3.1 像素处理

C#处理图像,主要使用到Bitmap 类的 GetPixel方法和SetPixel方法。

  1. Bitmap.GetPixel 方法:获取Bitmap中指定像素的颜色。

public Color GetPixel( int x, int y )

参数x:要检索的像素的 x 坐标。

参数y:要检索的像素的 y 坐标。

返回值:Color 结构,它表示指定像素的颜色。

  1. Bitmap.SetPixel 方法:设置Bitmap中指定像素的颜色。

public void SetPixel( int x, int y, Color color )

参数x:要设置的像素的 x 坐标。

参数y:要设置的像素的 y 坐标。

参数color:Color 结构,它表示要分配给指定像素的颜色。

GetPixel方法和SetPixel方法相互对应。通常是对源图像使用GetPixel方法获得颜色,对颜色的R(红)、G(绿)、B(蓝)分量进行处理,使用颜色的FromArgb方法将新的R、G、B分量合成新的颜色,最后使用SetPixel方法将新的颜色应用到目标图像。

【项目:code17-031】图像处理。

从17.3.1.1节至17.3.1.6节介绍处理图像的多种方法,涉及的代码均在【项目:code17-031】中。为确保代码顺利运行,请先加入以下代码:

        Bitmap sourceImg;
        private void Form1_Load(object sender, EventArgs e)
        {
            picSource.SizeMode = PictureBoxSizeMode.StretchImage;
            picDest.SizeMode = PictureBoxSizeMode.StretchImage;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = "图片文件|*.jpg;*.png";
            if (ofd.ShowDialog() != DialogResult.OK)
                return;
            sourceImg = (Bitmap)Image.FromFile(ofd.FileName);
            picSource.Image = sourceImg;
        }

学习更多vb.net知识,请参看vb.net 教程 目录

学习更多C#知识,请参看vb.net 教程 目录