[英]System.ArgumentException: 'Parameter is not valid.' in C# get image from SQL Server
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.我正在尝试从 SQL Server 获取图像,但位图对象显示无效参数。 the byte[] array seems to be causing this problem.
byte[] 数组似乎导致了这个问题。 Any ideas on how to fix it?
关于如何解决它的任何想法?
I have the newest SQL Server version as well.我也有最新的 SQL Server 版本。
根据 SQL 中的数据类型,您可以使用
byte[] image = dr[“image”]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.