3

I want the pbGrade to be on top of pbItemtype (pb = picture box)

 pbItemtype.BackColor = Color.Transparent;

 // Change parent for overlay PictureBox...
 pbItemtype.Parent = pbGrade;

I already tried this but then the pbItemtype is not even appearing, also the 2 picture boxes change images (pbItemtype and pbGrade)

enter image description here

TaW
  • 53,122
  • 8
  • 69
  • 111
Nick Blaze
  • 33
  • 1
  • 6

3 Answers3

3

Best approach is to construct the image with overlays off-line in a Bitmap object, then assign that constructed image to the PictureBox for display.

For example like this:

int width = 100;
int height = 100;
Image image = new Bitmap(width, height);

using (var graphics = Graphics.FromImage(image))
{
    graphics.DrawImage(MyApplication.Properties.Resources.ImageItemX, new Rectangle(0, 0, width, height));
    graphics.DrawImage(MyApplication.Properties.Resources.ImageGradeZ, new Rectangle(0, 0, width, height));
}

myPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
myPictureBox.Image = image;

This assumes that ImageItemX and ImageGradeZ are images with a transparent background (for example pngs) imported as project resources under these names.


For example given these resources

Images defined in resources in project

the code will produce this:

Form showing combined images in PictureBox

NineBerry
  • 26,306
  • 3
  • 62
  • 93
  • Actually OP asks for 'nothing more' than looking through the PBox onto the one below. No problem, as long as no controls are suposed to __overlap__.. - Still: Drawing stuff on the fly is also and often a good option.. – TaW Feb 05 '17 at 17:07
  • yeah true TaW what I want is to look thought the other picrure box to another picture box – Nick Blaze Feb 05 '17 at 18:02
3

Well actually you can do that easily and in fact you already did.

The code works but you also need to correct the Location as the nested PB will keep it previous one and thus will probably be off to the bottom right, probably leaving the visible size of the new Parent.... :

pbItemtype.BackColor = Color.Transparent;

// Change parent for overlay PictureBox...
pbItemtype.Parent = pbGrade;
// Move it to the top left of the parent:
pbItemtype.Location = Point.Empty;  // or some other position..

Transparency works well when nesting controls. It doesn't work when overlapping them, though !

(Of course the code we see will not exchange the images!)

TaW
  • 53,122
  • 8
  • 69
  • 111
0

In General, if you have two picture boxes and you want one of them to overlap the other is as simple as making the overlapped picture be generated before the overlapping picture:

You can alter the order of these statements on the Form.Designer.cs file.

Change this:

this.Add(this.main_image);
this.Add(this.background_image);

To this:

this.Add(this.background_image);
this.Add(this.main_image);
HyLe909
  • 1
  • 3