如何将图像放在Bitmap的图片框中
是否可以从内存中加载图片( byte[]
或stream
或Bitmap
)而不将其保存到磁盘?
这是我用来将byte[]
数组转换为Bitmap
:
unsafe { fixed (byte* ptr = Misc.ConvertFromUInt32Array(image)) { Bitmap bmp = new Bitmap(200, 64, 800, PixelFormat.Format32bppRgb, new IntPtr(ptr)); bmp.RotateFlip(RotateFlipType.Rotate180FlipX); bmp.MakeTransparent(Color.Black); bmp.Save("test.bmp"); } }
我可以将Bitmap
放在表单上的图片框中,而不是使用Bmp.save()
吗?
你试过这个吗?
pictureBox.Image = bmp;
我有一些类似于接受的答案导致内存泄漏的代码。 问题是,当您将图片框图像设置为位图时,您仍然指的是位图,而不是创建副本。 如果需要多次设置图像,则需要确保处理所有旧位图。
这适用于任何想要将位图克隆到图像框的人。 试试这个:
if (pictureBox.Image != null) pictureBox.Image.Dispose(); pictureBox.Image = myBitmap.Clone( new Rectangle(0, 0, myBitmap.Width, myBitmap.Height), System.Drawing.Imaging.PixelFormat.DontCare);
如果您正在使用C ++编程语言,可以这样做:
void backGroundImage() { Image^ back = gcnew Bitmap("C:\\Users\\User\\Documents\\image.bmp"); pictureBox1->BackGroundImage = back; };
然后,当您需要加载位图时,可以调用backGroundImage
。
pictureBox.Image = Properties.Resources.image.bmp;