0

I am new to C#, and making a form which creates buttons from database stored values. I already have image stored in the table,i don't know how to add image this way

Here is the code

SqlDataAdapter adapt = new SqlDataAdapter("select * from Items order by name", sc);
DataTable dt = new DataTable();
adapt.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
    Button btn = new Button();
    btn.Name = "btn" + dt.Rows[i][1];
    btn.Tag = dt.Rows[i][2];
    btn.BackgroundImage = dt.Rows[i][5];
    btn.Text = dt.Rows[i][1].ToString();
    btn.Font = new Font("Arial", 12f, FontStyle.Bold);

    btn.Height = 60;
    btn.Width = 120;
    flowLayoutPanel1.Controls.Add(btn);
}

this error occurs

Error 5 Cannot implicitly convert type 'object' to 'System.Drawing.Image'. An explicit conversion exists (are you missing a cast?)

  • 3
    So where's the `BackgroundImage` assigning part? Where's the image in your database? and more questions you haven't explained in your question. – Ghasem Dec 31 '15 at 07:34
  • that's what i m trying to find how to do? – Uzair Malik Dec 31 '15 at 07:39
  • Try switching to WPF instead of WinForms and introduce yourself to the concept of Data Binding. You will easily be able to do this with a ImageBrush or simply the Content of the Button that is no longer limited to Text in WPF. – eFloh Dec 31 '15 at 07:41
  • 2
    And just to mention: You should change your SQL code to it selects the columns you need explicitly instead of using the star. consider you add a column in between of the existing ones, your index values in code would change. Consider you add a very big data column (i.e. byte/char columns). You would always force the db engine to load the data you actually don't need in this function. – eFloh Dec 31 '15 at 07:43
  • I doubt if you've include the code which throws the exception in your question – Ghasem Dec 31 '15 at 07:45
  • btn.BackgroundImage = `dt.Rows[i][5];` this gives the error – Uzair Malik Dec 31 '15 at 07:45
  • Also what's the type of the column including images. byte maybe? – Ghasem Dec 31 '15 at 07:46

1 Answers1

1

I'd suggest turn the column including images to bytes and then do the following: (Or maybe it works with image as well. I'm not sure)

byte[] data = (byte[]) dt.Rows[i][5];
MemoryStream ms = new MemoryStream(data);
btn.BackgroundImage = Image.FromStream(ms);

Also this question's answers has multiple answers you could use

Community
  • 1
  • 1
Ghasem
  • 14,455
  • 21
  • 138
  • 171
  • @Alex: don't we need a using around the memory stream here? I think the Image.FromStream will copy the bytes and the stream should be disposed afterwards. – eFloh Dec 31 '15 at 08:15
  • 1
    @eFloh No we don't. disposing of the image will dispose the stream. You'll lose the image if you dispose `ms` – Ghasem Dec 31 '15 at 09:12
  • 1
    @Alex: You are right, source is here: https://msdn.microsoft.com/en-us/library/93z9ee4x.aspx#Anchor_2. Some posts even suggest you keep a reference to the stream yourself because the Image doesn't?! – eFloh Dec 31 '15 at 09:29