Reputation: 9
private void GetData()
{
con.Open();
Cmd = new SqlCommand("Select Image,Name,Price from Product" ,con);
dr = Cmd.ExecuteReader();
while(dr.Read())
{
long len = dr.GetBytes(0, 0, null, 0, 0);
byte[] array = new byte[Convert.ToInt32(len)+ 1];
dr.GetBytes(0, 0, array, 0, Convert.ToInt32(len));
pic = new PictureBox();
pic.Width = 100;
pic.Height = 100;
pic.BackgroundImageLayout = ImageLayout.Zoom;
price = new Label();
price.Text = dr["price"].ToString();
price.Dock = DockStyle.Bottom;
price.TextAlign = ContentAlignment.MiddleCenter;
Names = new Label();
Names.Text = dr["name"].ToString();
Names.TextAlign = ContentAlignment.MiddleCenter;
MemoryStream ms = new MemoryStream(array);
Bitmap bitmap = new Bitmap(ms); // <--------(Error comes here)
pic.BackgroundImage = bitmap;
pic.Controls.Add(price);
pic.Controls.Add(Names);
flowLayoutPanel2.Controls.Add(pic);
}
con.Close();
}
I'm trying to get an image from SQL Server but the bitmap objects shows invalid parameters. the byte[] array seems to be causing this problem. Any ideas on how to fix it?
I have the newest SQL Server version as well.
Upvotes: 0
Views: 159
Reputation: 31
Depending in the datatype in SQL you can just use
byte[] image = dr[“image”]
Upvotes: 0