简体   繁体   English

System.ArgumentException:参数无效

[英]System.ArgumentException: Parameter is not valid

I have a page that sends html5 canvas data, encoded as a base64 bmp image (using this algorithm http://devpro.it/code/216.html ) to a serverside process that converts it into a System.Drawing.Image object and does some operations on it. 我有一个页面发送html5画布数据,编码为base64 bmp图像(使用此算法http://devpro.it/code/216.html )到服务器端进程,将其转换为System.Drawing.Image对象和做一些操作。

In my local environment, this works just fine, but on my ec2 instance I get the following error: 在我的本地环境中,这工作正常,但在我的ec2实例上,我收到以下错误:

System.ArgumentException: Parameter is not valid. System.ArgumentException:参数无效。 at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData) at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement) 在System.Drawing.Image.FromStream(Stream stream,Boolean useEmbeddedColorManagement,Boolean validateImageData)中的System.Drawing.Image.FromStream(Stream stream,Boolean useEmbeddedColorManagement)

My code looks as follows: 我的代码如下:

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);
}
...

Here's a text file with a sample b64string that I'm using to test: https://docs.google.com/leaf?id=0BzVLGmig1YZ3MTM0ODBiNjItNzk4Yi00MzI5LWI5ZWMtMzU1OThlNWEyMTU5&hl=en_US 这是一个包含我正在测试的样本b64string的文本文件: https ://docs.google.com/leaf?id = 0BzVLGmig1YZ3MTM0ODBiNjItNzk4Yi00MzI5LWI5ZWMtMzU1OThlNWEyMTU5 &hl = en_US

I've also tried the following and had the same results: 我也尝试了以下内容并得到了相同的结果:

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

Any insight would be greatly appreciated! 任何见解将不胜感激!

I still don't know the real cause of your problem, but i guess it is related with a image format which Image class doesn't recognize. 我仍然不知道你的问题的真正原因,但我猜它与Image类无法识别的图像格式有关。 After inspecting the binary data a little bit, I could be able to form your image. 在检查二进制数据后,我可以形成您的图像。 I hope this helps. 我希望这有帮助。

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;
    }
}

The posted code seems correct. 发布的代码似乎是正确的。 I have tested it and it works fine. 我测试了它,它工作正常。

The exception "System.ArgumentException: Parameter is not valid." 异常“System.ArgumentException:Parameter无效”。 without any other hint (especially not the name of the parameter) is a wrapper for GDI+ (the underlying technology behind .NET Image class) standard InvalidParameter error, which does not tell use exactly what parameter is invalid. 没有任何其他提示(尤其不是参数的名称)是GDI +(.NET Image类背后的基础技术)标准InvalidParameter错误的包装器,它不会告诉使用什么参数无效。

So, following the FromStream code with .NET Reflector, we can see that the parameters used in GDI+ calls are essentially ... the input stream. 因此,遵循带有.NET Reflector的FromStream代码,我们可以看到GDI +调用中使用的参数本质上是......输入流。

So my guess is the input stream you provide is sometimes invalid as an image? 所以我的猜测是你提供的输入流有时像图像一样无效? You should save the failing input streams (using File.SaveAllBytes(sf) for example) for further investigation. 您应该保存失败的输入流(例如,使用File.SaveAllBytes(sf))进行进一步调查。

This could happen if sf contained invalid image data. 如果sf包含无效图像数据,则可能发生这种情况 Verify the validity of the data you're passing into the stream, and see if that fixes your issue. 验证您传入流中的数据的有效性,并查看是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DrawToBitmap - System.ArgumentException:参数无效 - DrawToBitmap - System.ArgumentException: Parameter is not valid 参数无效-System.argumentException-图像处理 - Parameter not valid - System.argumentexception - Image Handling System.ArgumentException:“参数无效”内存流到图像 - System.ArgumentException: "Parameter is not Valid" Memorystream to Image ImageResizer-转换PSD文件时出错-System.ArgumentException:参数无效 - ImageResizer - Error converting PSD file - System.ArgumentException: Parameter is not valid System.ArgumentException:参数无效。 GraphicsPath.AddString - System.ArgumentException: Parameter is not valid. GraphicsPath.AddString System.ArgumentException:参数无效。 在C#中 - System.ArgumentException: Parameter is not valid. in C# 计时器中graphics.DrawLine上的“ System.ArgumentException参数无效” - 'System.ArgumentException parameter is not valid' at graphics.DrawLine in a timer System.ArgumentException:&#39;参数无效。 (showDialog错误) - System.ArgumentException: 'Parameter is not valid.' (showDialog error) System.ArgumentException:“不是有效的默认值参数名称:defaultValue”(NControl) - System.ArgumentException: 'Not a valid default value Parameter name: defaultValue' (NControl) System.ArgumentException:DataGridViewComboBoxCell 值无效 - System.ArgumentException: DataGridViewComboBoxCell value is not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM