Load a bitmap image into Windows Forms using open file dialog

后端 未结 8 708
梦毁少年i
梦毁少年i 2020-12-10 02:21

I need to open the bitmap image in the window form using open file dialog (I will load it from drive). The image should fit in the picture box.

Here is the code I t

相关标签:
8条回答
  • 2020-12-10 02:46

    You should try to:

    • Create the picturebox visually in form (it's easier)
    • Set Dock property of picturebox to Fill (if you want image to fill form)
    • Set SizeMode of picturebox to StretchImage

    Finally:

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog dlg = new OpenFileDialog();
        dlg.Title = "Open Image";
        dlg.Filter = "bmp files (*.bmp)|*.bmp";
        if (dlg.ShowDialog() == DialogResult.OK)
        {                     
            PictureBox1.Image = Image.FromFile(dlg.Filename);
        }
        dlg.Dispose();
    }
    
    0 讨论(0)
  • 2020-12-10 02:49

    PictureBox.Image is a property, not a method. You can set it like this:

    PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);
    
    0 讨论(0)
  • 2020-12-10 02:50

    You can try the following:

    private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog fDialog = new OpenFileDialog();
            fDialog.Title = "Select file to be upload";
            fDialog.Filter = "All Files|*.*";
            //  fDialog.Filter = "PDF Files|*.pdf";
            if (fDialog.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = fDialog.FileName.ToString();
            }
        }
    
    0 讨论(0)
  • 2020-12-10 02:51

    Works Fine. Try this,

    private void addImageButton_Click(object sender, EventArgs e)
    {
        OpenFileDialog of = new OpenFileDialog();
        //For any other formats
        of.Filter = "Image Files (*.bmp;*.jpg;*.jpeg,*.png)|*.BMP;*.JPG;*.JPEG;*.PNG"; 
        if (of.ShowDialog() == DialogResult.OK)
        {
            pictureBox1.ImageLocation = of.FileName;
    
        }
    }
    
    0 讨论(0)
  • 2020-12-10 02:52

    It's simple. Just add:

    PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;
    
    0 讨论(0)
  • 2020-12-10 02:53
    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog open = new OpenFileDialog();
        if (open.ShowDialog() == DialogResult.OK)
            pictureBox1.Image = Bitmap.FromFile(open.FileName);
    }
    
    0 讨论(0)
提交回复
热议问题