简体   繁体   English

System.ArgumentException:“参数无效。” 在 C# 中从 SQL Server 获取图像

[英]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.

相关问题 System.ArgumentException:参数无效。 在C#中 - System.ArgumentException: Parameter is not valid. in C# C#System.ArgumentException:参数无效。 在System.Drawing.Bitmap..ctor(Stream stream) - C# System.ArgumentException: Parameter is not valid. at System.Drawing.Bitmap..ctor(Stream stream) 给出“System.ArgumentException:&#39;参数无效。&#39; &quot; 当我从数据库中检索图像时 - Gives "System.ArgumentException: 'Parameter is not valid.' " when i retrieving image from database System.ArgumentException:参数无效。 GraphicsPath.AddString - System.ArgumentException: Parameter is not valid. GraphicsPath.AddString System.ArgumentException:&#39;参数无效。 (showDialog错误) - System.ArgumentException: 'Parameter is not valid.' (showDialog error) Windows窗体:“ System.ArgumentException:参数无效。”来自系统堆栈 - Windows Forms: “System.ArgumentException: Parameter is not valid.” coming from System stack 更新 Access 数据库后检索图像时出现问题(System.ArgumentException:'参数无效。') - Problem retrieving the image after updating the Access database (System.ArgumentException: 'Parameter is not valid.') 参数无效-System.argumentException-图像处理 - Parameter not valid - System.argumentexception - Image Handling System.ArgumentException:“参数无效”内存流到图像 - System.ArgumentException: "Parameter is not Valid" Memorystream to Image System.ArgumentException:参数无效 - System.ArgumentException: Parameter is not valid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM