When you try to modify a bitmap object, save or upload an image to a web server you may get the error message A generic error occurred in GDI+. The error can occur regardless of the application in use. This post provides the most applicable solutions affected users can apply to resolve the issue.
What is GDI+?
Windows GDI+ provides two-dimensional vector graphics, imaging, and typography, acting as an intermediate layer between applications and device drivers for rendering two-dimensional graphics, images, and text. GDI+ improves on Windows Graphics Device Interface (GDI) (the graphics device interface included with earlier versions of Windows) by adding new features and optimizing existing ones. In other words, GDI+ is a graphical subsystem of Windows that consists of an application programming interface (API) to display graphics and formatted text on both video display and printer.
A generic error occurred in GDI+
The A generic error occurred in the GDI+ error is very generic as the exception does not provide any details. However, if you’re faced with this issue, the suggestions provided below should help you resolve the issue depending on your case scenario or the task you performed.
1] Reboot the destination environment and then repeat the action you were performing. This worked for affected users deploying or moving application(s) from acceptance to prod.
2] This error can occur due to a ‘lock’ being set when you try to save and overwrite your modified bitmap because when you initialize a Bitmap object from an image stored on a hard disk, it creates a lock on the underlying image file. In other words, when either a Bitmap object or an Image object is constructed from a file, the file remains locked for the lifetime of the object. As a result, you cannot change an image and save it back to the same file where it originated. In this case, to resolve the issue, do either of the following:
- Instead of overwriting the file, save a new file with a different name than the original file.
- If you must overwrite the existing file, create a separate bitmap object from the existing bitmap object, then dispose of the old bitmap object which will release the lock on the image file. Now, you can make the needed changes in the new bitmap object and save the new bitmap object with the original image file name.
- Save to an ‘intermediary’ memory stream. The
bitmap.Save(...)code, although counter-intuitive, should work with minor variations to account for any particular method call.
Note that it’s only when the Bitmap object is disposed of, that the underlying lock on the file is removed – once the lock is removed, you may overwrite it.
Read: Unblock or delete locked or protected files on Windows using ThisIsMyFile
3] You need to dispose of your images because if you rely on the garbage collector to find these in time and release the unmanaged GDI resources, you’re likely to get an exception. In addition, make a note of the following points when writing your code:
ConvertToIt seems like a waste, just cast if you need to.
- If you need to combine paths, use
- Use string interpolation instead of plus’ing strings together.
- If you need to escape a character like a backslash, use Verbatim String Literal @”…”.
- Always favor using the
usingstatement which never forgets to dispose of an object, even if the code throws an exception.
Read: Best Programming Principles & Guidelines all Programmers should follow
4] No Write permission on the folder when you are saving a file especially if you get the error on the website because the website runs under restricted permissions. In Web Application, the application pool or account which runs the website must have write access to the folder to save the file – so make sure the folder in which you are trying to save the file is writable. For example, if you are running a website under
DefaultAppPool, you must assign
IIS AppPool\DefaultAppPool user write access to the folder.
Read: Website asking for permission to store data on computer for offline use
5] If you’re running Azure, and you get the A generic error occurred in GDI+ while opening a bitmap already only on a remote web server, and not on your local test machine, it could be that the GDI+ present on Azure IIS servers can’t handle new format BMPs generated by GIMP. If this is the case for you, to resolve the issue, do any of the following:
- Resave BMP with Paint.
- Use an uncompressed PNG instead.
- Save as 24bit BMP with GIMP. This method will produce bad quality.
Read: Paint 3D not saving; Does not display Export option
6] This error message is displayed if the path you pass to
Bitmap.Save() is invalid (folder doesn’t exist etc). Simply put, you’re writing the wrong path to save the image. In this case, make sure to correct the saving path and that your path does include the filename as well.
7] In the case the bitmap image file already existed in the system drive, and your app threw the error, do the following:
- Verify that the destination folder exists.
- Verify that there isn’t already a file with the same name in the destination folder.
- Check your folder’s permission where the image is saved on your local drive. To do this, right-click on the folder, then navigate to Properties > Security > Edit > Add – select Everyone and check Allow Full Control.
Read: How to take full Ownership of Files & Folders in Windows
I hope you find this post helpful!
How to solve A generic error occurred in GDI+ in C#?
If you get A generic error occurred in GDI+ in C#, it could be due to the bitmap image file you are trying to save already existing on your system drive. In this case, you can verify that the destination folder exists and that there isn’t already a file with the same name in the destination folder.
What is a GDI+ error?
GDI+ throws an error when it cannot save files usually due to two most common reasons. One of the reasons is, that when you are initializing a Bitmap object from an image stored on a 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.
How do I fix GDI windows?
To fix the GDI+ Windows error on your device, see if the following suggestions work:
- Run SFC scan in elevated command prompt.
- Run the Power Troubleshooter.
- Update the graphics card driver.
- Check for Windows Update.
- Start the computer in clean boot.
Read: Fix gdi32full.dll not found or missing error on Windows
What is GDI on my laptop?
GDI allows application programmers to display information on a screen or printer without having to be concerned about the details of a particular display device. The application programmer makes calls to methods provided by GDI+ classes and those methods in turn make the appropriate calls to specific device drivers. GDI+ insulates the application from the graphics hardware, and it is this insulation that allows developers to create device-independent applications.
Does Windows still use GDI?
With the introduction of Windows XP, GDI was deprecated in favor of its successor, the C++-based GDI+ subsystem. While GDI+ is included with Windows XP and later, the GDI+ dynamic library can also be shipped with an application and used on older versions of Windows.