在绘制图像灰度时我们要考虑到在WinForm中坐标轴的走向,左上角为原点,向右为X轴,向下为Y轴
private void btnImageOperation_Click( object sender, EventArgs e ) { int height = this.pbImageOld.Image.Height; int width = this.pbImageOld.Image.Width; int[]imageArr=new int[256]; for (int i = 0; i <= 255; i++) { imageArr[i] = 0; } Color pixel; int gray, r, g, b; Bitmap bitmap=(Bitmap)this.pbImageOld.Image; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { pixel = bitmap.GetPixel(j,i);//获取指定坐标对应的像素点的颜色 r = pixel.R; g = pixel.G; b = pixel.B; gray = (int)(0.3 * r + 0.59 * g + 0.11 * b);//将RGB换成灰度 imageArr[gray] = imageArr[gray] + 1;//这个亮度的值加1 } } Bitmap delImage = new Bitmap( 256, 256 );//直方图 using (Graphics graphics = Graphics.FromImage( delImage )) { for (int x = 0; x < delImage.Width; x++) { //graphics.DrawLine( Pens.Black, 0, x, imageArr[x], x );//每个色阶画一条直线 graphics.DrawLine(Pens.Black,x,255,x,(255-imageArr[x]));//对应坐标(x,y) (x,y) } } pbImageNew.Image = delImage; }