裁剪位图会得到空位图

发布于 2024-10-29 08:17:39 字数 1542 浏览 5 评论 0原文

我从这里(SO)获取了一个裁剪图像的代码。我在包含白色字体的黑色文本的位图上尝试过。我得到的结果是完全白色的输出,没有内容。

        // create new bitmap with desired size and same pixel format
        Bitmap croppedBitmap = new Bitmap(rect.Width, rect.Height, bitmap.PixelFormat);

        // create Graphics "wrapper" to draw into our new bitmap
        // "using" guarantees a call to gfx.Dispose()
        using (Graphics gfx = Graphics.FromImage(croppedBitmap))
        {
            // draw the wanted part of the original bitmap into the new bitmap
            gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
        }

        return croppedBitmap;

有什么猜测吗?

PS如果我在油漆中裁剪,它当然可以工作

编辑

如果我裁剪我的照片,例如它可以工作......

附录

代码:

矩形

        Rectangle 1: 8 50, 95, 80, 30 // invoice number
        Rectangle 2: 625, 778, 475, 22 // Total amount

CropImage():

    public static Bitmap CropImage(Bitmap bitmap, Rectangle rect)
    {
        Bitmap croppedBitmap = new Bitmap(rect.Width, rect.Height, bitmap.PixelFormat);

        using (Graphics gfx = Graphics.FromImage(croppedBitmap))
        {
            gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
        }

        return croppedBitmap;
    }

图像: (敏感数据被隐藏,我只留下了我想要裁剪的部分) http://img33.imageshack.us/img33/5703/modelx.png

I've taken a code from here (SO) that crops images. I tried it on bitmaps containing black text on white font. The result i get in return is a completely white output with no content.

        // create new bitmap with desired size and same pixel format
        Bitmap croppedBitmap = new Bitmap(rect.Width, rect.Height, bitmap.PixelFormat);

        // create Graphics "wrapper" to draw into our new bitmap
        // "using" guarantees a call to gfx.Dispose()
        using (Graphics gfx = Graphics.FromImage(croppedBitmap))
        {
            // draw the wanted part of the original bitmap into the new bitmap
            gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
        }

        return croppedBitmap;

Any guess?

PS if i crop in paint of course it does work

edit

If I crop a picture of me for example it works....

Appendix

Code:

Rectangles:

        Rectangle 1: 8 50, 95, 80, 30 // invoice number
        Rectangle 2: 625, 778, 475, 22 // Total amount

CropImage():

    public static Bitmap CropImage(Bitmap bitmap, Rectangle rect)
    {
        Bitmap croppedBitmap = new Bitmap(rect.Width, rect.Height, bitmap.PixelFormat);

        using (Graphics gfx = Graphics.FromImage(croppedBitmap))
        {
            gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
        }

        return croppedBitmap;
    }

Image:
(Sensitive data is hidden, I only left the part I'm trying to crop)
http://img33.imageshack.us/img33/5703/modelx.png

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

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

发布评论

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

评论(1

○闲身 2024-11-05 08:17:39

固定代码:

    Rectangle rect = new Rectangle(625, 778, 475, 22);

    Bitmap bitmap = Bitmap.FromFile(@"C:\m.png") as Bitmap;

    Bitmap croppedBitmap = new Bitmap(bitmap, rect.Width, rect.Height);
    croppedBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);

    using (Graphics gfx = Graphics.FromImage(croppedBitmap))
    {
        gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
    }

    croppedBitmap.Save(@"C:\m-1.png", System.Drawing.Imaging.ImageFormat.Png);

Fixed code:

    Rectangle rect = new Rectangle(625, 778, 475, 22);

    Bitmap bitmap = Bitmap.FromFile(@"C:\m.png") as Bitmap;

    Bitmap croppedBitmap = new Bitmap(bitmap, rect.Width, rect.Height);
    croppedBitmap.SetResolution(bitmap.HorizontalResolution, bitmap.VerticalResolution);

    using (Graphics gfx = Graphics.FromImage(croppedBitmap))
    {
        gfx.DrawImage(bitmap, 0, 0, rect, GraphicsUnit.Pixel);
    }

    croppedBitmap.Save(@"C:\m-1.png", System.Drawing.Imaging.ImageFormat.Png);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文