locked
Parameter is not valid error in MemoryStream RRS feed

  • Question

  • User-1360958501 posted

    G'day,

    I'm out of my depth but I know this routine works but when I apply it to my own ends I get this error.  Can someone please shed some light as to the likely cause.  The imageFile object exists and is a byte array.  

    <!--StartFragment --><code> 
    Exception Details: System.ArgumentException: Parameter is not valid.

    Source Error:

    Line 46:     Private Function ResizeImageFile(ByVal imageFile() As Byte, ByVal targetSize As Integer) As Byte()
    Line 47:         Dim original As System.Drawing.Image
    Line 48:         original = System.Drawing.Image.FromStream(New MemoryStream(imageFile))
    Line 49:         Dim targetH As Integer
    Line 50:         Dim targetW As Integer

    Stack Trace:

    [ArgumentException: Parameter is not valid.]
       System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) +339
       System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement) +33
       System.Drawing.Image.FromStream(Stream stream) +27

    </code>
    Saturday, May 21, 2005 7:29 PM

All replies

  • User166373706 posted
    Ever get a resolution to this issue? I'm having the same problem. I get it when I try to pull an image out of sql server if that image was pushed into sql server via access (linked table).
    Tuesday, February 14, 2006 12:24 PM
  • User-1372641848 posted

    if imageFile is your byte array then following will work.

    Dim ms As New MemoryStream()
    ms.Write(imageFile, 0, imageFile.Length)

    Dim original As New System.Drawing.Bitmap(ms)

    Wednesday, February 15, 2006 7:31 AM
  • User1982720092 posted

    its not working, i am tires and still have error.

    insert code is:

    SqlConnection con = dbcls.openConncection();

    SqlCommand myCommand = new SqlCommand(query, con);

    myCommand.Parameters.Add("@ImageData", SqlDbType.Image, 16, "picture");

    myCommand.Parameters["@ImageData"].Value = byt;

    int i = myCommand.ExecuteNonQuery();

    insert code is working. but retrieve code not working:

    byte[] imageData = (byte[])row["picture"];

    System.Drawing.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 = System.Drawing.Bitmap.FromStream(ms);

    }

    at last line i get exception "Parameter is not valid". i tried

    newImage = System.Drawing.Image.FromStream(ms,true);

    as well but same problem.

    Please please help. i am stuck

    Thursday, May 15, 2008 12:17 PM
  • User1959021347 posted

     jst check it out with the following solution

    it works for me


      Bitmap btm = new Bitmap(rr.ToString());//give the path from where u want the exact image
            Bitmap btm_new = new Bitmap(btm, btm.Width, btm.Height);
            btm_new.SetResolution(btm.HorizontalResolution, btm.VerticalResolution);
            Graphics g = Graphics.FromImage(btm_new);
            btm.Dispose();
            btm = btm_new;

    btm.Save("");//jst give the path where u want to save that image
         
           
            g.Dispose();
            btm.Dispose();

     

     

    Thursday, May 22, 2008 6:03 AM
  • User18311010 posted

    getting nearly the exact same thing - did you ever find a solution?

    Monday, July 28, 2008 5:10 PM