我有这段代码可以将 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;
}