Auto Ad Code

Showing posts with label image. Show all posts
Showing posts with label image. Show all posts

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:

Thursday, July 8, 2010

Image Update Issue (Cache)

Another common issue with developers is that sometimes they don't see an updated Image or may be flash file no matter they have changed the file at Server.
For example a developer is showing default.jpg as display image in any forum application but even when user changes his/her display image they still see the previous image as display image. Whats the problem here? though Image on server has been replaced successfully. Actually problem is with browser cache that browser has cached the image having name default.jpg for that path and whenever browser sees that path it picks image from cache instead of server.
Okay so whats the solution?
Solution is pretty much simple and straight which is to include some random string at the end of image path
for example the following image tag can create the cache issue
<­im­g s­rc=­"images­/­default­.­jpg­"­/­>
to over come the cache issue simply add any random string(starting from ? like query string) after .­jpg
i.e. following image tag will never create the image cache issue
<­im­g s­rc=­"images­/­default­.­jpg­?­6453426­"­/­>

To make this dynamic one can assign the source for image tag from Server side code and can make use of Random class to append some number after the jp­g. In this case you will have to add runat="server" and assign some id to image tag or you can simply use the asp:image tag and the ImageURL property. Let us suppose we are having imgTst and we are assigning src from serverside to avoid the cache issue.
Over code will be like this

VB

Randomize()
Dim mRnd As Random = Nothing
Dim mRandomNumber As Integer = mRnd.Next(0, 99999) 'This will generate any Random number from 0 to 99999
imgTst.src = "images/default.jpg?" & mRandomNumber

and if imgTst is asp:Image then our code will be like

Randomize()
Dim mRnd As Random = Nothing
Dim mRandomNumber As Integer = mRnd.Next(0, 99999) 'This will generate any Random number from 0 to 99999
imgTst.ImageUrl = "images/default.jpg?" & mRandomNumber

C# (C Sharp)

VBMath.Randomize(); //You can skip this
Random mRnd = null;
int mRandomNumber = mRnd.Next(0, 99999);
//This will generate any Random number from 0 to 99999
imgTst.src = "images/default.jpg?" + mRandomNumber;

and if imgTst is asp:Image then our code will be like

VBMath.Randomize();  //You can skip this
Random mRnd = null;
int mRandomNumber = mRnd.Next(0, 99999);
//This will generate any Random number from 0 to 99999
imgTst.ImageUrl = "images/default.jpg?" + mRandomNumber;


Same technique can be applied for flash files (having extension flv, fla) or fpr any file for which we wan to avoid the cache issue