Click here to Skip to main content
15,896,730 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi friends,

This is My code.

C#
sExtension = Path.GetExtension(Bigimg.FileName);
sFileName = Bigimg.FileName.Replace(Bigimg.FileName.ToString(), news.NewsId.ToString()) + sExtension;                        
string sPath1 = ConfigurationManager.AppSettings["BlockImagePath"].ToString() + sFileName;
if (!File.Exists(sPath1))
   File.Delete(sPath1);

Bigimg.PostedFile.SaveAs(sPath1);
System.Drawing.Image img = System.Drawing.Image.FromFile(sPath1);
img.Save(@sPath1, System.Drawing.Imaging.ImageFormat.Jpeg);

I using the above code to convert image format to JPEG.

in last line, img.save thrown exception "A Generic error occured in GDI+"

Please help me to resolve this.


Thanks.
Posted
Updated 14-Nov-13 2:10am
v2

I'm seeing some difficulties here, maybe you can explain them:

1) You're using a string variable here to replace itself with another string. The same result could be achieved much easier
C#
sFileName = Bigimg.FileName.Replace(Bigimg.FileName.ToString(), news.NewsId.ToString()) + sExtension;

//

sFileName = news.NewsId.ToString() + sExtension;


2) Something called "BlockImagePath" should be a string already. So no need to ToString() it.
And using Path.Combine[^] can help path creation robustness.
C#
string sPath1 = Path.Combine(
    ConfigurationManager.AppSettings["BlockImagePath"],
    sFileName
);


3) Delete something that isn't there?
C#
if (!File.Exists(sPath1))
   File.Delete(sPath1);
I suppose that you wanted that without the "!".

4) And the you're saving an image to disk, read it again, and re-write it another time.
I think you should provide some insight in the data type of PostedFile.
 
Share this answer
 
I saved the converted image in another name.now it works fine.

System.Drawing.Image img = System.Drawing.Image.FromFile(@sPath);
sPath1 = ConfigurationManager.AppSettings["BlockImagePath"].ToString();
replacedname = Path.GetFileNameWithoutExtension(sPath);
img.Save(@sPath1 + replacedname + "1.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

Thanks friends
 
Share this answer
 

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