久久久久久久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自動化控制桌面應用程序并單擊菜單條)
主站蜘蛛池模板: 午夜精品在线 | 双性呜呜宫交受不住了h | av青青草| 亚洲精品久久 | 国产成人精品亚洲男人的天堂 | 国产激情在线视频 | 国产91精品在线观看 | 色综合视频 | 日韩免费一区二区三区 | 黄色在线免费观看视频 | 精品国产区一区二 | 成人午夜在线视频 | 国产又粗又大又爽 | 男人影院在线观看 | 欧美精品在线视频 | 欧美黄色精品 | 国产精品国产成人国产三级 | 国产h视频在线观看 | 天天爽天天爽 | 成人av在线网站 | 精品理论片| 欧美日韩一区在线观看 | 久久精品国产免费 | 日韩精品久久久久久免费 | 日韩精品视频免费在线观看 | 香蕉看片 | 在线观看av的网站 | 国产精品久久 | 久久亚洲欧美 | 狠狠干av | 欧美激情影院 | 日韩美女在线 | 欧美 日韩 国产 成人 在线 | 亚洲天码中字 | 亚洲成人av在线播放 | 亚洲精品91天天久久人人 | 一级看片免费视频 | 九九热在线播放 | 日本国产精品 | 成人精品免费视频 | 日韩视频免费看 |