ari*_*ari 8 c# asp.net image-processing
我有一个页面发送html5画布数据,编码为base64 bmp图像(使用此算法http://devpro.it/code/216.html)到服务器端进程,将其转换为System.Drawing.Image对象和做一些操作.
在我的本地环境中,这工作正常,但在我的ec2实例上,我收到以下错误:
System.ArgumentException:参数无效.在System.Drawing.Image.FromStream(Stream stream,Boolean useEmbeddedColorManagement,Boolean validateImageData)中的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);
}
...
Run Code Online (Sandbox Code Playgroud)
这是一个包含我正在测试的样本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;
Run Code Online (Sandbox Code Playgroud)
任何见解将不胜感激!
我仍然不知道你的问题的真正原因,但我猜它与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;
}
}
Run Code Online (Sandbox Code Playgroud)