System.ArgumentException:参数无效 [英] System.ArgumentException: Parameter is not valid
问题描述
我有发送HTML5画布的数据,连接codeD为Base64 BMP图像(使用该算法页面http://devpro.it/$c$c/216.html )到一个服务器端过程,将其转换成为System.Drawing.Image对象并执行一些操作就可以了。
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.
在我的本地环境中,这工作得很好,但在我的EC2实例我收到以下错误:
In my local environment, this works just fine, but on my ec2 instance I get the following error:
System.ArgumentException:参数无效。在
System.Drawing.Image.FromStream(流流,布尔
useEmbeddedColorManagement,布尔validateImageData)在
System.Drawing.Image.FromStream(流流,布尔
useEmbeddedColorManagement)
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)
我的code如下所示:
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);
}
...
下面是与我用来测试的样本b64string的文本文件:<一href=\"https://docs.google.com/leaf?id=0BzVLGmig1YZ3MTM0ODBiNjItNzk4Yi00MzI5LWI5ZWMtMzU1OThlNWEyMTU5&hl=en_US\">https://docs.google.com/leaf?id=0BzVLGmig1YZ3MTM0ODBiNjItNzk4Yi00MzI5LWI5ZWMtMzU1OThlNWEyMTU5&hl=en_US
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
我也试过以下,并有同样的结果:
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;
任何有识之士将大大AP preciated!
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. 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;
}
}
这篇关于System.ArgumentException:参数无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!