locked
byte image convert to image , parameter is not valid error RRS feed

  • Question

  • Hi all;

    I have problem. I want to make byte array image convert to image . I get "parameter is not valid" error.

    read byte image data format :monoraw

    my code 


    mєѕυт ριşкιη | mesutpiskin.com


    • Edited by MesutPişkin Thursday, December 3, 2015 1:12 PM image format
    Thursday, December 3, 2015 12:21 PM

Answers

  • Thank you everyone.

    I found the solution.

      private static Image CreateBitmap8bits(byte[] pRawBuffer, int nWidth, int nHeight)
            {
                if (pRawBuffer == null || pRawBuffer.Length == 0)
                {
                    return null;
                }
                Bitmap bmpBitmap = new Bitmap(nWidth, nHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                ColorPalette pal = bmpBitmap.Palette;
                //Create custome palette
                for (int i = 0; i <= 255; i++)
                {
                    pal.Entries[i] = Color.FromArgb(i, i, i);
                }
                bmpBitmap.Palette = pal;
                BitmapData bmpData;
                bmpData = bmpBitmap.LockBits(new Rectangle(0, 0, bmpBitmap.Width, bmpBitmap.Height), ImageLockMode.WriteOnly, bmpBitmap.PixelFormat);
                Marshal.Copy(pRawBuffer, 0, bmpData.Scan0, pRawBuffer.Length);
                bmpBitmap.UnlockBits(bmpData);
                return bmpBitmap;
            }


    mєѕυт ριşкιη | mesutpiskin.com

    • Marked as answer by MesutPişkin Friday, December 4, 2015 2:26 PM
    Friday, December 4, 2015 2:26 PM

All replies

  • Probably GetStreamImage does not put valid bytes to images list. Show some details.

    Thursday, December 3, 2015 1:33 PM
  • The error could be because "The stream does not have a valid image format"

    However can you try the below block of code for creating the image, if it didn't work please share the code building the images List:

    using(MemoryStream memStream = new MemoryStream(images[0].ImageData))
    {
    	Image resim = Image.FromStream(ms);
    
    }

    Fouad Roumieh

    Thursday, December 3, 2015 1:47 PM
  • @Fouad Roumieh Thank you so much for your answer but still error

    error details

    An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
    Additional information: parameter is not valid.

    It may be the monoraw image format :(


    mєѕυт ριşкιη | mesutpiskin.com


    Thursday, December 3, 2015 1:56 PM
  • thaks for answer. @Viorel_

    GetStreamImage is work. GetStreamImage 3 party api.


    mєѕυт ριşкιη | mesutpiskin.com


    • Marked as answer by MesutPişkin Thursday, December 3, 2015 2:04 PM
    • Unmarked as answer by MesutPişkin Thursday, December 3, 2015 2:04 PM
    • Edited by MesutPişkin Thursday, December 3, 2015 2:05 PM
    Thursday, December 3, 2015 1:59 PM
  • So is it sorted now?

    Check the below thread to verify the image format:

    http://stackoverflow.com/questions/5209506/how-can-i-know-what-image-format-i-get-from-a-stream


    Fouad Roumieh

    Thursday, December 3, 2015 4:08 PM
  • Hi meSut PİŞKİN,

    I think maybe the error is related to the variable “images[0].ImageData”. The following code you could have a look and you could use this method to directly load an Image file to check whether the “images[0].ImageData” is right or not. Here, I use a JPG file as a loaded file. For your issue,you should make sure the Image file is the same with the one that “images[0].ImageData” points to.

                Image img = Image.FromFile(@"D:\abc.jpg");
                byte[] arr;
                using (MemoryStream ms = new MemoryStream())
                {
                    //Here, you should change the ImageFormat depending on the format of your actually loaded file
                    img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                    arr = ms.ToArray();
                }
                using (MemoryStream ms = new MemoryStream(arr,0,arr.Length))
                {
                    ms.Position = 0;
                    Image resim;
                    resim = Image.FromStream(ms,true);
                }
    

    Best Regards,

    April WX.

    Friday, December 4, 2015 8:22 AM
  • Hi meSut PİŞKİN,

    I think maybe the error is related to the variable “images[0].ImageData”.

    ...


    Hi,

    I agree. Maybe the image is already in a disposed state. Then it would not be null, but every Property etc would throw this error. So my question is, in the GetStreamImage method, do you dispose the image somewhere? Maybe add a clone of it to the list...

    Regards,

      Thorsten

    Friday, December 4, 2015 9:20 AM
  • In order to check the returned image, perform this experiment: write the bytes to a file using File.WriteAllBytes(“myfile.xxx”, images[0].ImageData), using an appropriate file extension, such as “.bmp”, depending on image format. Then open the file in a graphical editor, such as Paint or Visual Studio.

    Friday, December 4, 2015 9:38 AM
  • Thank you everyone.

    I found the solution.

      private static Image CreateBitmap8bits(byte[] pRawBuffer, int nWidth, int nHeight)
            {
                if (pRawBuffer == null || pRawBuffer.Length == 0)
                {
                    return null;
                }
                Bitmap bmpBitmap = new Bitmap(nWidth, nHeight, System.Drawing.Imaging.PixelFormat.Format8bppIndexed);
                ColorPalette pal = bmpBitmap.Palette;
                //Create custome palette
                for (int i = 0; i <= 255; i++)
                {
                    pal.Entries[i] = Color.FromArgb(i, i, i);
                }
                bmpBitmap.Palette = pal;
                BitmapData bmpData;
                bmpData = bmpBitmap.LockBits(new Rectangle(0, 0, bmpBitmap.Width, bmpBitmap.Height), ImageLockMode.WriteOnly, bmpBitmap.PixelFormat);
                Marshal.Copy(pRawBuffer, 0, bmpData.Scan0, pRawBuffer.Length);
                bmpBitmap.UnlockBits(bmpData);
                return bmpBitmap;
            }


    mєѕυт ριşкιη | mesutpiskin.com

    • Marked as answer by MesutPişkin Friday, December 4, 2015 2:26 PM
    Friday, December 4, 2015 2:26 PM