C#实现的图像反转

Bitmap m_Bitmap = new Bitmap(1,1);
//打开图像
private void OpenImage_Click(object sender, System.EventArgs e)
		{
			OpenFileDialog openFileDialog = new OpenFileDialog();
			openFileDialog.Filter = @"Bitmap文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg|所有合适文件(*.bmp;*.jpg)|*.bmp;*.jpg";
			openFileDialog.FilterIndex = 2 ;
			openFileDialog.RestoreDirectory = true ;
			if(DialogResult.OK == openFileDialog.ShowDialog())
			{
				m_Bitmap = (Bitmap)Bitmap.FromFile(openFileDialog.FileName, false);
			}

		}
//反转操作
public bool Invert(Bitmap b)
		{
			BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), 
				ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
			int stride = bmData.Stride;
			System.IntPtr Scan0 = bmData.Scan0;
			unsafe
			{
				byte * p = (byte *)(void *)Scan0;
				int nOffset = stride - b.Width*3;
				int nWidth = b.Width * 3;
				for(int y=0;y<b.Height;++y)
				{
					for(int x=0; x < nWidth; ++x )
					{
						p[0] = (byte)(255-p[0]);
						++p;
					}
					p += nOffset;
				}
			}
			b.UnlockBits(bmData);
			return true;
		}

C#实现的图像反转_第1张图片
C#实现的图像反转_第2张图片

你可能感兴趣的:(C#实现的图像反转)