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]));
VB .Net
Dim ms As MemoryStream = New MemoryStream(File.ReadAllBytes( [IMAGE PATH]))
Reference:
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);
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:
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/a3a784f2-f901-4748-90e2-dbc7e755aeeb
No comments:
Post a Comment