博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
.net图片自动裁剪白边函数案例
阅读量:5356 次
发布时间:2019-06-15

本文共 3136 字,大约阅读时间需要 10 分钟。

1.项目要求上传白底的图片要进行裁剪白边,于是同事谢了个函数感觉很好用。

2.

#region 剪切白边        ///         /// 剪切白边        ///         ///         /// 
public static Image Crop(Image p) { int x, y ;//for use of X,Y Coordinates of pixels Bitmap b = new Bitmap(p); //image needed to crop Color c = new Color(); //pixel color for use of identifying if background int intLeft=0;//furthest left X coordinate int intRight=0;//furthest right X coordinate int intBottom =0;//furthest to the bottom Y coordinate int intTop =0; y = 0; while(y < b.Height) { x = 0; while (x < b.Width) //loop through pixels on X axis until end of image width { c = b.GetPixel(x, y); //Get the color of the pixel if (c.R != 255&&c.R!=0 && c.G != 255&&c.G!=0 && c.B != 255&&c.B!=0) { if (c.R < 240 || c.G < 240 || c.B < 240) { //Determine if pixel is further left than the value we already have if (intLeft == 0 || intLeft > x ) { intLeft = x; } //Determine if pixel is further to the top than the value we already have if (intTop == 0 || intTop > y ) { intTop = y; } //Determine if pixel is further right than the value we already have if (intRight <= b.Width && intRight < x ) { intRight = x; } //Determine if pixel is further to the bottom than the value we already have if (intBottom <= b.Height && intBottom < y ) { intBottom = y; } } } x += 1; } y += 1; } int intNewWidth = intRight; //Establish width of new cropped image int intNewHeight = intBottom; //Establish height of new cropped image Bitmap imgCropped =new Bitmap(intNewWidth - intLeft + 2, intNewHeight - intTop + 2); Graphics objGraphics = Graphics.FromImage(imgCropped); //set the background color to white (you can choose what you like objGraphics.Clear(System.Drawing.Color.Transparent); int intStartTop = 1 - intTop; /// 40 + 5 int intStartLeft = 1 - intLeft; /// 40 + 5 //Draw the original image to your new cropped sized image objGraphics.DrawImage(b, intStartLeft, intStartTop); b.Dispose(); objGraphics.Dispose(); //return the Cropped image to the caller return imgCropped; }

 

转载于:https://www.cnblogs.com/guozefeng/p/4178250.html

你可能感兴趣的文章
a标签添加点击事件
查看>>
Context.startActivity出现AndroidRuntimeException
查看>>
Intellij idea创建javaWeb以及Servlet简单实现
查看>>
代理网站
查看>>
Open multiple excel files in WebBrowser, only the last one gets activated
查看>>
FFmpeg进行视频帧提取&音频重采样-Process.waitFor()引发的阻塞超时
查看>>
最近邻与K近邻算法思想
查看>>
【VS开发】ATL辅助COM组件开发
查看>>
FlatBuffers In Android
查看>>
《演说之禅》I &amp; II 读书笔记
查看>>
thinkphp3.2接入支付宝支付接口(PC端)
查看>>
response和request
查看>>
【转】在Eclipse中安装和使用TFS插件
查看>>
回到顶部浮窗设计
查看>>
C#中Monitor和Lock以及区别
查看>>
【NOIP2017】奶酪
查看>>
$ 一步一步学Matlab(3)——Matlab中的数据类型
查看>>
5.6.3.7 localeCompare() 方法
查看>>
Linux下好用的简单实用命令
查看>>
常用web字体的使用指南
查看>>