未在正确的位置绘制图像 [英] Image is not drawn at the correct spot

查看:107
本文介绍了未在正确的位置绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Bitmap image = ReadBitmap("image.png");
Bitmap imageCopy = new Bitmap(image);
Bitmap canvas = new Bitmap(imageCopy.Width+100, imageCopy.Height);

// From this bitmap, the graphics can be obtained, because it has the right PixelFormat
using(Graphics g = Graphics.FromImage(canvas))
{
    // Draw the original bitmap onto the graphics of the new bitmap
    g.DrawImage(image, 0, 0);
}

// Use tempBitmap as you would have used originalBmp
InputPictureBox.Image = image;
OutputPictureBox.Image = canvas;   

我不了解此C#代码的输出.

I haven't understood the output of this c# code.

原始图像未放置在正确的位置.它应该在(0, 0). 另外,我需要黑色背景.

The original image is not placed at the correct position. It should have been at (0, 0). Also, I need a black background.

那么,怎么回事以及如何纠正呢?

So, what is going on and how to correct this?

推荐答案

您正在加载图像,然后使用以下方法创建此源的副本:
Bitmap bitmap = new Bitmap();

You are loading an Image, then a copy of this source is created using:
Bitmap bitmap = new Bitmap();

以这种方式创建图像副本时,您会牺牲/更改一些细节:

When you create a copy of an Image this way, you sacrifice/alter some details:
Dpi Resolution: if not otherwise specified, the resolution is set to the UI resolution. 96 Dpi, as a standard; it might be different with different screen resolutions and scaling. The System in use also affects this value (Windows 7 and Windows 10 will probably/possibly provide different values)
PixelFormat: If not directly copied from the Image source or explicitly specified, the PixelFormat is set to PixelFormat.Format32bppArgb.

根据您的发言,您可能想要这样的东西:

From what you were saying, you probably wanted something like this:

using (Bitmap imageSource = (Bitmap)Image.FromFile(@"[SomeImageOfLena]"))
using (Bitmap imageCopy = new Bitmap(imageSource.Width + 100, imageSource.Height, imageSource.PixelFormat))
{
    imageCopy.SetResolution(imageSource.HorizontalResolution, imageSource.VerticalResolution);
    using (Graphics g = Graphics.FromImage(imageCopy))
    {
        g.Clear(Color.Black);
        g.CompositingMode = CompositingMode.SourceCopy;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage(imageSource, (imageCopy.Width - imageSource.Width) / 2, 0);
        pictureBox1.Image = (Image)imageSource.Clone();
        pictureBox2.Image = (Image)imageCopy.Clone();
    }
}

这是结果:
(上/下边框黑色实际上是Picturebox背景色)

This is the result:
(The upper/lower frame black color is actually the Picturebox background color)


当原始图像Dpi分辨率与使用new Bitmap()创建图像副本时使用的基本Dpi分辨率不同时,您的结果可能与预期的不同.

When the original Image Dpi Resolution is different from the base Dpi Resolution used when creating an Image copy with new Bitmap(), your results may be different from what is expected.

在相同情况下,源图像分别为150、96和72 Dpi时会发生以下情况:

This is what happens with a source Image of 150, 96 and 72 Dpi in the same scenario:


另一个重要的细节是 Dispose();显式地调用Dispose方法,或隐式地将Image构造器包含在

Another important detail is the IDisposable nature of the Image object.
When you create one, you have to Dispose() of it; explicitly, calling the Dispose method, or implicitly, enclosing the Image contructor in a Using statement.

也可能不分配直接从FileStream加载的Image对象.
GDI +将锁定文件,您将无法复制,移动或删除它.
使用该文件,与图像相关的所有资源也将被锁定.

Also, possibly, don't assign an Image object directly loaded from a FileStream.
GDI+ will lock the file, and you will not be able to copy, move or delete it.
With the file, all resources tied to the Images will also be locked.

使用new Bitmap()(如果您不关心上述详细信息)或使用

Make a copy with new Bitmap() (if you don't care of the above mentioned details), or with Image.Clone(), which will preserve the Image Dpi Resolution and PixelFormat.

这篇关于未在正确的位置绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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