0

When I get a Bitmap image from SQL Server DB to display it in a PictureBox, I get an exception:

Parameter is not valid error

Here's my code:

public void FillHotMenu(int key, string connection, string logdir)
{
    try
    {
        SqlConnection conn = new SqlConnection(connectionstr);
        conn.Open();

        SqlCommand cmd = new SqlCommand("Pm_R_GetHotMenus", conn);
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@keyid", key);

        SqlDataReader read = cmd.ExecuteReader();
        flowLayoutPanel1.Controls.Clear();

        while (read.Read())
        {
            byte[] b = (byte[])(read["KeyBitmap"]);

            if (b.Length > 1)
            {
                PictureBox pic = new PictureBox();
                pic.Size = new Size(flowLayoutPanel1.Width / 4, flowLayoutPanel1.Height / 4);

                string s = read["KeyBitmap"].ToString();

                using (MemoryStream ms = new MemoryStream(b))
                {
                    Bitmap image = new Bitmap(ms);  // *** I'm getting error here
                    pic.Image = image;
                }

                pic.SizeMode = PictureBoxSizeMode.StretchImage;

                flowLayoutPanel1.Controls.Add(pic);
            }
            else if (read["KeyTextValue"].ToString() != "")
            {
                textvalue = "";
                PictureBox pic = new PictureBox();
                pic.Size = new Size(flowLayoutPanel1.Width / 4, flowLayoutPanel1.Height / 4);
                textvalue = read["KeyTextValue"].ToString();
                pic.Paint += new PaintEventHandler(pictureBox1_Paint);
                flowLayoutPanel1.Controls.Add(pic);
            }
        }

        conn.Close();
    }
    catch (Exception ex)
    {
        string logstr = ex.InnerException == null ? "" : ex.InnerException.Message;
        log.append("ERROR:" + ex.Message + "-->" + logstr, logdirectory);
        MessageBox.Show(ex.Message);
        Environment.Exit(0);
    }
}

How can I solve this problem?


Edit:

I've tried this code with no exceptions but nothing is shown in the PictureBox:

var ms = new MemoryStream(b);
ms.Seek(0, SeekOrigin.Begin);
ms.Position = 0;
byte[] imagebytes = ms.ToArray();


Bitmap bitmap = new Bitmap(pic.Width, pic.Height);
bitmap.Save(ms, ImageFormat.Png);

pic.Image = Image.FromStream(ms);

pic.SizeMode = PictureBoxSizeMode.StretchImage;
Sezer Erdogan
  • 167
  • 1
  • 9
  • 34
  • 1) Here, you should not dispose the `MemoryStream`, create it without the `using` statement. `var ms = new MemoryStream(b);`. 2) Reset the position of the `ms`, after the previous step, write: `ms.Position = 0;` or `ms.Seek(0, Origin.Begin)`. 3) Pass the `ms` to the Bitmap's constructor to create a new one. 4) Before doing this: `flowLayoutPanel1.Controls.Clear();` Make sure you `.Dispose();` the previously created `PictureBoxes` if any. 5) Before all that, make sure you are getting the correct data from the db. –  Jun 26 '20 at 16:47
  • hi @JQSOFT I try this code but I got same result. – Sezer Erdogan Jun 29 '20 at 08:22
  • 6) You are reading the bitmap data in `byte[] b = (byte[])(read["KeyBitmap"]);` then you read the same field again to get a string in `string s = read["KeyBitmap"].ToString();` which makes no sense. Remove the `string s = read["KeyBitmap"].ToString();` and try. –  Jun 29 '20 at 08:34
  • I m not using s only I put for debug. – Sezer Erdogan Jun 29 '20 at 09:17
  • Well, Try to save the image as file using: `((Bitmap)new ImageConverter().ConvertFrom(b)).Save("test.png", ImageFormat.Png);`, if you get an exception or it doesn't exist in the application directory, then `b` is the problem. –  Jun 29 '20 at 09:41
  • I tried but picture is coming empty. – Sezer Erdogan Jun 29 '20 at 13:58

0 Answers0