Search code examples
c#image-processingvideo-capture

Bitmap.Save() method exception


I am trying to write a windows application to convert .avi videos to bitmap frames. I am able to get the bitmaps but I am having problems saving them.

Frames are saved perfectly up to 1649th frame. After that I get this exception:

Attempted to read or write protected memory. This is often an indication that other memory is corrupt

I ran the code several times, the code always throw above exception when processing 1649th frame. The output folder is empty at the beginning and its size is 389 MBs when the program stops.

I am guessing that windows do not allow a program to write this amount of data in a short interval but I am not sure and I don't know how to fix it. Can anyone help?

for(counter = reader.Start; counter<(reader.Start + reader.Length); counter++)
{
    DummyBitmap = reader.GetNextFrame();
    DummyBitmap.Save(folderBrowserDialog2.SelectedPath + "\\" + counter.ToString()      + ".bmp");
    reader.Position++;
}

Solution

  • Bitmap class implements IDisposable interface so it would be wise to use it as:

    using (var b = new Bitmap(...))
    {
    
    }
    

    Also, maybe this post can give you some answers: Bitmap memory leak.