当前位置:网站首页>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
边栏推荐
- 新冠病毒和网络安全的异同及思考
- 通过socks5代理下载webrtc源码错误:curl: (7) Can't complete SOCKS5 connection xx.xx.xx.xx
- ZYNQ之FPGA LED 灯闪烁实验
- 沉浸式体验参加网络安全培训班,学习过程详细到底!
- Multi-threaded sequential output
- Question 1000: Input two integers a and b, calculate the sum of a+b, this question is multiple sets of test data
- C语言对文件的操作(完整版)
- IP 核之 MMCM/PLL 实验
- Treating as key frame since WebRTC-SpsPpsIdrIsH264Keyframe is disabled 解决
- [English learning][sentence] good sentence
猜你喜欢
随机推荐
EL表达式
IDEA创建Servlet步骤
Arduino之ESP8266编程学习总结体会
使用cef离屏渲染技术实现在线教育课件和webrtc视频回放融合录制
安装Apache服务时出现的几个问题, AH00369,AH00526,AH00072....
Pipe redirection
SSO单点登陆
Stream API
网络安全求职指南
FAREWARE ADDRESS
MySQL存储过程学习笔记(基于8.0)
webrtc代码解读二:音视频播放同步过程
Object. RequireNonNull method
webrtc技术名词和关键技术要点:SVC,REMB,SVC...
结构体内存对齐-C语言
IDEA中创建web项目实现步骤
SFTP的用法
线性表之动态数组(ArrayList)的自实现
Usage of SFTP
counting cycle