当前位置:网站首页>C# 剪裁图片内容区域
C# 剪裁图片内容区域
2022-08-04 05:33:00 【wenshanshan0824】
效果:

功能:
通过过滤条件找到图片有效边缘,然后剪切
其他说明:
1.时间匆忙,代码没有组织,比较乱
2.没想到什么好算法,最土的,4次嵌套找到4个点,然后剪裁
3.过滤条件可以根据实际情况自己修改
4.嵌套循环一次跨10像素,所以最后为了避免意外,追加20像素边距.
代码:
<span style="white-space:pre"> </span>Bitmap image =new Bitmap(pictureBox1.Image);
Bitmap new_image;
//只保留有效区域
int iWidth = image.Width;
int iHeight = image.Height;
int startX = -1;
int startX_T = -1;
int startY = -1;
int startY_T = -1;
int endX = -1;
int endX_T = -1;
int endY = -1;
int endY_T = -1;
Color colCurr;
//通过两次嵌套确定起点xy
for (int y = 0; y < iHeight; y = y + 10)
{
for (int x = 0; x < iWidth; x = x + 10)
{
colCurr = image.GetPixel(x, y);
if (Convert.ToInt32( colCurr.R.ToString())<200)
{
startX_T = x;
startY_T = y;
break;
}
}
if (startX_T != -1)
{
break;
}
}
for (int x = 0; x < iWidth; x = x + 10)
{
for (int y = 0; y < iHeight; y = y + 10)
{
colCurr = image.GetPixel(x, y);
if (Convert.ToInt32(colCurr.R.ToString()) < 200)
{
if (startX_T > x)
{
startX = x;
}
else
{
startX = startX_T;
}
if (startY_T > y)
{
startY = y;
}
else
{
startY = startY_T;
}
break;
}
}
if (startX != -1)
{
break;
}
}
//通过两次嵌套确定结束点xy
for (int y = iHeight - 1; y >= 0; y = y - 10)
{
for (int x = iWidth - 1; x >= 0; x = x - 10)
{
colCurr = image.GetPixel(x, y);
if (Convert.ToInt32(colCurr.R.ToString()) < 200)
{
endY_T = y;
endX_T = x;
break;
}
}
if (endY_T != -1)
{
break;
}
}
for (int x = iWidth - 1; x >= 0; x = x - 10)
{
for (int y = iHeight - 1; y >= 0; y = y - 10)
{
colCurr = image.GetPixel(x, y);
if (Convert.ToInt32(colCurr.R.ToString()) < 200)
{
if (endX_T > x)
{
endX = endX_T;
}
else
{
endX = x;
}
if (endY_T > y)
{
endY = endY_T;
}
else
{
endY = y;
}
break;
}
}
if (endY != -1)
{
break;
}
}
Console.WriteLine("startX:" + startX + "startY:" + startY + "entX:" + endX + "endY:" + endY);
//处理边缘,强制+20
if (startX > 20)
{
startX = startX - 20;
}
else
{
startX = 0;
}
if (startY > 20)
{
startY = startY - 20;
}
else
{
startY = 0;
}
if (iWidth - endX > 20)
{
endX = endX + 20;
}
else
{
endX = iWidth;
}
if (iHeight - endY > 20)
{
endY = endY + 20;
}
else
{
endY = iHeight;
}
Rectangle cropArea = new Rectangle();
cropArea.X = startX;
cropArea.Y = startY;
cropArea.Width = endX - startX;
cropArea.Height = endY - startY;
new_image = image.Clone(cropArea, image.PixelFormat);
this.pictureBox2.Image = new_image;
new_image.Save("eff_new.png", System.Drawing.Imaging.ImageFormat.Png);http://download.csdn.net/detail/wenshanshan0824/9548644
边栏推荐
- JDBC第一学之进行数据库连接时出现The server time zone.....解决办法
- Stream API
- Copy Siege Lion's Annual "Battle" | Review 2020
- C语言对文件的操作(完整版)
- 【HIT-SC-MEMO2】哈工大2022软件构造 复习笔记2
- 新冠病毒和网络安全的异同及思考
- The usefulness of bind() system call
- 题目1000:输入两个整数a和b,计算a+b的和,此题是多组测试数据
- 华为鲲鹏arm服务器下使用webrtc和boost踩坑记--编译篇
- Fabric v1.1 environment construction
猜你喜欢
随机推荐
The usefulness of bind() system call
webrtc代码解读二:音视频播放同步过程
EL expression
网络安全学习的三大不可取之处
Usage of SFTP
LeetCode_Nov_2nd_Week
clssloader与双亲委派
动态内存管理-C语言
Shell基础
无一技之长学什么可以做到月入上万?
安装Apache服务时出现的几个问题, AH00369,AH00526,AH00072....
IP 核之 MMCM/PLL 实验
集合---ArrayList的底层
【C语言】数组名是什么
LeetCode_Nov_3rd_Week
[Development Miscellaneous][Editor][Code Reading]ctags & vim
[开发杂项][调试]debug into kernel
[开发杂项][编辑器][代码阅读]ctags&vim
Tensorflow/Pytorch安装(Anaconda环境下,无版本冲突,亲测有效)
[daily office][ssh]cheatsheet









