问题描述
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();
以这种方式创建图像副本时,您会牺牲/更改一些细节:
Dpi分辨率 :如果未另行指定,则将分辨率设置为UI分辨率,标准配置为96 Dpi;屏幕分辨率和缩放比例不同,分辨率可能会有所不同.使用中的系统也会影响此值(Windows 7和Windows 10可能/可能会提供不同的值).
PixelFormat :如果不是直接从图像源复制或未明确指定,则PixelFormat
设置为PixelFormat.Format32bppArgb
.
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:
另一个重要的细节是图像对象的IDisposable 性质.
创建一个时,您必须 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()
(如果您不关心上述详细信息)或使用 Image.Clone(),它将保留图像Dpi Resolution
和PixelFormat
.
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
.
这篇关于未在正确的位置绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!