c# - 系统参数异常 : Parameter is not valid

标签 c# asp.net image-processing

我有一个页面发送 html5 Canvas 数据,编码为 base64 bmp 图像(使用此算法 http://devpro.it/code/216.html )到服务器端进程,将其转换为 System.Drawing.Image 对象并对其执行一些操作。

在我的本地环境中,这工作得很好,但在我的 ec2 实例上我收到以下错误:

System.ArgumentException: Parameter is not valid. at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement)

我的代码如下所示:

System.Drawing.Image image = null;
string b64string = "...";
byte[] sf = Convert.FromBase64String(b64string );
using (MemoryStream s = new MemoryStream(sf, 0, sf.Length))
{
   image = System.Drawing.Image.FromStream(s, false);
}
...

这是一个文本文件,其中包含我用来测试的示例 b64string:https://docs.google.com/leaf?id=0BzVLGmig1YZ3MTM0ODBiNjItNzk4Yi00MzI5LWI5ZWMtMzU1OThlNWEyMTU5&hl=en_US

我也尝试了以下并得到了相同的结果:

System.Drawing.ImageConverter converter = new System.Drawing.ImageConverter();
image = converter.ConvertFrom(sf) as System.Drawing.Image;         

任何见解将不胜感激!

最佳答案

我仍然不知道您问题的真正原因,但我猜这与 Image 类无法识别的图像格式有关。稍微检查一下二进制数据后,我就可以形成你的图像了。我希望这会有所帮助。

Bitmap GetBitmap(byte[] buf)
{
    Int16 width = BitConverter.ToInt16(buf, 18);
    Int16 height = BitConverter.ToInt16(buf, 22);

    Bitmap bitmap = new Bitmap(width, height);

    int imageSize = width * height * 4;
    int headerSize = BitConverter.ToInt16(buf, 10);

    System.Diagnostics.Debug.Assert(imageSize == buf.Length - headerSize);

    int offset = headerSize;
    for (int y = 0; y < height; y++)
    {
        for (int x = 0; x < width; x++)
        {
            bitmap.SetPixel(x, height - y - 1, Color.FromArgb(buf[offset + 3], buf[offset], buf[offset + 1], buf[offset + 2]));
            offset += 4;
        }
    }
    return bitmap;
}

private void Form1_Load(object sender, EventArgs e)
{
    using (FileStream f = File.OpenRead("base64.txt"))
    {
        byte[] buf = Convert.FromBase64String(new StreamReader(f).ReadToEnd());

        Bitmap bmp = GetBitmap(buf);
        this.ClientSize = new Size(bmp.Width, bmp.Height);
        this.BackgroundImage = bmp;
    }
}

关于c# - 系统参数异常 : Parameter is not valid,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7533845/

相关文章:

c# - MEF 是否需要 .NET 4?

matlab - 如何在Matlab中提取前景图像

使用 jQuery UI Sortable 序列化方法的 ASP.NET Web 服务示例

c# - 如何从 aspx 下拉列表中获取选定的值?

php - PHP图像识别

image-processing - 使用miniflann时发生OpenCV错误

c# - 如何在完成之前访问 DirectoryInfo.EnumerateFiles

c# - Unity Netcode (MLAPI) 主机移动且客户端连接,但客户端不移动

c# - 更改 Y 轴的比例

c# - 将数组发送到 API 并获取空列表