当前位置:网站首页>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
边栏推荐
猜你喜欢
【HIT-SC-MEMO2】哈工大2022软件构造 复习笔记2
集合--LinkedList
LeetCode_Dec_1st_Week
2020-03-27
tmux concept and usage
基于Webrtc和Janus的多人视频会议系统开发5 - 发布媒体流到Janus服务器
2020-03-27
基于语音识别的QT设计的csgo互动类视频游戏
gRPC intro 1:RPC
Question 1000: Input two integers a and b, calculate the sum of a+b, this question is multiple sets of test data
随机推荐
PS像素画学习-1
counting cycle
基于Webrtc和Janus的多人视频会议系统开发6 - 从Janus服务器订阅媒体流
淘宝分布式文件系统存储(二)
Copy Siege Lion's Annual "Battle" | Review 2020
20170729
通过socks5代理下载webrtc源码错误:curl: (7) Can't complete SOCKS5 connection xx.xx.xx.xx
Pipe redirection
【HIT-SC-MEMO3】哈工大2022软件构造 复习笔记3
LeetCode_22_Apr_4th_Week
结构体传参-C语言
LeetCode_Nov_3rd_Week
Socket编程详解
管道重定向
Janus转发丢包导致音视频不同步原因分析
[开发杂项][编辑器][代码阅读]ctags&vim
IDEA创建Servlet步骤
题目1000:输入两个整数a和b,计算a+b的和,此题是多组测试数据
指针运算相关面试题详解【C语言】
网络安全行业是蓝景吗?