0

Hi i am new to windows form programming and having some trouble coding.

What i am trying to do is replace the current background of the windows form with another background that has been stored in my database. This is the image that has been stored

<code>enter image description here</code>

 cmd = new SqlCommand("select Background from Employee where EmployeeName='" + label2.Text + "'", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            MemoryStream ms = new MemoryStream((byte[])ds.Tables[0].Rows[0]["Background"]);
            pictureBox1.Image = new Bitmap(ms);

These are the codes that I tried using a picturebox as the background, But unable to do so with the background. Any form of help is much appreciated

EGS
  • 55
  • 1
  • 7
  • Possible duplicate of [Load Picturebox Image From Memory?](https://stackoverflow.com/questions/2540750/load-picturebox-image-from-memory) – MethodMan Dec 05 '17 at 02:49
  • @MethodMan Correct me i am wrong but I do not think it is a duplicate. I only use picturebox code as a reference to load background from the database – EGS Dec 05 '17 at 03:02
  • look at the answers on the link and you will see examples on how to use `Image.Load` method implement that if you can.. – MethodMan Dec 05 '17 at 03:04

1 Answers1

0

this.BackgroundImage = new Bitmap(ms);

Note: This will present the image using the BackgroundImageLayout property setting (eg. - None, Center, Stretch, etc.).

  • I received "Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'." as the database for certain record is null, may i know how do i go about it? – EGS Dec 05 '17 at 03:26
  • You could test for the value prior to using it. "if(ds.Tables[0].Rows[0]["Background"]!=null) { ... execute code }", otherwise skip. –  Dec 05 '17 at 03:41