PDA

Click to See Complete Forum and Search --> : [1.1] Add multiple images in a single picturebox


Kal-El
Aug 3rd, 2007, 07:54 PM
Hi all!

I have this load an image to a picturebox:

private void onLoadImages(object sender, System.EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.InitialDirectory = System.Environment.CurrentDirectory;
ofd.Filter = "Icons (*.ico)|*.ico|BMPs (*.bmp)|*.bmp|GIFs (*.gif)|*.gif|JPEGs (*.jp*)|*.jp*|PNGs (*.png)|*.png";
if (ofd.ShowDialog(this) != DialogResult.Cancel)
{
Image img = Image.FromFile(ofd.FileName);
this.pictureBox1.Image = new Bitmap(img, new Size(32, 32));
}
}

But this, as you may know..replace the old image...I want to make like a imagelist viewer, so the the image will be next to another...any ideas?

jmcilhinney
Aug 3rd, 2007, 09:22 PM
It's not possible to display more than one Image in a PictureBox. You can either use multiple PictureBoxes to display one Image each, or else combine all your individual images into a single Image object and display that in a single PictureBox. I'd be inclined to go with the first option.

bobphrapples1414
Sep 20th, 2007, 02:04 PM
Actually it IS possible to put more than one image in a picturebox. But only two ... backgroundimage and image if you use the properties.

So here's what you have to do to put MORE THAN ONE image in a picturebox control.

Bitmap BG1 = new Bitmap(BMP_FileName);
Bitmap BG2 = new Bitmap(BMP_FileName);
Bitmap BG3 = new Bitmap(BMP_FileName);
Bitmap BG4 = new Bitmap(BMP_FileName);

// overwrite paint
private void PictureBox_Paint(object sender, PaintEventArgs e)
{
// Create Point for upper-left corner of image1.
if (BG1 != null)
{
Point P1 = new Point(0, 0);
e.Graphics.DrawImage(BG1, P1);
}
if (BG2 != null)
{
Point P2 = new Point(0, 64);
e.Graphics.DrawImage(BG2, P2);
}
if (BG3 != null)
{
Point P3 = new Point(64, 0);
e.Graphics.DrawImage(BG3, P3);
}
if (BG4 != null)
{
Point P4 = new Point(64, 64);
e.Graphics.DrawImage(BG4, P4);
}
//.... and so forth
}