1

I followed instructions from this page to insert controls to tableLayoutPanel. http://goo.gl/SVKf8D

I am using this code to try to add a dynamic picturebox and set it's source:

    tableLayoutPanel1.Controls.Add(new PictureBox() { Image.FromFile(TableLayoutPanelTool.Properties.Resources.BaldGuyImage)}, 0, 0);

I cannot insert image source to picturebox this way.

What I need: A way to set the image to the Picturebox that I have added dynamically to the TableLayoutview

note:it is ok to give source directly.Something like source= C:/Projects/bin/debug

Ahmed Faizan
  • 446
  • 5
  • 12
  • 22

1 Answers1

2

If you have added the image into project's resources file, you can access them using Properties.Resources class. In this case, you can directly assign the resource into picturebox.image property:

tableLayoutPanel1.Controls.Add(new PictureBox() { Image = TableLayoutPanelTool.Properties.Resources.BaldGuyImage}, 0, 0);

You could read more here and also from here

Community
  • 1
  • 1
currarpickt
  • 2,290
  • 4
  • 24
  • 39
  • Thanks for the reply I doenst work. It says "Cannot convert from System.Drawing.Bitmap to string" I just tried tableLayoutPanel1.Controls.Add(new PictureBox() { Image = BitConverter.ToString(Image.FromFile(TableLayoutPanelTool.Properties.Resources.BaldGuyImage)) }, 0, 0); I doesn't work either. It is also ok to give source directly as C:/Projects/bin/debug – Ahmed Faizan Aug 12 '15 at 06:20
  • Image.FromFile expectes a filename ie a string. You already have the Iamge in the resources so you can probably just use: Image = (Image)(Table..... – TaW Aug 12 '15 at 06:24
  • @AhmedFaizan I have update the code. If you loaded the image from resources, just use Image = resource – currarpickt Aug 12 '15 at 06:37
  • Thanks.If you could provide the direct source method it would be cool as well. – Ahmed Faizan Aug 12 '15 at 08:20
  • 1
    @AhmedFaizan please see the edited post, I have added two links as reference – currarpickt Aug 12 '15 at 08:47