Click here to Skip to main content
15,904,156 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello Guys

i am simply saving a image after Resizing
every thing is going good but only before this line
target.Save(Server.MapPath("~\\Site-Content\\Uploaded_Photo\\" + fn + "" + ext));
below is my code
C#
String ext = Path.GetExtension(fileAttach.PostedFile.FileName);
fn = DateTime.Now.ToString("yyyyMMddHHmmssffff");
 int width = 760;
 int height = 760;
 Stream stream = fileAttach.PostedFile.InputStream;
 Bitmap image = new Bitmap(stream);
 Bitmap target = new Bitmap(width, height);
 Graphics graphic = Graphics.FromImage(target);
 graphic.DrawImage(image, 0, 0, width, height);
// A generic error occurred in GDI+. //occuring here
 target.Save(Server.MapPath("~\\Site-Content\\Uploaded_Photo\\" + fn + "" + ext));
Posted
Comments
idenizeni 8-Oct-13 16:34pm    
Does your ASP.Net process (or the user if you are using impersonation) have permission to write to the directory?

see I have tested your code it's working, but it image and graphics need to dispose\

String ext = Path.GetExtension(fileAttach.PostedFile.FileName);
           string fn = DateTime.Now.ToString("yyyyMMddHHmmssffff");
           int width = 760;
           int height = 760;
           Stream stream = fileAttach.PostedFile.InputStream;
           Bitmap image = new Bitmap(stream);
           Bitmap target = new Bitmap(width, height);
           Graphics graphic = Graphics.FromImage(target);
           graphic.DrawImage(image, 0, 0, width, height);
           // A generic error occurred in GDI+. //occuring here
           graphic.Dispose();
           image.Dispose();
           //
           target.Save(Server.MapPath("~\\Site-Content\\Uploaded_Photo\\" + fn + "" + ext));
 
Share this answer
 
Comments
Shubh Agrahari 9-Oct-13 1:27am    
nice try thanks for reply
I guess this is one of the strange errors that exist for a very long time. Try to save the image to a stream, and write that stream to a file.

C#
using (MemoryStream outStream = new MemoryStream()){
    target.Save(outStream);
    outStream.Seek(0, SeekOrigin.Begin);
    using (FileStream fileStream = new FileStream("path\to\file",
           FileMode.Create, FileAccess.Write))
    {
        outStream.CopyTo(fileStream);
    }
}
 
Share this answer
 
v3
Comments
Shubh Agrahari 9-Oct-13 1:27am    
thanks its working....may i know that what the need of MS & FS here...?
Eduard Keilholz 9-Oct-13 2:49am    
To be honest, I don't know. There's some slight difference between the memory stream and the stream created to write to a file, which is why the original way you used doesn't work while is seems to be file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900