1

我有这段代码可以将 SQL Server 2008 数据库中的图像显示到图片框

using (SqlCommand SqlCommand = new SqlCommand("Select * From Student Where StudentID = @a", myDatabaseConnection))
{
   SqlCommand.Parameters.AddWithValue("@a", SearchtextBox.Text);

   DataSet DS = new DataSet();

   SqlDataAdapter da = new SqlDataAdapter(SqlCommand);
   da.Fill(DS, "Images");

   dataGridView1.DataSource = DS.Tables[0];
   var imagesTable = DS.Tables["Images"];
   var imagesRows = imagesTable.Rows;
   var count = imagesRows.Count;

   if (count <= 0)
      return;

   var imageColumnValue = imagesRows[count - 1]["Image"];

   if (imageColumnValue == DBNull.Value)
      return;

   var data = (Byte[])imageColumnValue;

   using (var stream = new MemoryStream(data))
   {
       pictureBox1.Image = Image.FromStream(stream);
   }

但是即使我将其设置BackgroundImageLayout为拉伸,它显示的图像也不会拉伸

我也试过这个:

using (var stream = new MemoryStream(data))
{
    pictureBox1.Image = Image.FromStream(stream);
    pictureBox1.BackgroundImageLayout = ImageLayout.Stretch; 
}
4

1 回答 1

1

PictureBox.Image你的问题是你在and之间混淆了PictureBox.BackgroundImage,所以你的代码应该是以下之一:

using (var stream = new MemoryStream(data))
{
  pictureBox1.BackgroundImage = Image.FromStream(stream);
  pictureBox1.BackgroundImageLayout = ImageLayout.Stretch; 
}

//or

using (var stream = new MemoryStream(data))
{
  pictureBox1.Image = Image.FromStream(stream);
  pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; 
}
于 2013-08-04T03:41:51.610 回答