I need to stretch various sized bitmaps to fill a PictureBox. PictureBoxSizeMode.StretchImage
sort of does what I need but can't think of a way to properly add text or lines to the image using this method. The image below is a 5x5 pixel Bitmap stretched to a 380x150 PictureBox.
我需要拉伸各种大小的位图来填充PictureBox。 PictureBoxSizeMode.StretchImage排序做我需要的但却想不出使用这种方法正确地向图像添加文本或线条的方法。下图是一个5x5像素的位图,扩展到380x150 PictureBox。
pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
pictureBox.Image = bmp;
I tried adapting this example and this example this way
我尝试用这种方式调整这个例子和这个例子
using (var bmp2 = new Bitmap(pictureBox.Width, pictureBox.Height))
using (var g = Graphics.FromImage(bmp2))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(Point.Empty, bmp2.Size));
pictureBox.Image = bmp2;
}
but get this
但得到这个
What am I missing?
我错过了什么?
2 个解决方案
#1
5
It appears you're throwing away the bitmap (bmp2
) you'd like to see in your picture box! The using
block from the example you posted is used because the code no longer needs the Bitmap
object after the code returns. In your example you need the Bitmap to hang around, hence no using
-block on the bmp2
variable.
你似乎丢掉了你想在你的图片框中看到的位图(bmp2)!使用您发布的示例中的using块,因为代码返回后代码不再需要Bitmap对象。在您的示例中,您需要使用Bitmap,因此bmp2变量上没有使用块。
The following should work:
以下应该有效:
using (bmp)
{
var bmp2 = new Bitmap(pictureBox.Width, pictureBox.Height);
using (var g = Graphics.FromImage(bmp2))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(Point.Empty, bmp2.Size));
pictureBox.Image = bmp2;
}
}
#2
3
The red X on white background happens when you have an exception in a paint method.
当您在paint方法中有异常时,会出现白色背景上的红色X.
Your error is that you are trying to assign a disposed bitmap as the image source of your picturebox. The use of "using" keyword will dispose the bitmap you are using in the picturebox!
您的错误是您尝试将已处置的位图指定为图片框的图像源。使用“using”关键字将处理您在图片框中使用的位图!
So your exception, i know, will be ObjectDisposedException :)
所以你的异常,我知道,将是ObjectDisposedException :)
You should create the bitmap once and keep it until it is not needed anymore.
您应该创建一次位图并保留它,直到不再需要它为止。
void ReplaceResizedPictureBoxImage(Bitmap bmp)
{
var oldBitmap = pictureBox.Image;
var bmp2 = new Bitmap(pictureBox.Width, pictureBox.Height);
using (var g = Graphics.FromImage(bmp2))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(Point.Empty, bmp2.Size));
pictureBox.Image = bmp2;
}
if (oldBitmap != null)
oldBitmap.Dispose();
}
This function will allow you to replace the old bitmap disposing the previous one, if you need to do that to release resources.
如果您需要这样做以释放资源,此函数将允许您替换处理前一个位图的旧位图。
#1
5
It appears you're throwing away the bitmap (bmp2
) you'd like to see in your picture box! The using
block from the example you posted is used because the code no longer needs the Bitmap
object after the code returns. In your example you need the Bitmap to hang around, hence no using
-block on the bmp2
variable.
你似乎丢掉了你想在你的图片框中看到的位图(bmp2)!使用您发布的示例中的using块,因为代码返回后代码不再需要Bitmap对象。在您的示例中,您需要使用Bitmap,因此bmp2变量上没有使用块。
The following should work:
以下应该有效:
using (bmp)
{
var bmp2 = new Bitmap(pictureBox.Width, pictureBox.Height);
using (var g = Graphics.FromImage(bmp2))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(Point.Empty, bmp2.Size));
pictureBox.Image = bmp2;
}
}
#2
3
The red X on white background happens when you have an exception in a paint method.
当您在paint方法中有异常时,会出现白色背景上的红色X.
Your error is that you are trying to assign a disposed bitmap as the image source of your picturebox. The use of "using" keyword will dispose the bitmap you are using in the picturebox!
您的错误是您尝试将已处置的位图指定为图片框的图像源。使用“using”关键字将处理您在图片框中使用的位图!
So your exception, i know, will be ObjectDisposedException :)
所以你的异常,我知道,将是ObjectDisposedException :)
You should create the bitmap once and keep it until it is not needed anymore.
您应该创建一次位图并保留它,直到不再需要它为止。
void ReplaceResizedPictureBoxImage(Bitmap bmp)
{
var oldBitmap = pictureBox.Image;
var bmp2 = new Bitmap(pictureBox.Width, pictureBox.Height);
using (var g = Graphics.FromImage(bmp2))
{
g.InterpolationMode = InterpolationMode.NearestNeighbor;
g.DrawImage(bmp, new Rectangle(Point.Empty, bmp2.Size));
pictureBox.Image = bmp2;
}
if (oldBitmap != null)
oldBitmap.Dispose();
}
This function will allow you to replace the old bitmap disposing the previous one, if you need to do that to release resources.
如果您需要这样做以释放资源,此函数将允许您替换处理前一个位图的旧位图。