图片反转
2016-05-21 22:26
429 查看
图片反转:
伪彩色图片:
源码下载
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; } }
源码下载
相关文章推荐
- Linux下yum安装软件
- 46. Permutations
- 链表
- Unity3D 之UGUI 文本框和编辑框
- python 有关引用的一些问题
- AD转换设计中的基本问题整理
- 【福利】一些不用申请可调用的天气api接口
- Maven入门
- onethink后台上传图片,同一张图只能上传一次的问题
- 用java给html文件添加必要的控制html代码片
- Python Mysql Connect By Two ways
- 编码规范-如何写个优美的C++类
- 不以营销为目的的内容都是耍流氓
- 不以营销为目的的内容都是耍流氓
- 简单选择排序——java
- 剑指offer面试题4(java实现)
- 29.UITabBarController
- 【步兵 c++】当模版遇上二进制
- C语言——通过指针如何操作字符串——储存和运算
- SQL SERVER 条件语句的查询