当前位置:网站首页>C # remove the redundant white part of the picture
C # remove the redundant white part of the picture
2022-07-20 19:16:00 【Brother Xiaohui】
/// <summary>
/// Cut the picture ( Remove the edges )
/// </summary>
/// <param name="FilePath"> Picture path </param>
public void RemoveWhiteEdge(string FilePath)
{
Bitmap bmp = new Bitmap(FilePath);
// Up, down, left, right
int top = 0, left = 0, right = bmp.Width, bottom = bmp.Height;
// Look for the top line , From the left (0) To the right , From above (0) Go down
for (int i = 0; i < bmp.Height; i++)// That's ok
{
bool find = false;
for (int j = 0; j < bmp.Width; j++)// Column
{
Color c = bmp.GetPixel(j, i);
if (!IsWhite(c))
{
top = i;
find = true;
break;
}
}
if (find)
break;
}
// Look for the leftmost line , From above (top position ) Go down , From left to right
for (int i = 0; i < bmp.Width; i++)// Column
{
bool find = false;
for (int j = top; j < bmp.Height; j++)// That's ok
{
Color c = bmp.GetPixel(i, j);
if (!IsWhite(c))
{
left = i;
find = true;
break;
}
}
if (find)
break;
}
// Find the bottom line , From bottom to top , From left to right
for (int i = bmp.Height - 1; i >= 0; i--)// That's ok
{
bool find = false;
for (int j = left; j < bmp.Width; j++)// Column
{
Color c = bmp.GetPixel(j, i);
if (!IsWhite(c))
{
bottom = i;
find = true;
break;
}
}
if (find)
break;
}
// Look for the rightmost line , From top to bottom , From right to left
for (int i = bmp.Width - 1; i >= 0; i--)// Column
{
bool find = false;
for (int j = 0; j <= bottom; j++)// That's ok
{
Color c = bmp.GetPixel(i, j);
if (!IsWhite(c))
{
right = i;
find = true;
break;
}
}
if (find)
break;
}
// Clone a part of a bitmap object .
Rectangle cloneRect = new Rectangle(left, top, right - left, bottom - top);
Bitmap cloneBitmap = bmp.Clone(cloneRect, bmp.PixelFormat);
cloneBitmap.Save(@"D:/service/123.png", ImageFormat.Png);
bmp.Dispose();
}
/// <summary>
/// Judge whether white and pure transparent color (10 Tolerance of points )
/// </summary>
public static bool IsWhite(Color c)
{
// Pure transparency is also white ,RGB All for 255 Pure white
if (c.A < 10 || (c.R > 245 && c.G > 245 && c.B > 245))
return true;
return false;
}
from Make dreams crazy
边栏推荐
- Matlab与1stOpt多元非线性回归
- 欧洲气象中心资料(ERA)下载方法
- Go language environment construction and samples
- Orthanc set up IP access
- WPF RadioButton 样式 (1)
- Matlab regression analysis obtains the coefficient, P value and R2 of predictive variables (only one line of code)
- WPF MVVM下 关闭窗体 并打开新窗体
- arcpy批量转换数据投影(以Albers为例)
- 医疗PACS DICOM 存储 开源商业软件 Orthanc
- Curd of odoo basic development (add, delete, check, change)
猜你喜欢
Extend MySQL under Orthanc windows
【问题解决】微信开发者工具引入组件时报错Uncaught TypeError: Cannot read property ‘addListener‘ of undefined
WPF 自定义控件 实践
Pycharm Debug错误“Process finished with exit code -1073741819 (0xC0000005)”解决方案
Fill polygon with MATLAB
Arcgis/arcpy convert floating-point grid to integer grid
The industry's first "best practice map of live video technology" was released!
Map and set
Matlab TRMM_ 3b42 data format conversion (NC to TIF, with code)
Matlab填充多边形
随机推荐
【Codeforces1706A】 Another String Minimization Problem
Lidar data download steps
新建STM32工程并配置——基于寄存器
MATLAB realizes the distance method of superior and inferior solution (TOPSIS method)
WPF RadioButton style (2)
【问题解决】端口号被占用问题
第七章:集合
Matlab实现优劣解距离法(TOPSIS法)
CodeForces 1417B Two Arrays
Geoscience academic resources (common data and their download addresses)
JSR303数据校验
融云推出实时社区解决方案,垂直兴趣社交「高级玩家」上线
Invalid values in MATLAB fill image
"Error in enabling windows function netfx3" (error cause, detailed analysis and solution) and installation on windows server2012 occur during the installation of SQL Server 2012 NET Framework 3
Orthanc set up IP access
Go基础笔记_5_流程语句
Map and set
WPF 密码框 密码可见切换
Fill polygon with MATLAB
The white paper on the development of distributed storage (2022) issued by the ICT Institute: Huayun data participated in the preparation to speed up the development of the industry