By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,361 Members | 2,168 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,361 IT Pros & Developers. It's quick & easy.

save image

P: n/a
Hello

I want to save a image to specified path as a jpg format.
But I'm getting some error messages.

Codes are shown below ;

path ="C:\\image.jpg";
FileStream fs = new FileStream(path,FileMode.Create,FileAccess.Write);
MemoryStream ms = new MemoryStream();
i.Save(ms,ImageFormat.Jpeg);
byte[] b = new byte[ms.Length];
ms.Write(b,0,b.Length);
fs.Write(b,0,b.Length);
fs.Close();

i is image object,it was loaded from anywhere(from path or database),
We suppose that size of i object is 223 kb and after codes mentioned above a
image is saved and it's size is 223kb but,picture in the saved file is not
appear.

i object is load with codes below;

String path;
Stream myStream;
OpenFileDialog openFileDialogPicture = new OpenFileDialog();
openFileDialogPicture.InitialDirectory = "C:\\temp\\image.jpg";
openFileDialogPicture.Filter = "picture files (*.jpg)|*.jpg";
openFileDialogPicture.FilterIndex = 2 ;
openFileDialogPicture.RestoreDirectory = true ;
if(openFileDialogPicture.ShowDialog() == DialogResult.OK)
{
if((myStream = openFileDialogPicture.OpenFile())!= null)
{
path = openFileDialogPicture.FileName;
Image i = Image.FromFile(path);
}
}

finally,How can I save this image object to specified path.

Thanks.
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Hi Brenny,

If you just want to copy this file to another location I would recommend using File.Copy instead of saving it again since that would just create a slightly lower quality JPG than you had to begin with.

In any case, to save an Image as JPG just use
Image.Save(path, ImageFormat.Jpeg);

Btw, when you say you get error message it helps if you tell us the exact errors (copy/paste).
--
Happy coding!
Morten Wennevik [C# MVP]
Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.