locked
Parameter is not valid RRS feed

  • Question

  • User-389597101 posted

    Hi

     I have a stream of bytes of an image. I am converting it into the image

    Byte[] buffer = Encoding.Default.GetBytes(m_Stream); //   Sorry I can't paste the bytes of image because of its mammoth size)
    MemoryStream memorystream = new MemoryStream(buffer);
    Image img = Image.FromStream(memorystream);  

     

    But on last line I am getting error.

    "Parameter is not valid" 

    Can anyone please tell me the reason for this.

    Am I missing something.

    Kindly advice

    Regards

    Karan 

     

     

     


     

    Tuesday, September 16, 2008 9:31 AM

Answers

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 16, 2008 10:39 AM
  • User-1034726716 posted

    This may help:

    http://www.codeproject.com/KB/recipes/ImageConverter.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 16, 2008 10:44 AM
  • User-1734134863 posted

    Hi,

    Based on my research, we could use Bitmap and MemoryStream to create the image.

            SqlCommand com = new SqlCommand("select imgdata from table where id=1", con); //read the bytes data from database
            SqlDataReader dr = com.ExecuteReader();
            dr.Read();
            MemoryStream ms = new MemoryStream((Byte[])dr["imgdata"]);    
            Bitmap image = new Bitmap(ms); //create bitmap
            image.Save("test.jpg"); //save the image
            dr.Close();

     

     

    Please feel free to let me know if I’ve misunderstood anything.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 19, 2008 1:04 AM

All replies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 16, 2008 10:39 AM
  • User-1034726716 posted

    This may help:

    http://www.codeproject.com/KB/recipes/ImageConverter.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 16, 2008 10:44 AM
  • User-389597101 posted
    Hi I am using this code Byte[] buffer = UnicodeEncoding.Default.GetBytes(Stream); MemoryStream memorystream = new MemoryStream(buffer); Image img; img.Save(memorystream, System.Drawing.Imaging.ImageFormat.Jpeg); Byte[] ret = memorystream.ToArray(); FileStream fs = new FileStream(@"c:\karan.jpg", FileMode.Create, FileAccess.Write); fs.Write(ret, 0, ret.Length); fs.Flush(); fs.Close(); Here 3rd line of code Image img; Here I want to know how to initialize the Image img. Kindly advice Regards Karan
    Thursday, September 18, 2008 4:28 AM
  • User-1734134863 posted

    Hi,

    Based on my research, we could use Bitmap and MemoryStream to create the image.

            SqlCommand com = new SqlCommand("select imgdata from table where id=1", con); //read the bytes data from database
            SqlDataReader dr = com.ExecuteReader();
            dr.Read();
            MemoryStream ms = new MemoryStream((Byte[])dr["imgdata"]);    
            Bitmap image = new Bitmap(ms); //create bitmap
            image.Save("test.jpg"); //save the image
            dr.Close();

     

     

    Please feel free to let me know if I’ve misunderstood anything.

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 19, 2008 1:04 AM
  • User-1166471706 posted

    Hi,

    string strfn = Convert.ToString(DateTime.Now.ToFileTime());

    FileStream fs = new FileStream(strfn, FileMode.Create);

    byte[] btImage = assign from DB

    if (btImage !=null )

    {

    fs.Write(btImage, 0, btImage.Length);

    }

    // U can either convert as Image / Bitmap, based on ur need... In case if image type is needed then use Image Class instead of BitMap class.

    if (fs.Length > 0)

    {

    Bitmap bmp = new Bitmap(fs);

    bmp.Save(Server.MapPath("Images/Temp.jpg"));

    imgEmp.ImageUrl = "Images/Temp.jpg";

    }

    Friday, September 19, 2008 3:23 AM
  • User-578859172 posted

    It would seem the stream data is not a valid image. In any case why are you creating a memorystream from a byte array created from ANOTHER STREAM instead of just using m_Stream directly with Image.FromStream!?

    Try using Image.FromStream(m_Stream, false, false) to skip image data validation.

    Friday, September 19, 2008 6:21 AM
  • User1344168137 posted

    Same Problem i too Face.... while saving the Msagl Graph in .jpg file format... but i have save it as .EMF or .WMF  file format...

    what's the problem to save .jpg file format??


    Wednesday, January 20, 2010 2:14 AM
  • User-578859172 posted

    I don't know what your code looks like, but looking at the OP again it's clear what the problem with that was: It uses a text encoding to get the bytes, presumably reading the file using a StreamReader. Thus the bytes obtained aren't actually the raw image bytes, and no longer represent a valid image.

    If it is your code that saved the image you cannot open, try opening a .jpg saved with another program first, to get the reading right. If you read the bytes directly from a FileStream (and not a StreamReader) Image.FromStream should work fine.

    HTH

    Wednesday, January 20, 2010 6:04 AM