0

How can I load 2 Images in one PictureBox ?

Here is an example:

http://postimg.org/image/l78kth897/

Thank you so much.

zak zak
  • 109
  • 2
  • 3
  • 10
  • 1
    Try 2 overlapping pictureboxes http://stackoverflow.com/questions/4623165/make-overlapping-picturebox-transparent-in-c-net – Nathan Mar 26 '14 at 16:07
  • 1
    Overlay two picture boxes. PictureBox can only have the one source, so to get two images in it, you would have to do a TON of image processing. – BradleyDotNET Mar 26 '14 at 16:07
  • @LordTakkera: This is grossly incorrect. It takes 2 or 3 lines of code to display two images in a PictureBox. – dotNET Mar 26 '14 at 16:48
  • @dotNET, Was not aware of the Paint methods. I'm not sure that counts as "loading" two images (since you are forcing the draw, as opposed to setting the source like normal) but that is a great solution. Thanks for the information! – BradleyDotNET Mar 26 '14 at 17:02
  • Right. Even if by "load" he means he wants to save two sources, all he needs to do is to derive from `PictureBox` (or `Control`) and define two public properties of `Image` type. Then he can again follow the `DrawImage` route and display them in `OnPaint()` overload. I guess this will be 15 or so easy lines of code. – dotNET Mar 26 '14 at 17:12

1 Answers1

3

You can use Graphics.DrawImage() to draw any image anywhere inside a PictureBox or any other control for that matter. If you're writing your own control, override OnPaint(). If you want to use an existing PictureBox, simply use its Paint event to do this:

e.Graphics.DrawImage(YourImageObjectHere, ...);  
e.Graphics.DrawImage(YourSecondImageObjectHere, ...);

GDI+ already supports transparency channel, so if your images have transparent areas, they'll draw just like the sample image you have posted. DrawImage() has over a dozen overloads, using which you can control several aspects of how an image is drawn. The simplest one takes the image object and the position to draw at.

Remember that an image object is an object of System.Drawing.Image or one of its derived classes. If all you have is the path of the image, you should use Image.FromFile() to create an Image object from that image file first.

dotNET
  • 33,414
  • 24
  • 162
  • 251