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

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

问题描述

Bitmap image = ReadBitmap("image.png");位图 imageCopy = new Bitmap(image);位图画布 = new Bitmap(imageCopy.Width+100, imageCopy.Height);//从这个位图中可以得到图形,因为它有正确的PixelFormat使用(图形 g = Graphics.FromImage(画布)){//将原始位图绘制到新位图的图形上g.DrawImage(image, 0, 0);}//像使用 originalBmp 一样使用 tempBitmapInputPictureBox.Image = 图像;OutputPictureBox.Image = canvas;

我还没有理解这个 c# 代码的输出.

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

那么,这是怎么回事,如何纠正?

解决方案

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

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

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

这是在同一场景中使用 150、96 和 72 Dpi 的源图像发生的情况:


另一个重要的细节是 IDisposable 图像对象的性质.
创建一个时,您必须处置();显式地调用 Dispose 方法,或隐式地将图像构造器包含在 使用语句.

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

使用 new Bitmap()(如果您不关心上述细节)或 Image.Clone(),它将保留图像Dpi ResolutionPixelFormat.

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;   

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

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?

解决方案

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();
    }
}

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


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.

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


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.

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.

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天全站免登陆