久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

使用打開文件對話框將位圖圖像加載到 Windows 窗

Load a bitmap image into Windows Forms using open file dialog(使用打開文件對話框將位圖圖像加載到 Windows 窗體中)
本文介紹了使用打開文件對話框將位圖圖像加載到 Windows 窗體中的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我需要使用打開文件對話框以窗口形式打開位圖圖像(我將從驅動器加載它).圖像應適合圖片框.

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.

這是我試過的代碼:

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 類,使用 構造函數重載,從磁盤上的文件加載圖像.在編寫代碼時,您正在嘗試使用 PictureBox.Image 屬性,就好像它是一種方法.

You have to create an instance of the Bitmap class, using the constructor overload that loads an image from a file on disk. As your code is written now, you're trying to use the PictureBox.Image property as if it were a method.

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

Change your code to look like this (also taking advantage of the using statement to ensure proper disposal, rather than manually calling the Dispose method):

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

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

Of course, that's not going to display the image anywhere on your form because the picture box control that you've created hasn't been added to the form. You need to add the new picture box control that you've just created to the form's Controls collection using the Add method. Note the line added to the above code here:

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

這篇關于使用打開文件對話框將位圖圖像加載到 Windows 窗體中的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加圖標?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
主站蜘蛛池模板: 毛片在线免费 | 久久人| 黄色在线免费观看 | 精品国产91久久久久久 | 日韩在线免费视频 | 精品亚洲国产成av人片传媒 | 国产农村一级国产农村 | 亚洲国产欧美一区 | a毛片 | 99热欧美| 在线一区 | 精品日韩一区 | 91黄在线观看 | 成人免费视频观看视频 | 国产一区免费 | 久久丝袜 | 一道本视频 | 免费的av网站 | 欧美一级在线 | 免费观看羞羞视频网站 | 99re在线视频观看 | 天堂在线中文 | 成人免费在线小视频 | 日韩三级在线 | 凹凸日日摸日日碰夜夜 | 国产成人jvid在线播放 | 91av免费看 | 精品国产一区二区 | 日韩欧美一区二区三区免费观看 | 中文字幕一区二区三区精彩视频 | xx性欧美肥妇精品久久久久久 | 欧美日韩在线观看一区二区三区 | 欧美综合一区 | 91伦理片| 精品视频一区二区三区在线观看 | 激情网站在线观看 | 中文字幕一区二区三区精彩视频 | 一区二区久久 | 一区二区三区四区在线视频 | 99国产精品99久久久久久 | 五月激情婷婷网 |