23

是否可以从内存(byte[]或)加载图片而不将其保存到磁盘?streamBitmap

这是我用来将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");
    }
}

除了使用Bmp.save(),我可以将Bitmap图片框放在我的表单上吗?

4

3 回答 3

60

你试过这个吗?

pictureBox.Image = bmp;
于 2009-04-13T10:49:13.183 回答
8

我有一些类似于导致内存泄漏的公认答案的代码。问题是当您将图片框图像设置为位图时,您仍然指的是位图,而不是创建副本。如果您需要多次设置图像,则需要确保处理所有旧位图。

这适用于希望将位图克隆到图像框的任何。试试这个:

if (pictureBox.Image != null) pictureBox.Image.Dispose();
pictureBox.Image = myBitmap.Clone(
    new Rectangle(0, 0, myBitmap.Width, myBitmap.Height), 
    System.Drawing.Imaging.PixelFormat.DontCare);
于 2014-05-03T03:15:53.517 回答
0

如果您正在使用 C++ 编程语言,则可以这样做:

void backGroundImage()
{
    Image^ back = gcnew Bitmap("C:\\Users\\User\\Documents\\image.bmp");
    pictureBox1->BackGroundImage = back;
};

然后您可以backGroundImage在需要加载位图时调用。

于 2013-01-12T08:19:15.673 回答