如何将Graphics
对象转换为Base64
C#
字符串
Image img = Image.FromStream(imageStream, true);
Graphics graphics = Graphics.FromImage(img);
答案 0 :(得分:0)
您可以使用Graphics
中的MemoryStreams在Bitmaps
,Base64 Strings
和C#
之间进行转换
//
//Convert Base64 String to Image
byte[] imageBytes = Convert.FromBase64String(imageBase64String);
MemoryStream imageStream = new MemoryStream(imageBytes, 0, imageBytes.Length);
imageStream.Write(imageBytes, 0, imageBytes.Length);
Image img = Image.FromStream(imageStream, true);
//
// Convert Image to Graphics Object
Graphics graphics = Graphics.FromImage(img);
graphics.DrawRectangle(new Pen(Color.Red, 3), rectangle);
//
//Convert Graphics Object to Base64String
using (MemoryStream ms = new MemoryStream())
{
// Convert Image to byte[]
img.Save(ms, ImageFormat.Jpeg);
imageBytes = ms.ToArray();
// Convert byte[] to Base64 String
string base64String = Convert.ToBase64String(imageBytes);
return base64String;
}