save image

B

brenny

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.
 
M

Morten Wennevik

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top