[英]Is There a Way to Convert System.Data.SqlTypes.SqlBinary of Datatable To Type System.Byte[]?
我在 C# 中有一個動態圖片框,它從一列數據表中獲取圖片,數據表結構是:
public DataTable Pictures = new DataTable();
Pictures.Columns.Add("id", typeof(bool));
Pictures.Columns.Add("filebytes", typeof(SqlBinary));
我有一個字節數組來在圖片框中顯示圖片:
byte[] ImageBinary;
以下代碼用於顯示圖像:
PictureBox a = new PictureBox();
a.Location = new Point(100, 90);
a.Size = new Size(25, 25);
a.SizeMode = PictureBoxSizeMode.StretchImage;
groupBox3.Controls.Add(a);
MemoryStream ms = null;
Image img = null;
Tools b = new Tools();
ImageBinary = b.CreateImageBinary(openFileDialog1.FileName);
ms = new MemoryStream(ImageBinary);
Pictures.Rows.Add(new object[] { i, ImageBinary });
img = Image.FromStream(ms);
a.Image = img;
ms.Close();
它從 Openfiledialog 獲取圖像
我想從數據表中為圖片框分配一個值我的問題是當我像這樣更改代碼時:
ImageBinary = (byte[])Pictures.Rows[sm][1]; //sm is a variable to specific a row of datatable
它讓我出現錯誤,例如:
無法將“system.data.sqltypes.sqlbinary”類型的對象轉換為“system.byte[]”類型
我很困惑請幫幫我
public static explicit operator byte[] (System.Data.SqlTypes.SqlBinary x);
我的建議是添加using System.Data.SqlTypes;
在聲明顯式運算符的.cs
文件的開頭導入。
檢查包含它的 dll 的文檔,以防編譯器找不到命名空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.