图片反转:
if (_srcBitmap != null) //_srcBitmap = (Bitmap)Image.FromFile(_curFileName) { _dstBitmap = (Bitmap)_srcBitmap.Clone(); BitmapData bmpData = _srcBitmap.LockBits(new Rectangle(0, 0, _srcBitmap.Width, _srcBitmap.Height),ImageLockMode.ReadWrite, _srcBitmap.PixelFormat); BitmapData dstData = _dstBitmap.LockBits(new Rectangle(0, 0, _dstBitmap.Width, _dstBitmap.Height),ImageLockMode.ReadWrite, _dstBitmap.PixelFormat); unsafe { byte* p = (byte*)bmpData.Scan0; for (int y = 0; y < bmpData.Height; y++) { for (int x = 0; x < bmpData.Width; x++) { byte* dstp = (byte*)dstData.Scan0 + y * dstData.Stride + x * 3; dstp[0] = p[bmpData.Width * 3 - (x + 1) * 3]; dstp[1] = p[bmpData.Width * 3 - (x + 1) * 3 + 1]; dstp[2] = p[bmpData.Width * 3 - (x + 1) * 3 + 2]; } p += bmpData.Stride; //Stride 为扫描宽度 Stride > bmpData.Width * 3 ,Width 表示每行的像素个数,每个像素占3个byte位 } _srcBitmap.UnlockBits(bmpData); _dstBitmap.UnlockBits(dstData); } _srcBitmap = (Bitmap)_dstBitmap.Clone(); Invalidate(); }
private void Form1_Paint(object sender, PaintEventArgs e) { Graphics g = e.Graphics; if (_srcBitmap != null) { g.DrawImage(_srcBitmap, 10, 30, _srcBitmap.Width, _srcBitmap.Height); } }
伪彩色图片:
private Bitmap gcTrans(Bitmap bmp, bool method, byte seg) { if (bmp != null) { if (System.Drawing.Imaging.PixelFormat.Format24bppRgb == bmp.PixelFormat) { Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height); System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); IntPtr ptr = bmpData.Scan0; int bytes = bmp.Width * bmp.Height * 3; byte[] grayValues = new byte[bytes]; System.Runtime.InteropServices.Marshal.Copy(ptr, grayValues, 0, bytes); bmp.UnlockBits(bmpData); byte[] rgbValues = new byte[bytes]; //清零 Array.Clear(rgbValues, 0, bytes); byte tempB; if (method == false) { //强度分层法 for (int i = 0; i < bytes; i += 3) { byte ser = (byte)(256 / seg); tempB = (byte)(grayValues[i] / ser); //分配任意一种颜色 rgbValues[i + 1] = (byte)(tempB * ser); rgbValues[i] = (byte)((seg - 1 - tempB) * ser); rgbValues[i + 2] = 0; } } else { //灰度级-彩色变换法 for (int i = 0; i < bytes; i += 3) { if (grayValues[i] < 64) { rgbValues[i + 2] = 0; rgbValues[i + 1] = (byte)(4 * grayValues[i]); rgbValues[i] = 255; } else if (grayValues[i] < 128) { rgbValues[i + 2] = 0; rgbValues[i + 1] = 255; rgbValues[i] = (byte)(-4 * grayValues[i] + 2 * 255); } else if (grayValues[i] < 192) { rgbValues[i + 2] = (byte)(4 * grayValues[i] - 2 * 255); rgbValues[i + 1] = 255; rgbValues[i] = 0; } else { rgbValues[i + 2] = 255; rgbValues[i + 1] = (byte)(-4 * grayValues[i] + 4 * 255); rgbValues[i] = 0; } } } bmp = new Bitmap(bmp.Width, bmp.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb); bmpData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); ptr = bmpData.Scan0; System.Runtime.InteropServices.Marshal.Copy(rgbValues, 0, ptr, bytes); bmp.UnlockBits(bmpData); return bmp; } else { return null; } } else { return null; } }
时间: 2024-09-29 20:01:07