save image

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • brenny

    save image

    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.Creat e,FileAccess.Wr ite);
    MemoryStream ms = new MemoryStream();
    i.Save(ms,Image Format.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 openFileDialogP icture = new OpenFileDialog( );
    openFileDialogP icture.InitialD irectory = "C:\\temp\\imag e.jpg";
    openFileDialogP icture.Filter = "picture files (*.jpg)|*.jpg";
    openFileDialogP icture.FilterIn dex = 2 ;
    openFileDialogP icture.RestoreD irectory = true ;
    if(openFileDial ogPicture.ShowD ialog() == DialogResult.OK )
    {
    if((myStream = openFileDialogP icture.OpenFile ())!= null)
    {
    path = openFileDialogP icture.FileName ;
    Image i = Image.FromFile( path);
    }
    }

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

    Thanks.


  • Morten Wennevik

    #2
    Re: save image

    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.Jpe g);

    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]

    Comment

    Working...