0
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.

Dale K
  • 25,246
  • 15
  • 42
  • 71
  • what is the datatype of your `image` column in your sql table ? – Anand Sowmithiran Jun 04 '22 at 11:42
  • 2
    Debug your code line by line, and find out if this line ->> `long len = dr.GetBytes(0, 0, null, 0, 0);` returns anything correct, I doubt it. See the example use of `GetBytes` in the ms docs [page](https://learn.microsoft.com/en-us/dotnet/framework/data/adonet/retrieving-binary-data) – Anand Sowmithiran Jun 04 '22 at 11:45
  • You should add the actual version of SQL server, because it won't be the newest when the question is read in a year's time. – Dale K Jun 04 '22 at 23:34
  • Anand Sowmithiran : The dataype is image – Shahein Ockersz Jun 06 '22 at 09:44

1 Answers1

0

Depending in the datatype in SQL you can just use

byte[] image = dr[“image”]
Dale K
  • 25,246
  • 15
  • 42
  • 71
Sander Bos
  • 31
  • 1