C# 自动裁剪图像

发布于 2024-11-29 10:12:33 字数 128 浏览 3 评论 0原文

我有一个黑白 System.Drawing.Bitmap 我需要自动裁剪它,以便它只有适合图像所需的大小。该图像始终从左上角 (0,0) 位置开始,但我不确定需要多少高度和宽度。有什么办法可以自动裁剪到合适的尺寸吗?

I have a black and white System.Drawing.Bitmap I need to auto-crop it so that it is only as big as needed to fit the image. This image always starts at the top left (0,0) position but I'm not sure how much height and width is will require. If there any way to auto-crop it to size?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

醉城メ夜风 2024-12-06 10:12:33

以下是我使用的代码:

    // Figure out the final size
    int maxX = 0;
    int maxY = 0;
    for (int x = 0; x < bitmap.Width; x++)
    {
        for (int y = 0; y < bitmap.Height; y++)
        {
            System.Drawing.Color c = bitmap.GetPixel(x, y);
            System.Drawing.Color w = System.Drawing.Color.White;
            if (c.R != w.R || c.G != w.G || c.B != w.B)
            {
                if (x > maxX)
                    maxX = x;
                if (y > maxY)
                    maxY = y;
            }
        }
    }
    maxX += 2;

The following is the code I used:

    // Figure out the final size
    int maxX = 0;
    int maxY = 0;
    for (int x = 0; x < bitmap.Width; x++)
    {
        for (int y = 0; y < bitmap.Height; y++)
        {
            System.Drawing.Color c = bitmap.GetPixel(x, y);
            System.Drawing.Color w = System.Drawing.Color.White;
            if (c.R != w.R || c.G != w.G || c.B != w.B)
            {
                if (x > maxX)
                    maxX = x;
                if (y > maxY)
                    maxY = y;
            }
        }
    }
    maxX += 2;
岛歌少女 2024-12-06 10:12:33

如果您的图像不像我的那样从 0,0 开始,这里有一个变体,可以让您获得自动裁剪范围:

public static Rectangle GetAutoCropBounds(Bitmap bitmap)
{
    int maxX = 0;
    int maxY = 0;

    int minX = bitmap.Width;
    int minY = bitmap.Height;

    for (int x = 0; x < bitmap.Width; x++)
    {
        for (int y = 0; y < bitmap.Height; y++)
        {
            var c = bitmap.GetPixel(x, y);
            var w = Color.White;
            if (c.R != w.R || c.G != w.G || c.B != w.B)
            {
                if (x > maxX)
                    maxX = x;
                if (x < minX)
                    minX = x;
                if (y > maxY)
                    maxY = y;
                if (y < minY)
                    minY = y;
            }
        }
    }

    maxX += 2;

    return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}

注意:如果您正在处理任何数量或高分辨率的图像,那么您需要研究一下比 GetPixel 方法更快的替代方法。

If your image does not start at 0,0 like mine, here is a variant that gets you the auto-crop bounds:

public static Rectangle GetAutoCropBounds(Bitmap bitmap)
{
    int maxX = 0;
    int maxY = 0;

    int minX = bitmap.Width;
    int minY = bitmap.Height;

    for (int x = 0; x < bitmap.Width; x++)
    {
        for (int y = 0; y < bitmap.Height; y++)
        {
            var c = bitmap.GetPixel(x, y);
            var w = Color.White;
            if (c.R != w.R || c.G != w.G || c.B != w.B)
            {
                if (x > maxX)
                    maxX = x;
                if (x < minX)
                    minX = x;
                if (y > maxY)
                    maxY = y;
                if (y < minY)
                    minY = y;
            }
        }
    }

    maxX += 2;

    return new Rectangle(minX, minY, maxX - minX, maxY - minY);
}

Note: if you are processing any amount of images in quantity or high-resolution then you'll want to look into faster alternatives than the GetPixel method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文