【发布时间】:2012-06-04 23:31:14
【问题描述】:
我正在尝试捕获屏幕,然后将其转换为 Base64 字符串。这是我的代码:
Rectangle bounds = Screen.GetBounds(Point.Empty);
Bitmap bitmap = new Bitmap(bounds.Width, bounds.Height);
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(Point.Empty, Point.Empty, bounds.Size);
}
// Convert the image to byte[]
System.IO.MemoryStream stream = new System.IO.MemoryStream();
bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Bmp);
byte[] imageBytes = stream.ToArray();
// Write the bytes (as a string) to the textbox
richTextBox1.Text = System.Text.Encoding.UTF8.GetString(imageBytes);
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
使用richTextBox调试,显示:
BM6~
所以由于某种原因字节不正确导致 base64String 变为空。知道我做错了什么吗?谢谢。
【问题讨论】: