If you are trying to modify Bitmap, you may encounter the following GDI error which is very generic and does not provide any details. As the exception does not provide more details, it is very frustrating to figure out the root cause.
Bitmap.Save(): A generic error occurred in GDI+
2 Reasons Why This Generic GDI Error Occurred
GDI+ throws an error when it cannot save file. Following are 2 reasons why this error occurs.
- When you are initializing a Bitmap object from an image stored on hard disk, it creates a lock on the underlying image file. Due to the lock when you try to save and overwrite your modified bitmap, it throws this error.
- When you are saving a file, make sure the user has Write permission on the folder. This is important when you are getting this error on the Website because Website runs under restricted permissions.
3 Ways to Fix Generic GDI Error
There are three 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.
- Make sure the folder in which you are trying to save file is writable. In Web Application, the application pool or account which runs the Website must have write access to to the folder in order to save the file. For example if you are running Website under “DefaultAppPool”, you must give “IIS AppPool\DefaultAppPool” user “write” access to the folder.
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()
As shown in the above example, I am reading the bitmap, modifying it and overwriting it on the same file. As the process creates a lock on the underlying image, it will throw an exception.
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()
As shown in the above example, as soon as I create bitmap from an image, I am disposing the original bitmap. It releases the lock on the file. Hence I am able to overwrite the same file with updated bitmap.