Image.FromStream() method returns Invalid Argument exception

前端 未结 10 2489
臣服心动
臣服心动 2020-11-29 09:18

I am capturing images from a smart camera imager and receiving the byte array from the camera through socket programming (.NET application is the client, camera is the serve

10条回答
  •  独厮守ぢ
    2020-11-29 10:11

    Try to use something similar to what is described here https://social.msdn.microsoft.com/Forums/vstudio/en-US/de9ee1c9-16d3-4422-a99f-e863041e4c1d/reading-raw-rgba-data-into-a-bitmap

    Image ImageFromRawBgraArray(
        byte[] arr, 
        int charWidth, int charHeight,
        int widthInChars, 
        PixelFormat pixelFormat)
    {
        var output = new Bitmap(width, height, pixelFormat);
        var rect = new Rectangle(0, 0, width, height);
        var bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat);
    
        // Row-by-row copy
        var arrRowLength = width * Image.GetPixelFormatSize(output.PixelFormat) / 8;
        var ptr = bmpData.Scan0;
        for (var i = 0; i < height; i++)
        {
            Marshal.Copy(arr, i * arrRowLength, ptr, arrRowLength);
            ptr += bmpData.Stride;
        }
    
        output.UnlockBits(bmpData);
        return output;
    }
    

提交回复
热议问题