2011-08-12 17 views
2

私は画像ボックスを持っています。画像ボックス内の画像は静的な画像です。リソースフォルダからイメージをロードしています。私は画像ボックスから静止画像を読んでデータベースに格納することができません。ここに私のコードです。画像ボックスの画像をデータベースに保存

private void btnOk_Click(object sender, EventArgs e) 
    { 
     Image votingBackgroundImage = pictureBox5.Image; 
     Bitmap votingBackgroundBitmap = new Bitmap(votingBackgroundImage); 
     Image votingImage = (Image)votingBackgroundBitmap; 
      var maxheight = (votingImage.Height * 3) + 2; 
      var maxwidth = votingImage.Width * 2; 
      if (maxheight == 227 && maxwidth == 720) 
      { 

       System.IO.MemoryStream defaultImageStream = new System.IO.MemoryStream(); 
       Bitmap NewImage =new Bitmap(votingImage,new Size(720,227)); 
       Image b = (Image)NewImage; 
        b.Save(defaultImageStream, System.Drawing.Imaging.ImageFormat.Bmp); 
       defaultImageData = new byte[defaultImageStream.Length]; 
       } 
    } 

クエリは、私は、データベース内の画像を追加するために使用:

  String MyString1=string.format("insert into question_table(background_image) Values(@background_image)"); 
      com = new SqlCommand(MyString1, myConnection); 
       com.Parameters.Add("@background_image", SqlDbType.Image).Value = defaultImageData; 
       com.ExecuteNonQuery(); 

私は0 x000000として値を格納database.It SQLでこれをチェックすると...

答えて

1

あなただけbyte[]を作成しているが、あなたが実際にコンテンツ

をコピーしたことはありませんあなたはブロブすなわちバイトとしてそれを格納する必要が

System.IO.MemoryStream defaultImageStream = new System.IO.MemoryStream(); 
Bitmap NewImage =new Bitmap(votingImage,new Size(720,227)); 
Image b = (Image)NewImage; 
b.Save(defaultImageStream, System.Drawing.Imaging.ImageFormat.Bmp); 
defaultImageData = new byte[defaultImageStream.Length]; 
//assign byte array the content of image 
defaultImageData = defaultImageStream .ToArray(); 
0

ストリームのコンテンツを格納するためにバイトの配列を割り当てているように見えますが、コンテンツ自体はコピーしません。

おそらく中間配列は必要ありません。 MemoryStreamToArray()方法を使用してみてください:

b.Save(defaultImageStream, System.Drawing.Imaging.ImageFormat.Bmp); 
// ... 
com.Parameters.Add("@background_image", SqlDbType.Image).Value 
    = defaultImageStream.ToArray(); 
0

を試してみてくださいアレイ。