System.ArgumentException:参数无效

编程入门 行业动态 更新时间:2024-10-13 22:27:20
本文介绍了System.ArgumentException:参数无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有发送HTML5画布的数据,连接codeD为Base64 BMP图像(使用该算法页面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 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的文本文件:docs.google/leaf?id=0BzVLGmig1YZ3MTM0ODBiNjItNzk4Yi00MzI5LWI5ZWMtMzU1OThlNWEyMTU5&hl=en_US

Here's a text file with a sample b64string that I'm using to test: docs.google/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:参数无效

本文发布于:2023-11-12 11:00:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581299.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:参数   System   ArgumentException

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!