Bitmap.Save(): A Generic Error Occurred in GDI+

If you are trying to modify Bitmap, you may encounter the following GDI error which is very generic and does not provide any details.

Bitmap.Save(): A generic error occurred in GDI+

Why this generic GDI error occurred?

When you initializing a Bitmap object from an image stored on hard disk, it creates a lock on the underlying image file. Due to lock when you try to save your modified bitmap, it throws this error.

How do I fix this generic GDI error?

There are two ways to fix this issue

  • Instead of overwriting the file, save a new file with a different name than the original file
  • Only when the Bitmap object is disposed, the underlying lock on the file is removed. Once the lock is removed, you may overwrite the file. If you must overwrite the existing file, create a separate bitmap object from existing bitmap object. Now dispose the old bitmap object which will release the lock on the image file. Go ahead and make the needed changes in new bitmap object and save the new bitmap object with original image file name.

Sample code that causes error

Dim oBitmap As Bitmap
oBitmap = New Bitmap("c:\\example.jpg")
Dim oGraphic As Graphics
oGraphic = Graphics.FromImage(oBitmap)
Dim oBrush As New SolidBrush(Color.Black)
Dim ofont As New Font("Arial", 8 )
oGraphic.DrawString("Some text to write", ofont, oBrush, 10, 10)
oBitmap.Save("c:\\example.jpg",ImageFormat.Jpeg)
oBitmap.Dispose()
oGraphic.Dispose()

Sample code with fix

Dim oBitmap As Bitmap
oBitmap = New Bitmap("c:\\example.jpg")
Dim oGraphic As Graphics
' Here create a new bitmap object of the same height and width of the image.
Dim bmpNew As Bitmap = New Bitmap(oBitmap.Width, oBitmap.Height)
oGraphic = Graphics.FromImage(bmpNew)
oGraphic.DrawImage(oBitmap, New Rectangle(0, 0, _
bmpNew.Width, bmpNew.Height), 0, 0, oBitmap.Width, _
oBitmap.Height, GraphicsUnit.Pixel)
' Release the lock on the image file. Of course,
' image from the image file is existing in Graphics object
oBitmap.Dispose()
oBitmap = bmpNew
 
Dim oBrush As New SolidBrush(Color.Black)
Dim ofont As New Font("Arial", 8 )
oGraphic.DrawString("Some text to write", ofont, oBrush, 10, 10)
oGraphic.Dispose()
ofont.Dispose()
oBrush.Dispose()
oBitmap.Save("c:\\example.jpg", ImageFormat.Jpeg)
oBitmap.Dispose()

Share

47 comments

Your email address will not be published. Required fields are marked *

  1. Sandeep Arora says:

    Thanks for posting this. This really helped!

  2. Lakhan Dewangan says:

    Thanks This really Helped my in fixing my problem.

  3. Somanth says:

    Thanks for solution
    This is perfect solution for my problem

  4. Sajan 9895227675 says:

    no need of all these…after dispose just add GC.Collect(); …tats all

  5. Tushar says:

    This is the final solution ( VB .NET). Hope this ends the problems of bitlocks and Image Saving.

    Public Shared Function CopyToStandaloneBitmap(ByRef InputImage As Image) As Image

    Dim memory As New MemoryStream()
    InputImage.Save(memory, Imaging.ImageFormat.Png)

    Return Image.FromStream(memory)
    End Function

    Public Shared Function InitializeStandaloneImageCopy(ByVal strPathFile As String) As Image

    If strPathFile Is Nothing Then Return Nothing
    If strPathFile.Length <= 0 Then Return Nothing
    If Not FileExists(strPathFile) Then Return Nothing

    Dim fs As New FileStream(strPathFile, FileMode.Open, FileAccess.Read)
    Dim img As Image = Image.FromStream(fs)
    Dim imgClone As Image = CopyToStandaloneBitmap(img)
    img.Dispose()
    img = Nothing
    fs.Close()
    fs = Nothing
    Return imgClone
    End Function

    *******************************
    Tushar 🙂