ha.M.ed
ha.M.ed

Reputation: 985

Display images with PictureBox in c#

I have write below codes in c# to display an image in PictureBox but when run the application, nothing shown...

Please help me to fix that.

here is my code:

    private void button1_Click(object sender, EventArgs e)
    {
        PictureBox p =new PictureBox();
        p.ImageLocation = "1.jpg"
        p.Location = new Point(100, 75);
    }

Upvotes: 3

Views: 54564

Answers (4)

Missael
Missael

Reputation: 1

public static async Task<PictureBox> CargarImagenDesdeUnDirectorioAsync()
    {
PictureBox pbImage
        try
        {
            openFileDialog.Title = "Open Image";
            openFileDialog.Filter = "Imagen (*.jpg)|*.jpg|Imagen (*.png)|*.png";
            openFileDialog.InitialDirectory = Environment.GetFolderPath (Environment.SpecialFolder.MyPictures);
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                // pbImage.ImageLocation = openFileDialog.FileName;
                pbImage.Image = new Bitmap(openFileDialog.FileName);
                
                await Task.Delay(200);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("Error al cargar la imagen, mensaje del error: " + e.Message);
        }
        return pbImage;
    }

Upvotes: 0

ptolomeo
ptolomeo

Reputation: 11

It's possible that the PictureBox size is small and your image is too big (check the SizeMode property to "StrechImage") I was using a high res PNG icon with transparent background and took a little time to figure out.

Upvotes: 1

Rosmarine Popcorn
Rosmarine Popcorn

Reputation: 10965

PictureBox.Image = new Bitmap("yourImage.jpg"); 

The formats supported are: BMP, EMF, EXIF, GIF, ICON, JPEG, PNG, TIFF and WMF.

Upvotes: 6

Hans Passant
Hans Passant

Reputation: 942408

Add this line:

  this.Controls.Add(p);

Upvotes: 11

Related Questions