Home > ASP .Net > Bitmap.save(): A generic error occurred in GDI+

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

Try the following code

	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()

You will get the above mentioned error:A generic error occurred in GDI+. This problem occurs because until the bitmap object is disposed, it creates a lock on the underlying image file. So you can save the newly generated file with different name but not overwrite the file because of lock. Now suppose you want to overwrite the file then create another bitmap from old bitmap. dispose the object of old bitmap, process new bitmap object and save the new bitmap object with original file name. The above chunk of code should be written in the following way.

	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()
Categories: ASP .Net Tags:
  1. September 3rd, 2010 at 16:10 | #1

    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 :-)

  2. September 22nd, 2010 at 11:54 | #2
  3. September 22nd, 2010 at 11:55 | #3
  4. Sajan 9895227675
    January 6th, 2011 at 05:33 | #4

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

  5. Somanth
    June 18th, 2011 at 15:26 | #5

    Thanks for solution
    This is perfect solution for my problem

Comment pages
1 ... 3 4 5 19
  1. No trackbacks yet.