2017-03-17 56 views
0

我有這行代碼創建一個QR碼,現在我想將它保存爲png這樣我就可以把它變成低於IMG tag..see:ASP.NET位圖的PNG圖像

using (Bitmap bitMap = qrCode.GetGraphic(20)) 
{  
    using (MemoryStream ms = new MemoryStream()) 
    { 
     bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png); 
    }  
} 

message = "<img src='" + What goes here? + "' />"; 

接下來我該做什麼?我不想將它保存爲一個base64

+0

也許我在這裏的答案可以幫助。問題幾乎完全相同。 http://stackoverflow.com/questions/42815553/pass-generated-barcode-to-an-html-tag/42817577#42817577 – VDWWD

回答

1

你可以做兩件事之一。

  1. 將其保存到一個文件,是通過網站獲取
  2. 將其轉換爲Base64編碼字符串,並使用內嵌的數據圖像

方法1:

string publicPath = @"somewebaccessiblepath\file.png" 
    using(MemoryStream ms = new MemoryStream()) 
    { 
     // image creation and conversion here 

     using (FileStream file = new FileStream(publicPath , FileMode.Create, FileAccess.Write)) 
     { 
      ms.WriteTo(file); 
      file.Close(); 
      ms.Close(); 
     } 
    } 

    message = "<img src=\"" + publicPath + "\" />"; 

方法2:

byte[] byteArr = ms.ToArray(); 
    string b64Txt = Convert.ToBase64String(byteArr); 

    string hrefText = "data:image/png;base64," + b64Txt; 

    message = "<img src=\"" + hrefText + "\" />";