zoukankan      html  css  js  c++  java
  • 清除图片周围的空白区域

    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;
                }
            } } }
  • 相关阅读:
    TeamViewer14
    mysql 导出表结构和表数据 mysqldump用法
    虚拟机中不能上外网
    Mysql初始化root密码和允许远程访问
    常用sql语句
    查看连接MYSQL数据库的IP信息
    设置linux下shell显示不同颜色的字体
    常用mysql导入导出数据的命令
    spring boot 以jar的方式启动常用shell脚本
    idea的properties文件乱码问题解决
  • 原文地址:https://www.cnblogs.com/littlehb/p/5119123.html
Copyright © 2011-2022 走看看