簡體   English   中英

使用打開文件對話框將 bitmap 圖像加載到 Windows Forms 中

[英]Load a bitmap image into Windows Forms using open file dialog

我需要使用打開文件對話框打開 window 表單中的 bitmap 圖像(我將從驅動器加載它)。 圖像應適合圖片框。

這是我嘗試過的代碼:

private void button1_Click(object sender, EventArgs e)
{
    var dialog = new OpenFileDialog();

    dialog.Title  = "Open Image";
    dialog.Filter = "bmp files (*.bmp)|*.bmp";

    if (dialog.ShowDialog() == DialogResult.OK)
    {                     
        var PictureBox1 = new PictureBox();                    
        PictureBox1.Image(dialog.FileName);
    }

    dialog.Dispose();
}

您必須使用從磁盤上的文件加載圖像的構造函數重載來創建Bitmap class的實例。 由於您的代碼現在是編寫的,因此您正在嘗試使用PictureBox.Image屬性,就好像它是一個方法一樣。

將您的代碼更改為如下所示(也利用using語句來確保正確處理,而不是手動調用Dispose方法):

private void button1_Click(object sender, EventArgs e)
{
    // Wrap the creation of the OpenFileDialog instance in a using statement,
    // rather than manually calling the Dispose method to ensure proper disposal
    using (OpenFileDialog dlg = new OpenFileDialog())
    {
        dlg.Title = "Open Image";
        dlg.Filter = "bmp files (*.bmp)|*.bmp";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            PictureBox PictureBox1 = new PictureBox();

            // Create a new Bitmap object from the picture file on disk,
            // and assign that to the PictureBox.Image property
            PictureBox1.Image = new Bitmap(dlg.FileName);
        }
    }
}

當然,這不會在表單的任何位置顯示圖像,因為您創建的圖片框控件尚未添加到表單中。 您需要使用Add方法將剛剛創建的新圖片框控件添加到窗體的Controls集合中。 請注意在此處添加到上述代碼中的行:

private void button1_Click(object sender, EventArgs e)
{
    using (OpenFileDialog dlg = new OpenFileDialog())
    {
        dlg.Title = "Open Image";
        dlg.Filter = "bmp files (*.bmp)|*.bmp";

        if (dlg.ShowDialog() == DialogResult.OK)
        {
            PictureBox PictureBox1 = new PictureBox();
            PictureBox1.Image = new Bitmap(dlg.FileName);

            // Add the new control to its parent's controls collection
            this.Controls.Add(PictureBox1);
        }
    }
}

工作正常。 嘗試這個,

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;

    }
}

您應該嘗試:

  • 以形式直觀地創建圖片框(更容易)
  • 將圖片框的Dock屬性設置為Fill (如果您希望圖像填充表單)
  • 設置picturebox的SizeModeStretchImage

最后:

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();
}
private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog open = new OpenFileDialog();
    if (open.ShowDialog() == DialogResult.OK)
        pictureBox1.Image = Bitmap.FromFile(open.FileName);
}

您可以嘗試以下方法:

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();
        }
    }

你,也可以這樣試試, PictureBox1.Image = Image.FromFile("<your ImagePath>" or <Dialog box result>);

PictureBox.Image 是一個屬性,而不是一個方法。 你可以這樣設置:

PictureBox1.Image = System.Drawing.Image.FromFile(dlg.FileName);

這很簡單。 只需添加:

PictureBox1.BackgroundImageLayout = ImageLayout.Zoom;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM