Why do I get the an out of memory error when I use using?

家住魔仙堡 提交于 2019-12-22 04:09:46

问题


I have the following method, to convert a BitmapImage to a System.Drawing.Bitmap:

public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
    Bitmap bitmap;

    using (var ms = new MemoryStream())
    {
        var encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
        encoder.Save(ms);

        bitmap = new Bitmap(ms);
    }

    return bitmap;
}

Whenever I try and use the returned Bitmap object, I get the following error:

OutOfMemoryException occured - Out of memory.

However, whenever I replace the code with this:

public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
    var ms = new MemoryStream();

    var encoder = new JpegBitmapEncoder();
    encoder.Frames.Add(BitmapFrame.Create(bitmapImage));

    encoder.Save(ms);

    return new Bitmap(ms);
}

This works fine. However, I am pretty sure that I am supposed to use using as the MemoryStream object implements IDisposable. What's going on here?


回答1:


Bitmap's constructor Bitmap Constructor (Stream) claims that

You must keep the stream open for the lifetime of the Bitmap.

In your case, when you're using using statement, stream (being Disposable) automatically disposed, so your Bitmap object becomes invalid. It's not about that you allocate too much memory, but about that bitmap point to -something that no longer exists.




回答2:


What @Tigran said was absolutely correct, and I implemented @CodesInChaos' workaround like so:

public static Bitmap BitmapImageToBitmap(BitmapImage bitmapImage)
{
    Bitmap bitmap;

    using (var ms = new MemoryStream())
    {
        var encoder = new JpegBitmapEncoder();
        encoder.Frames.Add(BitmapFrame.Create(bitmapImage));
        encoder.Save(ms);

        using (var localBitmap = new Bitmap(ms))
        {
            bitmap = localBitmap.Clone(new Rectangle(0, 0, localBitmap.Width, localBitmap.Height),
                   PixelFormat.Format32bppArgb);  
        }
    }

    return bitmap;
}


来源:https://stackoverflow.com/questions/14439549/why-do-i-get-the-an-out-of-memory-error-when-i-use-using

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!