c# Byte to Image ArgumentOutOfRangeException



我有一个小问题要将字节数组转换为位图。这是我的例外:

'system.argumentOutofRangeException'的未经处理的例外 发生在mscorlib.dll

我的代码:

public static System.Drawing.Bitmap ByteToImage(byte[] data)
{
    System.Drawing.Bitmap bmp;
    using (var ms = new MemoryStream(data))
    {
        bmp = new System.Drawing.Bitmap(ms);
    }
    return bmp;
}
Bitmap b = ByteToImage(editor1.system.Tiles[0].ImageData);
Form f = new Form();
f.BackgroundImage = b;
f.Show();

我需要在列表中加载一个串行的字节数组,然后在运行时转换为图像。

如果我保存位图

b.Save(@"C:test.png");

如果我尝试在运行时加载位图,它可以正常工作。

使用波纹码,它将解决您的问题。

    public static Image FormatImage(Image img, int outputWidth, int outputHeight)
    {
        Bitmap outputImage =null;
        Graphics graphics = null;
        try
        {
            outputImage = new Bitmap(outputWidth, outputHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
            graphics = Graphics.FromImage(outputImage);
            graphics.DrawImage(img, new Rectangle(0, 0, outputWidth, outputHeight),
            new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
            return outputImage;
        }
        catch (Exception ex)
        {                
           return img;
        }

}

相关内容

  • 没有找到相关文章

最新更新