我仍然不知道问题的真正原因,但我想它与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;
}
}
0
我有一个页面将html5画布数据发送到服务器端进程,该数据编码为base64 bmp图像(使用此算法http://devpro.it/code/216.html ),该服务器端进程将其转换为System.Drawing.Image对象,并对它进行一些操作。
在我的本地环境中,这很好,但是在我的ec2实例上,我收到以下错误:
我的代码如下所示:
这是一个带有样本b64string的文本文件,我正在使用该文本文件进行测试: https ://docs.google.com/leaf?id=0BzVLGmig1YZ3MTM0ODBiNjItNzk4Yi00MzI5LWI5ZWMtMzU1OThlNWEyMTU5 &hl=zh_CN
我也尝试了以下方法,并得到了相同的结果:
任何见解将不胜感激!