Error“ Parameter is not valid ” while converting Bytes into Image

前端 未结 6 903
心在旅途
心在旅途 2020-11-30 12:19

I am converting bytes into an image but I get an error

Parameter is not valid

I am pasting my code. Kindly check the code and

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-30 12:53

    After trying many things I found a way which has a little bit more control. In this example you can specify the pixel format and copy the bytes to a Bitmap.

    byte[] buffer = GetImageBytes();
    var bitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb);
    var bitmap_data = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);
    Marshal.Copy(buffer, 0, bitmap_data.Scan0, buffer.Length);
    bitmap.UnlockBits(bitmap_data);
    var result = bitmap as Image;
    

提交回复
热议问题