久久久久久久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自動化控制桌面應用程序并單擊菜單條)
主站蜘蛛池模板: 中文字幕在线看第二 | 欧美a级成人淫片免费看 | 午夜影院操 | 欧美成人手机视频 | 国产精品揄拍一区二区久久国内亚洲精 | 狠狠爱网址 | 免费黄色日本 | 男人的天堂久久 | 91国产在线播放 | 91av在线免费观看 | 2018国产精品| 一区二区不卡视频 | 久久男人| 极品一区 | 中文字幕成人av | 九九热精品视频 | 99免费在线观看视频 | 亚洲国产精品久久久 | 亚洲成人网在线播放 | 欧美一级在线观看 | 免费特级黄毛片 | 伊人免费视频二 | 免费在线观看一区二区三区 | 日韩手机在线看片 | 国产亚洲精品久久情网 | 浮生影院免费观看中文版 | 在线观看中文字幕亚洲 | 欧美激情亚洲激情 | 日本一区二区三区视频在线 | 亚洲人人 | 在线免费观看一区二区 | 日韩欧美国产一区二区三区 | 91精品国产一区二区三区 | 在线视频一区二区 | 成年人视频免费在线观看 | 久久99精品视频 | 久久久毛片 | 日韩中文字幕在线视频 | 毛片网站在线观看视频 | 国产午夜亚洲精品不卡 | 一级国产精品一级国产精品片 |