CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2008
    Posts
    38

    Convert String to image

    Hi , i want to save the image from the database to an physical location like C:\one.JPG. the datatype of the column in the table is Image. i tried with working with MemoryStream. but i am not suceed. i am getting "Parameter not valid" when converting the stream to image "Image.fromStream(ms,true)".

    DataView dv = new DataView(ds.Tables[0], null, null, DataViewRowState.CurrentRows);

    byte[] imageData =(Bytes) dv[0][3];

    Image newImage;
    //Read image data into a memory stream
    using (MemoryStream ms = new MemoryStream(imageData, 0, imageData.Length))
    {
    ms.Write(imageData, 0, imageData.Length);

    //Set image variable value using memory stream.
    newImage = Image.FromStream(ms, true);
    }

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: Convert String to image

    Hi,

    Are you sure that you have the image data in "imageData"? Have you tried stepping through the application with the debugger?

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Convert String to image

    I would try to call ms.Seek(0) before constructing the image. And are you sure that the raw data is in proper format? I think (I'm not able to prove it now) that it must be a BMP.

    Also, keep in mind this statement from MSDN: "You must keep the stream open for the lifetime of the Image."
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured