1
我在我的aspx頁面有一個syncfusion chartwebcontrol,我需要將圖表保存到二進制列中的數據庫中。我不確定如何將syncfuion控件的圖像轉換爲字節形式。如何獲取syncfusion chartwebcontrol的二進制數據以保存到數據庫?
我在我的aspx頁面有一個syncfusion chartwebcontrol,我需要將圖表保存到二進制列中的數據庫中。我不確定如何將syncfuion控件的圖像轉換爲字節形式。如何獲取syncfusion chartwebcontrol的二進制數據以保存到數據庫?
您可以通過使用SaveImage
方法保存ChartWebControl
爲Image
,那麼這個圖像轉換爲使用Stream
概念二進制數據和數據庫保存這個二進制數據。您可以使用File Stream
類將圖表圖像轉換爲二進制。
請參考下面的代碼片段
[C#]
this.ChartWebControl1.SaveImage(Server.MapPath("Chart.png"));
byte[] buffer = ImageToBinary(Server.MapPath("Chart.png"));
//Insert the above buffer data to db for chart image binary data
--------------------------------
public static byte[] ImageToBinary(string imagePath)
{
FileStream fileStream = new FileStream(imagePath, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);
fileStream.Close();
return buffer;
}
可以二進制數據轉換回圖像,請參考下面的代碼片段。
[C#]
public static Image BinaryToImage(System.Data.Linq.Binary binaryData)
{
if (binaryData == null) return null;
byte[] buffer = binaryData.ToArray();
MemoryStream memStream = new MemoryStream();
memStream.Write(buffer, 0, buffer.Length);
return Image.FromStream(memStream);
}