Arh Hokagi Arh Hokagi - 1 year ago 132
C# Question

Scrollable backgroundimage of a tablelayout in winforms

I have a tablelayout with 10x10 (columns x rows), I can drop objects into those cells.

I want to add an image (as a background) to that tablelayout that can be scrollable and covers all the table cells, the user can upload any image size.

drag/drop is working, my probleme is: I can't scroll the tablebackgound image eventho i set

AutoScroll
to true.
any idea how to fix that

Update:
now i can scroll but the image is displayed in wrong way

private void addImage()
{
Image img = Image.FromFile(@"C:\Users\c1\Desktop\img_Clean.png");
if (tableLayoutDropZone.BackgroundImage != null) tableLayoutDropZone.BackgroundImage.Dispose();
tableLayoutDropZone.BackgroundImage = img;
tableLayoutDropZone.AutoScrollPosition = Point.Empty;
tableLayoutDropZone.AutoScrollMinSize = new Size(img.Width, img.Height);

}

TaW TaW
Answer Source

You need to do three things:

  • Set yourTLP.AutoScroll = true and, of course the BackgroundImageLayout=None

  • Set a suitable AutoScrollMinSize; It should have the same size as the image you use as BackgroundImage, i.e. when the image changes you need to reset the AutoScrollMinSize

  • you need to code the Paint event to include this line: e.Graphics.DrawImage(yourTLP.BackgroundImage, yourTLP.AutoScrollPosition);

enter image description here

Here is my code to load a new image:

Image img = Image.FromFile(someimagepath);
if (yourTLP.BackgroundImage != null) yourTLP.BackgroundImage.Dispose();
yourTLP.BackgroundImage = img;
yourTLP.AutoScrollPosition = Point.Empty;
yourTLP.AutoScrollMinSize = new Size(img.Width, img.Height);

Note that as the TLP scrolls the controls it holds are scrolled as well!

If you want them to stay fixed you can do this:

  • Remove all above settings and code
  • Create a Panel with the very same settings and code
  • Set the BackColor of your TLP to Transparent
  • Nest it in the Panel

You may want to turn on DoubleBuffering for smoother scrolling..

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download