locked
Image to Byte[] --> sql --> Byte[] to Image RRS feed

  • Question

  • User705659023 posted

    Hi,

    I am trying to create a simple Add control where the user can select an image to upload. When the use clicks 'Submit' the image is to be converted to Byte[] and stored in SQL database column with type 'image'. I believe that this is being processed correctly, however when I try to retrieve the image back from the database I am unable to convert the data to the image.

    The code that I am using to convert the image to byte[] is


    Stream inStream = fileUpload.PostedFile.InputStream;
    
    System.Drawing.Image img = System.Drawing.Image.FromStream(inStream);
    
    MemoryStream ms = new MemoryStream();
    Byte[] imageBytes = null;
    
    EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, 85L);
                
    ImageCodecInfo jpegCodec = ImageCodecInfo.GetImageEncoders()[1];
    EncoderParameters encoderParams = new EncoderParameters(1);
    encoderParams.Param[0] = qualityParam;
    
    resizedImg.Save(ms, jpegCodec, encoderParams);
    
    imageBytes = ms.ToArray();


    When retrieving the image I use


    MemoryStream msRead = new MemoryStream((byte[])myReader["imgData"]);
                        
    System.Drawing.Image imgRead = System.Drawing.Image.FromStream(msRead);


    But when I run this code I get the following exception

    System.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream) at Add.Page_Load(Object sender, EventArgs e) in ...\Add.aspx.cs:line 43


    Am I doing something wrong?

    Monday, October 5, 2009 1:56 PM

Answers

All replies