博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
清除图片周围的空白区域
阅读量:6448 次
发布时间:2019-06-23

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

using System;using System.Drawing;using System.Drawing.Imaging;using System.Runtime.InteropServices;namespace ConsoleApplication1{    class ImageUtil    {        public static void  ClearBlank(string sourceFileName,out int x,out int y,out int w,out int h)         {
            using (var image = new Bitmap(sourceFileName))             {
                //获取图像的BitmapData对像                 var data = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);                 //循环处理                 var minW = 1000000;                 var maxW = 0;                 var minH = 1000000;                 var maxH = 0;                 unsafe                 {
                    byte* ptr = (byte*)(data.Scan0.ToPointer());                     for (var i = 0; i < data.Height; i++)                     {
                        for (var j = 0; j < data.Width; j++)                         {
                            var a = Convert.ToInt32(ptr[0].ToString());                             var b = Convert.ToInt32(ptr[1].ToString());                             var c = Convert.ToInt32(ptr[2].ToString());                             if (a < 150 && b < 150 && c < 150)                             {
                                if ((j + 1) > maxW)                                 {
                                    maxW = j + 1;                                 }                                 if ((i + 1) > maxH)                                 {
                                    maxH = i + 1;                                 }                                 if ((j + 1) < minW)                                 {
                                    minW = j + 1;                                 }                                 if ((i + 1) < minH)                                 {
                                    minH = i + 1;                                 }                             }                             ptr += 3;                         }                         ptr += data.Stride - data.Width * 3;                     }                 }                 x = minW;                 y = minH;                 w = maxW - minW;                 h = maxH - minH;             }         } }}

 

转载地址:http://vxlwo.baihongyu.com/

你可能感兴趣的文章
reids 安装记录
查看>>
获取androdmanifest里面的meta-data
查看>>
Centos 6.3编译安装nagios
查看>>
如何实现7*24小时灵活发布?阿里技术团队这么做
查看>>
iSCSI
查看>>
java1234_Activiti_第6讲_一般程序员使用的函数
查看>>
mysql拷贝表的几种方式
查看>>
NetApp FAS2240-4存储删除文件数据恢复
查看>>
技术人在学习爱的路上
查看>>
LVS -NAT模式配置实例
查看>>
北航 2012 秋季 现代软件工程 团队项目要求
查看>>
获取通讯组属性Get-DistributionGroup
查看>>
"知识管理夏季论坛",免费,欢迎你来!
查看>>
常用DOS命令
查看>>
能上QQ上不了网的解决办法
查看>>
flask + Python3 实现的的API自动化测试平台---- IAPTest接口测试平台
查看>>
【翻译】将Ext JS Grid转换为Excel表格
查看>>
关于人工智能的几个问题
查看>>
个人阅读作业2
查看>>
解决百度上传WebUploader在IE浏览器下点击无反应的问题
查看>>