簡體   English   中英

如何將圖像(.png)轉換為base64字符串,反之亦然,並將其轉換為指定位置

[英]How to Convert image (.png) to base64 string, vice a versa and strore it to a specified location

我正在嘗試將圖像(png)存儲到Windows 8應用程序中的sqlite數據庫,我發現它可以通過將其轉換為base64字符串並將字符串存儲到數據庫來完成。 稍后在應用程序中我想將該base64字符串轉換為png圖像並將其存儲到指定位置。 問題是我不知道如何將圖像轉換為base64和base64圖像並將其存儲到c#windows 8 app中的指定位置。 任何幫助,將不勝感激。

public string ImageToBase64(Image image, 
  System.Drawing.Imaging.ImageFormat format)
{
  using (MemoryStream ms = new MemoryStream())
  {
    // Convert Image to byte[]
    image.Save(ms, format);
    byte[] imageBytes = ms.ToArray();

    // Convert byte[] to Base64 String
    return Convert.ToBase64String(imageBytes);
  }
}

public Image Base64ToImage(string base64String)
{
    // Convert Base64 String to byte[]
    byte[] imageBytes = Convert.FromBase64String(base64String);
    using (var ms = new MemoryStream(imageBytes, 0, imageBytes.Length))
    {
        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        return Image.FromStream(ms, true);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM