c# - 如何从 PNG 图像中删除空白并重新保存它们

标签 c# c#-4.0 image-processing photoshop

好吧,我不知道这是否可以通过编程或任何软件来实现,但让我们学习一下。

现在让我用例子来演示

我正在使用paint.net魔棒容差0%

空白

enter image description here

此处空白已删除版本

enter image description here

这可以通过任何软件(例如 c# 或 photoshop 等)来完成

我需要进行批处理

最佳答案

我真的不知道这是否能回答你的问题,但我希望它能回答

以下代码用于从 Darren 的图像中删除周围的白色空间。

public static Bitmap Crop(Bitmap bmp)
    {
        int w = bmp.Width;
        int h = bmp.Height;

        Func<int, bool> allWhiteRow = row =>
        {
            for (int i = 0; i < w; ++i)
                if (bmp.GetPixel(i, row).R != 255)
                    return false;
            return true;
        };

        Func<int, bool> allWhiteColumn = col =>
        {
            for (int i = 0; i < h; ++i)
                if (bmp.GetPixel(col, i).R != 255)
                    return false;
            return true;
        };

        int topmost = 0;
        for (int row = 0; row < h; ++row)
        {
            if (allWhiteRow(row))
                topmost = row;
            else break;
        }

        int bottommost = 0;
        for (int row = h - 1; row >= 0; --row)
        {
            if (allWhiteRow(row))
                bottommost = row;
            else break;
        }

        int leftmost = 0, rightmost = 0;
        for (int col = 0; col < w; ++col)
        {
            if (allWhiteColumn(col))
                leftmost = col;
            else
                break;
        }

        for (int col = w - 1; col >= 0; --col)
        {
            if (allWhiteColumn(col))
                rightmost = col;
            else
                break;
        }

        if (rightmost == 0) rightmost = w; // As reached left
        if (bottommost == 0) bottommost = h; // As reached top.

        int croppedWidth = rightmost - leftmost;
        int croppedHeight = bottommost - topmost;

        if (croppedWidth == 0) // No border on left or right
        {
            leftmost = 0;
            croppedWidth = w;
        }

        if (croppedHeight == 0) // No border on top or bottom
        {
            topmost = 0;
            croppedHeight = h;
        }

        try
        {
            var target = new Bitmap(croppedWidth, croppedHeight);
            using (Graphics g = Graphics.FromImage(target))
            {
                g.DrawImage(bmp,
                  new RectangleF(0, 0, croppedWidth, croppedHeight),
                  new RectangleF(leftmost, topmost, croppedWidth, croppedHeight),
                  GraphicsUnit.Pixel);
            }
            return target;
        }
        catch (Exception ex)
        {
            throw new Exception(
              string.Format("Values are topmost={0} btm={1} left={2} right={3} croppedWidth={4} croppedHeight={5}", topmost, bottommost, leftmost, rightmost, croppedWidth, croppedHeight),
              ex);
        }
    }

谢谢

我希望这有帮助:)

关于c# - 如何从 PNG 图像中删除空白并重新保存它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12809560/

相关文章:

c# - 在控件上使用 DoubleBuffer 的优缺点

c# - 想要在运行时在程序集内加载程序集?

c# - 如何使用 LINQ 优化多个 if else

c# - 必须声明标量变量 "@dom"

c# - 如何避免 MinAreaRect 检测到的矩形旋转?

python - 如何使用 OpenCV 检测图像中的波纹

ASP.NET Core 中使用 IActionResult 的 C# 接口(interface)概念说明

c# - 是否可以在 Visual Studio 2010 的 ReportViewer 中重复使用页眉或页脚?

c# - WPF slider 和日期

c++ - OpenGL 计算着色器 - 奇怪的结果