0

I use this part of code to add the picture but it's half the cell...and still it's not working.

PictureBox pB = new PictureBox {
  Size = MaximumSize,
  Dock = DockStyle.Fill,
  BackgroundImageLayout = ImageLayout.Stretch
};

OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK) {
  string path = ofd.FileName;
  pB.Image = new Bitmap(path);
}

tableLayoutPanel1.Controls.Add(pB, x-1, y-1);
Control control = tableLayoutPanel1.GetControlFromPosition(x - 1, y - 1);
control.Dock = DockStyle.Fill;
control.BackgroundImageLayout = ImageLayout.Stretch;
TaW
  • 53,122
  • 8
  • 69
  • 111
  • Dock-filling a picturebox is not often correct. It only looks somewhat decently when the image is a texture with few details. But then you'd never use a TableLayoutPanel, you'd stitch the image together yourself. Consider that you need the SizeMode property the way it is already set. A FlowLayoutPanel could look marginally better. – Hans Passant May 28 '18 at 15:37

1 Answers1

1

You are confusing the two images a PictureBox can have:

  • The Image is the main one with all sorts of capabilties
  • The BackgroundImage is below it and can be use for just that: A background.

You want to set the layout for the Image; it is called SizeMode

PictureBox pB = new PictureBox
{
    Size = MaximumSize,
    Dock = DockStyle.Fill,
    SizeMode = PictureBoxSizeMode.StretchImage
};

Not sure where and how you set x and y but you should see the full image now, albeit more or less stretched.

And you don't need to set the SizeMode or Docking again at the end..

TaW
  • 53,122
  • 8
  • 69
  • 111