当覆盖两个相同大小的图像,一个是偏移 [英] When overlaying two identically-sized images, one is offset

查看:181
本文介绍了当覆盖两个相同大小的图像,一个是偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过在另一个上面覆盖一个创建的图像。在code的作品,但我覆盖的形象似乎略有拉长,我不明白为什么。

I am attempting to create an image by overlaying one on top of another. The code works, but the image I am overlaying seems to be slightly stretched and I can't work out why.

所以,code只是创建一个空白的红色长方形24x24的,然后我一个覆盖24x24的PNG文件看起来像这样:

So the code just creates a blank red 24x24 rectangle, then I overlay a 24x24 png file which looks like this:

在这里输入的形象描述

我所期待的是:

在这里输入的形象描述

但其实我得到这样的:

在这里输入的形象描述

Using backGround As New Bitmap(24, 24, Imaging.PixelFormat.Format32bppArgb)
        Using g = Graphics.FromImage(backGround)
            Using brush1 As New SolidBrush(Color.Red)
                g.FillRectangle(brush1, 0, 0, 24, 24)
                Using topimage = Image.FromFile("C:\Scratch\ManNoRecords24.png")
                    g.DrawImage(topimage, New Point(0, 0))
                End Using
            End Using
        End Using
        backGround.Save("C:\Scratch\Emp.png", Imaging.ImageFormat.Png)
    End Using

调试器显示topImage的属性:

Debugger showing the properties of the topImage:

在这里输入的形象描述

推荐答案

您可以使用

g.DrawImageUnscaledAndClipped(topimage, New Rectangle(0, 0, 24, 24))

来代替,避免了源图像的任何缩放而绘制的。这工作,但我其实不太清楚有什么不对您的解决方案。

instead, to avoid any scaling of the source image while drawing it. This works but I am actually not quite sure what is wrong with your solution.

从<一个href=\"http://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Graphics.cs,2f0714b9077274ba\"相对=nofollow>参考源, DrawImageUnscaledAndClipped 似乎使用像素作为默认单位图像尺寸并因此忽略源图像的DPI设置:

From the Reference Source, DrawImageUnscaledAndClipped seems to use Pixel as the default unit for the image size and hence ignores the DPI setting of the source image:

/// <include file='doc\Graphics.uex' path='docs/doc[@for="Graphics.DrawImageUnscaledAndClipped"]/*' />
/// <devdoc>
/// </devdoc>
public void DrawImageUnscaledAndClipped(Image image, Rectangle rect) {
    if(image == null) {
        throw new ArgumentNullException("image");
    }

    int width = Math.Min(rect.Width, image.Width);
    int height = Math.Min(rect.Height, image.Height);

    //We could put centering logic here too for the case when the image is smaller than the rect
    DrawImage(image, rect, 0, 0, width, height, GraphicsUnit.Pixel);
}

的DrawImage DrawImageUnscaled 不要再依据其内部的DPI设置,马特发现可能重新调整图像出为小于默认96,这导致图像的拉伸

DrawImage and DrawImageUnscaled do not and then probably rescale the image based on its internal DPI setting, which Matt found out to be less than the default 96, which leads to stretching of the image:

/// <include file='doc\Graphics.uex' path='docs/doc[@for="Graphics.DrawImageUnscaled"]/*' />
/// <devdoc>
/// </devdoc>
public void DrawImageUnscaled(Image image, Point point) {
    DrawImage(image, point.X, point.Y);
}

这篇关于当覆盖两个相同大小的图像,一个是偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆