0

I want to convert byte array to bitmap. I get this byte array from capture card. Rgb24 data include for this array. When i convert this array to bitmap object i got the "Parameter is not valid" error.

This is my byte array myByteArray{byte[921600]}

MemoryStream mStream = new MemoryStream(myByteArray);
Bitmap bi = new Bitmap(mStream );

and

using (MemoryStream mStream = new MemoryStream(myByteArray))
{
Bitmap bi = (Bitmap)System.Drawing.Image.FromStream(mStream );
}

and

using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(myByteArray, 0, myByteArray.Length);
mStream.Seek(0, SeekOrigin.Begin);

Bitmap bm = new Bitmap(mStream);
return bm;
}

Is this happen because of the size of the array? Can any one give a method to do this task? It will be greatly appreciated.

thank you

4
  • How do you get your array? Commented Nov 12, 2014 at 19:55
  • I suspect myByteArray is neither a System.Drawing.Image nor a System.Drawing.Bitmap. You have to convert it into one. Commented Nov 12, 2014 at 19:57
  • myByteArray{byte[921600]} isn't valid C#. What is your actual byte[] initialization? And there's no FromImage() method in System.Drawing.Image. If you show invalid code, you're only going to get useless answers. Commented Nov 12, 2014 at 19:59
  • I got this array through capture card callback function. This is the method i use for that. public static int ShowPIP(VIDEO_SAMPLE_INFO VideoInfo, IntPtr ptData, int lLength, long tRefTime, int lUserData) { byte[] bData = new byte[lLength]; Marshal.Copy(ptData, bData, 0, lLength); m_ShowPipForm.ShowPIP(VideoInfo, myByteArray); return 1; } Commented Nov 12, 2014 at 20:07

1 Answer 1

1

If your myByteArray is raw image data, this should work:

Bitmap bmp = null;
unsafe
{
    fixed (byte* p = myByteArray)
    {
        IntPtr unmanagedPointer = (IntPtr)p;

        // Deduced from your buffer size
        int width = 640;
        int height = 480;
        bmp = new Bitmap(width, height, width * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, unmanagedPointer);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much sir. This is work fine. But When display images the image is up side down.
i used bmp.RotateFlip(RotateFlipType.Rotate180FlipNone); and now i work fine. Thank You
I think you can use a negative stride to do that (3rd parameter of the constructor): -(width * 3)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.