我想捕獲屏幕,然後將其轉換爲Base64字符串。這是我的代碼:如何將位圖轉換爲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變爲空。任何想法我做錯了什麼?謝謝。
啊,woops。謝謝蒂姆。 :) –