通过GDI+对图片数据进行处理,下面列出各个效果的算法

对于读取图片的每个像素颜色的速度非常慢,这里使用LockBitmap类来对图片像素进行操作,LockBitmap类的定义看这里

http://www.cnblogs.com/bomo/archive/2013/02/26/2934055.html

1、旋转(90度,180度,270度)

        //旋转90,180,270public Bitmap RotateImage(Bitmap bmp, int angle){if (angle != 90 && angle != 180 && angle != 270){return null;}int width = bmp.Width;int height = bmp.Height;if (angle == 90){Bitmap newbmp = new Bitmap(height, width);using (Graphics g = Graphics.FromImage(newbmp)){Point[] destinationPoints = {new Point(height, 0), // destination for upper-left point of originalnew Point(height, width),// destination for upper-right point of originalnew Point(0, 0)}; // destination for lower-left point of original
                    g.DrawImage(bmp, destinationPoints);}return newbmp;}if (angle == 180){Bitmap newbmp = new Bitmap(width, height);using (Graphics g = Graphics.FromImage(newbmp)){Point[] destinationPoints = {new Point(width, height), // destination for upper-left point of originalnew Point(0, height),// destination for upper-right point of originalnew Point(width, 0)}; // destination for lower-left point of original
                    g.DrawImage(bmp, destinationPoints);}return newbmp;}if(angle == 270){Bitmap newbmp = new Bitmap(height, width);using (Graphics g = Graphics.FromImage(newbmp)){Point[] destinationPoints = {new Point(0, width), // destination for upper-left point of originalnew Point(0, 0),// destination for upper-right point of originalnew Point(height, width)}; // destination for lower-left point of original
                    g.DrawImage(bmp, destinationPoints);}return newbmp;}return null;}

2、重设大小

        //重设大小public Bitmap ResizeImage(Bitmap bmp, Size size){Bitmap newbmp = new Bitmap(size.Width, size.Height);using (Graphics g = Graphics.FromImage(newbmp)){g.DrawImage(bmp, new Rectangle(Point.Empty, size));}return newbmp;}

3、底片效果(反色)(255-r, 255-g, 255-b)

        //底片public Bitmap NegativeImage(Bitmap bmp){int height = bmp.Height;int width = bmp.Width;Bitmap newbmp = new Bitmap(width, height);LockBitmap lbmp = new LockBitmap(bmp);LockBitmap newlbmp = new LockBitmap(newbmp);lbmp.LockBits();newlbmp.LockBits();Color pixel;for (int x = 1; x < width; x++){for (int y = 1; y < height; y++){int r, g, b;pixel = lbmp.GetPixel(x, y);r = 255 - pixel.R;g = 255 - pixel.G;b = 255 - pixel.B;newlbmp.SetPixel(x, y, Color.FromArgb(r, g, b));}}lbmp.UnlockBits();newlbmp.UnlockBits();return newbmp;}

4、黑白效果

        //黑白public Bitmap GrayImage(Bitmap bmp, int type){int height = bmp.Height;int width = bmp.Width;Bitmap newbmp = new Bitmap(width, height);LockBitmap lbmp = new LockBitmap(bmp);LockBitmap newlbmp = new LockBitmap(newbmp);lbmp.LockBits();newlbmp.LockBits();Color pixel;for (int x = 0; x < width; x++){for (int y = 0; y < height; y++){pixel = lbmp.GetPixel(x, y);int r, g, b, Result = 0;r = pixel.R;g = pixel.G;b = pixel.B;switch (type){case 0://平均值法Result = ((r + g + b) / 3);break;case 1://最大值法Result = r > g ? r : g;Result = Result > b ? Result : b;break;case 2://加权平均值法Result = ((int)(0.3 * r) + (int)(0.59 * g) + (int)(0.11 * b));break;}newlbmp.SetPixel(x, y, Color.FromArgb(Result, Result, Result));}}lbmp.UnlockBits();newlbmp.UnlockBits();return newbmp;}

5、浮雕:找出附近的像素点r1,abs(r-r2+128)

        //浮雕public Bitmap EmbossmentImage(Bitmap bmp){int height = bmp.Height;int width = bmp.Width;Bitmap newbmp = new Bitmap(width, height);LockBitmap lbmp = new LockBitmap(bmp);LockBitmap newlbmp = new LockBitmap(newbmp);lbmp.LockBits();newlbmp.LockBits();Color pixel1, pixel2;for (int x = 0; x < width - 1; x++){for (int y = 0; y < height - 1; y++){int r = 0, g = 0, b = 0;pixel1 = lbmp.GetPixel(x, y);pixel2 = lbmp.GetPixel(x + 1, y + 1);r = Math.Abs(pixel1.R - pixel2.R + 128);g = Math.Abs(pixel1.G - pixel2.G + 128);b = Math.Abs(pixel1.B - pixel2.B + 128);if (r > 255)r = 255;if (r < 0)r = 0;if (g > 255)g = 255;if (g < 0)g = 0;if (b > 255)b = 255;if (b < 0)b = 0;newlbmp.SetPixel(x, y, Color.FromArgb(r, g, b));}}lbmp.UnlockBits();newlbmp.UnlockBits();return newbmp;}

6、柔化

        //柔化public Bitmap SoftenImage(Bitmap bmp){int height = bmp.Height;int width = bmp.Width;Bitmap newbmp = new Bitmap(width, height);LockBitmap lbmp = new LockBitmap(bmp);LockBitmap newlbmp = new LockBitmap(newbmp);lbmp.LockBits();newlbmp.LockBits();Color pixel;//高斯模板int[] Gauss ={ 1, 2, 1, 2, 4, 2, 1, 2, 1 };for (int x = 1; x < width - 1; x++){for (int y = 1; y < height - 1; y++){int r = 0, g = 0, b = 0;int Index = 0;for (int col = -1; col <= 1; col++){for (int row = -1; row <= 1; row++){pixel = lbmp.GetPixel(x + row, y + col);r += pixel.R * Gauss[Index];g += pixel.G * Gauss[Index];b += pixel.B * Gauss[Index];Index++;}}r /= 16;g /= 16;b /= 16;//处理颜色值溢出r = r > 255 ? 255 : r;r = r < 0 ? 0 : r;g = g > 255 ? 255 : g;g = g < 0 ? 0 : g;b = b > 255 ? 255 : b;b = b < 0 ? 0 : b;newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));}}lbmp.UnlockBits();newlbmp.UnlockBits();return newbmp;}

7、锐化

        //锐化public Bitmap SharpenImage(Bitmap bmp){int height = bmp.Height;int width = bmp.Width;Bitmap newbmp = new Bitmap(width, height);LockBitmap lbmp = new LockBitmap(bmp);LockBitmap newlbmp = new LockBitmap(newbmp);lbmp.LockBits();newlbmp.LockBits();Color pixel;//拉普拉斯模板int[] Laplacian = { -1, -1, -1, -1, 9, -1, -1, -1, -1 };for (int x = 1; x < width - 1; x++){for (int y = 1; y < height - 1; y++){int r = 0, g = 0, b = 0;int Index = 0;for (int col = -1; col <= 1; col++){for (int row = -1; row <= 1; row++){pixel = lbmp.GetPixel(x + row, y + col); r += pixel.R * Laplacian[Index];g += pixel.G * Laplacian[Index];b += pixel.B * Laplacian[Index];Index++;}}//处理颜色值溢出r = r > 255 ? 255 : r;r = r < 0 ? 0 : r;g = g > 255 ? 255 : g;g = g < 0 ? 0 : g;b = b > 255 ? 255 : b;b = b < 0 ? 0 : b;newlbmp.SetPixel(x - 1, y - 1, Color.FromArgb(r, g, b));}}lbmp.UnlockBits();newlbmp.UnlockBits();return newbmp;}

8、雾化

        //雾化public Bitmap AtomizationImage(Bitmap bmp){int height = bmp.Height;int width = bmp.Width;Bitmap newbmp = new Bitmap(width, height);LockBitmap lbmp = new LockBitmap(bmp);LockBitmap newlbmp = new LockBitmap(newbmp);lbmp.LockBits();newlbmp.LockBits();System.Random MyRandom = new Random();Color pixel;for (int x = 1; x < width - 1; x++){for (int y = 1; y < height - 1; y++){int k = MyRandom.Next(123456);//像素块大小int dx = x + k % 19;int dy = y + k % 19;if (dx >= width)dx = width - 1;if (dy >= height)dy = height - 1;pixel = lbmp.GetPixel(dx, dy);newlbmp.SetPixel(x, y, pixel);}}lbmp.UnlockBits();newlbmp.UnlockBits();return newbmp;}

【C#】图片处理(底片,黑白,锐化,柔化,浮雕,雾化)相关推荐

  1. Asp.net(C#)-彩色图片转化为黑白

        /// <summary>      /// 彩色图片转化为黑白      /// </summary>      /// <param name="s ...

  2. [css] 使用css将图片转换成黑白的效果

    [css] 使用css将图片转换成黑白的效果 filter: saturate(0); 个人简介 我是歌谣,欢迎和大家一起交流前后端知识.放弃很容易, 但坚持一定很酷.欢迎大家一起讨论 主目录 与歌谣 ...

  3. 纯CSS将图片转换成黑白

    纯CSS将图片转换成黑白 几行代码就可以实现!效果非常好! 1. 正常效果截图 鼠标悬停前后,分别示下: 2. 非正常效果截图:代码设置的不同,显示的效果不同! 比如:下面的设置导致图片效果不好! 参 ...

  4. 小tip: 使用CSS将图片转换成黑白(灰色、置灰)[转]

    小tip: 使用CSS将图片转换成黑白(灰色.置灰) 这篇文章发布于 2012年08月19日,星期日,20:41,归类于 css相关, SVG相关. 阅读 159943 次, 今日 146 次 by ...

  5. 004-如何用PS把彩色图片设置成黑白图片?

    方法/步骤 1.第1步:首先,打开一张彩色的图片. 2.第2步:然后,鼠标左键点击菜单栏中的[图像]按钮. 3.第三步:最后,在[图像]按钮的下拉菜单中依次点选[调整]-[去色],就可以把彩色图片设置 ...

  6. Android png图片彩色转黑白,透明度打印变黑色的解决方法

    最近客户有个需求:需要将彩色的图片转成黑白图片用pos机的打印机打印出来. 这个在实现的过程中发现个问题:从网上下载png图片,保存到本地发现透明背景变成了黑色. 正确的: 本地下载的: 因为第一次遇 ...

  7. 「Python|图像处理|场景案例」如何将彩色图片转成黑白的灰度图片?

    本文主要介绍如何使用python将彩色图片转换成黑白的灰度图片. 文章目录 场景描述 解决方案 具体代码 场景描述 假设我们现在有如下一张彩色图片: 我们希望将它转换成黑白图片,如下: 解决方案 将彩 ...

  8. [PPTX解析] 图片效果算法篇:柔化边缘

    PPTX解析:柔化边缘 PPT中可以对形状和图片进行柔化边缘操作(如图所示),其本质上可以看为对一个可视化对象的呈现进行视觉处理操作.通过本篇内容,我们将介绍柔化边缘的存储相关,并将说明我们如何实现近 ...

  9. css如何把图片设置为黑白_使用CSS将图像转换为黑白图像

    css如何把图片设置为黑白 Desaturating a color image couldn't be simpler with CSS. The filter is typically appli ...

  10. 浅谈JavaScript和Canvas实现彩色图片转换成黑白图片

    1.convertToGray() 在主体代码中使用 canvas 元素的id来获取画布,并设置画布的宽和高为图片的宽和高,再将上下文初始化为2d画布:彩色图片加载完成后,使用 drawImage() ...

最新文章

  1. 程序猿生存指南-4 借钱风波
  2. Multi-task Learning(Review)多任务学习概述
  3. 根据矩阵变化实现基于 HTML5 的 WebGL 3D 自动布局
  4. 马斯克一作!Neuralink脑机接口细节公布,特殊材料防大脑损伤,专用芯片助技术落地...
  5. 电子邮件成企业主动营销的首选工具
  6. Python使用循环实现1-100的和
  7. Visula Basic程序设计理论与实践pdf
  8. jbpm小项目测试_尝试使用jBPM Console NG(测试版)
  9. 提防Java中的函数式编程!
  10. 快速pow和sqrt的小技巧 hdu4282
  11. 解决oracle数据库连接不上的问题
  12. MOS管及MOS管的驱动电路设计
  13. 从IBM SCE+落地中国看IDC的转型
  14. (CMA-ES源码)协方差自适应进化策略(Covariance Matrix Adaptation Evolution Strategy,CMA-ES)——最好的单目标进化算法?
  15. 山东大学人工智能导论实验四 利用神经网络分类红色和蓝色的花
  16. Xcode(OC):control reaches end of non-void function
  17. 《后端》bug: java.lang.IllegalArgumentException: geronimo.jta.1.1.spec: Invalid module name: ‘1‘ is not
  18. 太阳神朱厚丞:揭秘2018年物联网发展趋势
  19. 【数学模型】基于Volterra理论的捕食模型
  20. 解决idea 拉取新项目没有maven窗口

热门文章

  1. 安装win8/win10提示无法在驱动器0分区上安装windows解决方法
  2. jmeter(十三)常见问题及解决方法
  3. Windows无法安装到这个磁盘 选中的磁盘具有MBR分区表解决方法
  4. VS2013、VS2015中,新建项目没有看到解决方案的问题(已解决)
  5. 如何查看Git提交中的更改?
  6. 如何实施基本的“长轮询”?
  7. 台式电脑如何重装系统windows10
  8. 用python直接调用asr技术_python中asr
  9. catti二级笔译综合能力真题_2006年-2011年CATTI二级笔译综合能力试题及答案2018年.doc...
  10. XElement.Load 需要释放吗_因为信用卡逾期还不上坐牢了,刑满释放后,还需要继续还钱吗?...