2016-04-11 17 views
0

私は、DataGridに配置された「DataGridViewImageColumn」のColumnTypeを使用して、クラスを渡して自分のクラスクエリに画像を保存しようとしています。DataGridViewImageからデータベースに画像を保存する

ここに私のコードです。

// Class name 
public byte[] Image; 

// Getting the value image from datagridview 
int i; 
for(...){ 
byte[] image = (byte[])dgv.Rows[i].Cells[7].Value; 
MemoryStream ms = new MemoryStream(image); 
belsalesquote.Image = Image.FromStream(ms); 
} 

エラーリストはは「暗黙タイプ 『System.Drawing.Image』が 『[]バイト』私のデータベース内の

マイ列タイプが 『イメージ』に等しい。

答えて

3
に変換できませんと言います

あなたが

//byte[] image = (byte[])dgv.Rows[i].Cells[7].Value; 

    Image image = (Image)dgv.Rows[i].Cells[7].Value; 

    using (MemoryStream m = new MemoryStream()) 
    { 
     image.Save(m, image.RawFormat); 
     byte[] imageBytes = m.ToArray(); 

     // Convert byte[] to Base64 String 
     string base64String = Convert.ToBase64String(imageBytes); 
    } 

をバイトを直接コンバート画像をカントつまり、あなたのデシベルに[base64String]保存することができます

+0

[base64String]はどういう意味ですか?私は列の型をイメージから[base64String]に変更します。 :) –

+0

文字列base64String = Convert.ToBase64String(imageBytes); 変数の名前は何でもいいので、 とすることができますので、データベースの任意のNVARCHAR型 –

関連する問題