为什么需要指定裁剪分辨率?

发布于 2024-07-29 11:23:32 字数 1107 浏览 4 评论 0原文

我用 C# 编写了一个裁剪图像的方法。 它通过创建一个新的位图并在其上绘制原始图像中的指定矩形(要裁剪的区域)来实现此目的。

对于我尝试过的图像,它产生了错误的结果。 生成的图像的大小是正确的,但内容就是这样。 就好像图像被放大了 2,然后被裁剪一样。 最终添加这一行修复了它:

result.setResolution(72, 72)

但是为什么我需要一个解决方案? 我只使用像素,而不是英寸或厘米。 另外,正确的解决方案是什么?

完整的代码是这个扩展方法:

public static Bitmap Crop(this Image image, int x, int y, int width, int height) {
    Bitmap result = new Bitmap(width, height);
    result.SetResolution(72, 72);

    // Use a graphics object to draw the resized image into the bitmap.
    using (Graphics graphics = Graphics.FromImage(result)) {
        // High quality.
        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        // Draw the image into the target bitmap.
        graphics.DrawImage(image, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
    }

    return result;
}

I wrote a method to crop images in C#. It does it by creating a new Bitmap and drawing onto it a specified rectangle (the area to be cropped) from the original image.

For the images I've tried with it was generating wrong results. The size of the resulting image was right, but the content was it. It was like if the image has been scaled up by 2 and then cropped. Eventually adding this line fixed it:

result.setResolution(72, 72)

But why do I need a resolution? I'm just working with pixels, never with inches or centimeters. Also, what would then be the correct resolution?

The full code is this extension method:

public static Bitmap Crop(this Image image, int x, int y, int width, int height) {
    Bitmap result = new Bitmap(width, height);
    result.SetResolution(72, 72);

    // Use a graphics object to draw the resized image into the bitmap.
    using (Graphics graphics = Graphics.FromImage(result)) {
        // High quality.
        graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
        graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        // Draw the image into the target bitmap.
        graphics.DrawImage(image, 0, 0, new Rectangle(x, y, width, height), GraphicsUnit.Pixel);
    }

    return result;
}

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

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

发布评论

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

评论(2

尹雨沫 2024-08-05 11:23:33

您使用了不正确的 DrawImage 重载。
您应该使用指定 Src 和 Dest 矩形的矩形。

graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);

尝试一下,如果不起作用,请在评论中告诉我。

You are using the incorrect overload of DrawImage.
You should be using the one where you specify the Src and Dest rects.

graphics.DrawImage(image, new Rectangle(0, 0, width, height), new Rectangle(x, y, width, height), GraphicsUnit.Pixel);

Try that and let me know in comments if it doesn't work.

软糖 2024-08-05 11:23:33

我怀疑答案在于图书馆实际进行修改的方式。 它只是复制并粘贴一些内存块。 分辨率指定每个像素使用的位数/字节数。 为了知道他需要复制多少字节,他需要知道每个像素使用了多少位/字节。

因此我认为这是一个简单的乘法,然后是内存复制。

问候

i suspect the answer lies in the way the library actually makes the modification. it just copy and pastes around some blocks of memory. the resolution specifies the number of bits/bytes used per pixel. in order to know how many bytes he needs to copy, he needs to know how many bits/bytes per pixel is used.

therefore i think this is a simple multiplication followed by a memcopy.

regards

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