Display an image into windows forms

前端 未结 4 1797
借酒劲吻你
借酒劲吻你 2021-02-07 11:51

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:



        
相关标签:
4条回答
  • 2021-02-07 12:19

    I display images in windows forms when I put it in Load event like this:

        private void Form1_Load( object sender , EventArgs e )
        {
            pictureBox1.ImageLocation = "./image.png"; //path to image
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        }
    
    0 讨论(0)
  • 2021-02-07 12:23
    private void Form1_Load(object sender, EventArgs e)
        {
            PictureBox pb = new PictureBox();
            pb.Location = new Point(0, 0);
            pb.Size = new Size(150, 150);
            pb.Image = Image.FromFile("E:\\Wallpaper (204).jpg");
            pb.Visible = true;
            this.Controls.Add(pb);
        }
    
    0 讨论(0)
  • 2021-02-07 12:24

    There could be many reasons for this. A few that come up quickly to my mind:

    1. Did you call this routine AFTER InitializeComponent()?
    2. Is the path syntax you are using correct? Does it work if you try it in the debugger? Try using backslash (\) instead of Slash (/) and see.
    3. This may be due to side-effects of some other code in your form. Try using the same code in a blank Form (with just the constructor and this function) and check.
    0 讨论(0)
  • 2021-02-07 12:27

    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.

    0 讨论(0)
提交回复
热议问题