Auto Ad Code

Tuesday, December 20, 2011

a generic error occurred in gdi+ [SOLVED]

Hello folks, 
If you are reading this BLOG post then either you are facing the 
"The process cannot access the file because it is being used by another process" error 
or the
"a generic error occurred in gdi+" error
while manipulating images in ASP .Net (VB .Net/C#).
The issue may appear due to  new Bitmap([IMAGE PATH]) statement 


or by the Image.FromFile([IMAGE PATH]) statement in your code where you load the image in your code. This is because GDI+ keeps a lock on files from which an image was contructed.  To avoid the lock, construct the image from a MemorySteam. That is
C Sharp (C#)
MemoryStream ms = new MemoryStream(File.ReadAllBytes([IMAGE PATH]));
Image img = Image.FromStream(ms);

VB .Net
Dim ms As MemoryStream = New MemoryStream(File.ReadAllBytes( [IMAGE PATH]))
Dim img As Image = Image.FromStream(ms)

Hope this will solve your issue and you will not receive any of the above errors.

Also don't forget to close and dispose the MemoryStream and Image after the usage else you may get the "Out of memory" error. 




Reference:

No comments:

Post a Comment