locked
WriteableBitmap to byte[] to Drawing.Image - Error 'Parameter is not valid' RRS feed

  • Question

  • Hi folks,

    I want to use the powerful drawing library to resize my images without losing quality. So I wrote a service to do the resize which takes the following,

     
    public byte[] EncodeImage(double scaleFactor, byte[] bitmapData)
    {
    
                    MemoryStream _mStream = new MemoryStream(bitmapData, 0, bitmapData.Length);
                    _mStream.Write(bitmapData, 0, bitmapData.Length);
                     
                    Image image = Image.FromStream(_mStream);// Error here saying 'Parameter is not valid'
     

    In my silverlight app, I'm converting the WriteableBitmap to Byte[], as shown and calling the service method, 

     

                //WriteableBitmap to byte array  - Method 1               
                int[] p = bmp.Pixels;
                int len = p.Length * 4;
                byte[] bitmapData = new byte[len]; // ARGB                
                Buffer.BlockCopy(p, 0, bitmapData, 0, len);
                ////////Call Service to resize            
                _service.GetImageData(1, bitmapData);               
    
                // Method 2 
                long matrixSize = bmp.PixelWidth * bmp.PixelHeight;
    
                long byteSize = matrixSize * 4 + 4;
    
                byte[] retVal = new byte[byteSize];
    
                long bufferPos = 0;
    
                retVal[bufferPos++] = (byte)((bmp.PixelWidth / 256) & 0xff);
                retVal[bufferPos++] = (byte)((bmp.PixelWidth % 256) & 0xff);
                retVal[bufferPos++] = (byte)((bmp.PixelHeight / 256) & 0xff);
                retVal[bufferPos++] = (byte)((bmp.PixelHeight % 256) & 0xff);
    
                for (int matrixPos = 0; matrixPos < matrixSize; matrixPos++)
                {
                    retVal[bufferPos++] = (byte)((bmp.Pixels[matrixPos] >> 24) & 0xff);
                    retVal[bufferPos++] = (byte)((bmp.Pixels[matrixPos] >> 16) & 0xff);
                    retVal[bufferPos++] = (byte)((bmp.Pixels[matrixPos] >> 8) & 0xff);
                    retVal[bufferPos++] = (byte)((bmp.Pixels[matrixPos]) & 0xff);
                }
                _service.GetImageData(1, retVal);
    
                //Method 3
                byte[] retVal = WriteableBitmapExtensions.ToByteArray(bmp);
               _service.GetImageData(1, retVal);
    Whatever method I use to convert to Byte Array, the service always saying that 'Parameter is not valid'. Please Help.
    Thursday, April 1, 2010 12:27 AM

Answers

  •  Hi

     Last week I faced a similar Issue.

    To resolve this I would suggest first write out a Physical File and see whether it is being created correctly (Previewed as a Image by the File System). This will verify that the content and encoding are both correct.

    Let us see how it goes.

    Monday, April 5, 2010 12:30 PM

All replies

  •  Atleast some one please point me to some other way to pass the image data to the service...!!! Sad

    Thursday, April 1, 2010 2:23 AM
  • Hi,

       You can have a look the below blog post. It seems to settle your problem.

       http://kodierer.blogspot.com/2009/11/convert-encode-and-decode-silverlight.html

    Best Regards

    Friday, April 2, 2010 1:37 AM
  • Hi Min hong tang, That's the first method I tried, but no luck. I only see 'Parameter is not valid' at Image.FromStream statement.
    Friday, April 2, 2010 2:25 AM
  •  Hi

     Last week I faced a similar Issue.

    To resolve this I would suggest first write out a Physical File and see whether it is being created correctly (Previewed as a Image by the File System). This will verify that the content and encoding are both correct.

    Let us see how it goes.

    Monday, April 5, 2010 12:30 PM