如何将Graphics Object转换为Base64 String

时间:2018-01-25 11:43:36

标签: c#

如何将Graphics对象转换为Base64

中的C#字符串
Image img = Image.FromStream(imageStream, true);
Graphics graphics = Graphics.FromImage(img);

1 个答案:

答案 0 :(得分:0)

您可以使用Graphics中的MemoryStreams在BitmapsBase64 StringsC#之间进行转换

//
//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;
}