Window image display

I wanted to display the image in the windows, but I already did this and the image did not appear.

Where am I 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); } 
+6
source share
3 answers

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

  • Just like you.
  • Using the ImageLocation property for a PictureBox:

     private void Form1_Load(object sender, EventArgs e) { PictureBox pb1 = new PictureBox(); pb1.ImageLocation = "../SamuderaJayaMotor.png"; pb1.SizeMode = PictureBoxSizeMode.AutoSize; } 
  • Using images from the Internet, for example:

     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 make sure that "../SamuderaJayaMotor.png" is the correct path to the image used.

+7
source

I display images in window forms when I put it in the Load event as follows:

  private void Form1_Load( object sender , EventArgs e ) { pictureBox1.ImageLocation = "./image.png"; //path to image pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize; } 
+1
source

There can be many reasons for this. Some of them quickly come to my mind:

  • Did you call this procedure AFTER InitializeComponent() ?
  • Is the path syntax used correctly? Does it work if you try it in the debugger? Try using backslash (\) instead of slash (/) and see.
  • This may be due to side effects of some other code in your form. Try to use the same code in an empty form (only with the constructor and with this function) and check.
0
source

Source: https://habr.com/ru/post/955286/


All Articles