Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an image into windows forms

Tags:

c#

I wanted to display an image to the windows forms, but i already did this and the image did not come out.

Where did I go wrong?

Here is the code:

private void Images(object sender, EventArgs e)
{
    PictureBox pb1 = new PictureBox();
    pb1.Image = Image.FromFile("../SamuderaJayaMotor.png");
    pb1.Location = new Point(100, 100);
    pb1.Size = new Size(500, 500);
    this.Controls.Add(pb1);
}
like image 450
Kaoru Avatar asked Oct 05 '13 03:10

Kaoru


People also ask

How do I upload a picture to Winforms?

To allow users to select files in a Windows Forms application you should look into using the OpenFileDialog class. To use the dialog on your form you will need to find it in the toolbox in Visual Studio and drag it on to your form. You can then use the file path to perform whatever task you wish with the file.


1 Answers

Here (http://www.dotnetperls.com/picturebox) there 3 ways to do this:

  • Like you are doing.
  • Using ImageLocation property of the PictureBox like:

    private void Form1_Load(object sender, EventArgs e)
    {
        PictureBox pb1 = new PictureBox();            
        pb1.ImageLocation = "../SamuderaJayaMotor.png";
        pb1.SizeMode = PictureBoxSizeMode.AutoSize;
    }
    
  • Using an image from the web like:

    private void Form1_Load(object sender, EventArgs e)
    {
        PictureBox pb1 = new PictureBox();            
        pb1.ImageLocation = "http://www.dotnetperls.com/favicon.ico";
        pb1.SizeMode = PictureBoxSizeMode.AutoSize;
    }
    

And please, be sure that "../SamuderaJayaMotor.png" is the correct path of the image that you are using.

like image 132
2 revs Avatar answered Oct 02 '22 13:10

2 revs