pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

使用打開文件對話框將位圖圖像加載到 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自動化控制桌面應用程序并單擊菜單條)
主站蜘蛛池模板: 365文案网_全网创意文案句子素材站 | 低温等离子清洗机(双气路进口)-嘉润万丰 | 粉末包装机-给袋式包装机-全自动包装机-颗粒-液体-食品-酱腌菜包装机生产线【润立机械】 | 回转炉,外热式回转窑,回转窑炉-淄博圣元窑炉工程有限公司 | 户外健身路径_小区健身器材_室外健身器材厂家_价格-浩然体育 | 臭氧灭菌箱-油桶加热箱-原料桶加热融化烘箱-南京腾阳干燥设备厂 臭氧发生器_臭氧消毒机 - 【同林品牌 实力厂家】 | 3d打印服务,3d打印汽车,三维扫描,硅胶复模,手板,快速模具,深圳市精速三维打印科技有限公司 | 缓蚀除垢剂_循环水阻垢剂_反渗透锅炉阻垢剂_有机硫化物-郑州威大水处理材料有限公司 | 光栅尺厂家_数显表维修-苏州泽升精密机械| 钢格板|镀锌钢格板|热镀锌钢格板|格栅板|钢格板|钢格栅板|热浸锌钢格板|平台钢格板|镀锌钢格栅板|热镀锌钢格栅板|平台钢格栅板|不锈钢钢格栅板 - 专业钢格板厂家 | 济南铝方通-济南铝方通价格-济南方通厂家-山东鲁方通建材有限公司 | crm客户关系管理系统,销售管理系统,crm系统,在线crm,移动crm系统 - 爱客crm | 宝宝药浴-产后药浴-药浴加盟-艾裕-专注母婴调养泡浴 | 济南品牌设计-济南品牌策划-即合品牌策划设计-山东即合官网 | 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 - 杭州标识标牌|文化墙|展厅|导视|户内外广告|发光字|灯箱|铭阳制作公司 | 桑茶-七彩贝壳桑叶茶 长寿茶| 浙江自考_浙江自学考试网 | 氧化铝球_高铝球_氧化铝研磨球-淄博誉洁陶瓷新材料有限公司 | 写方案网_方案策划方案模板下载| 根系分析仪,大米外观品质检测仪,考种仪,藻类鉴定计数仪,叶面积仪,菌落计数仪,抑菌圈测量仪,抗生素效价测定仪,植物表型仪,冠层分析仪-杭州万深检测仪器网 | 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 | 衬氟旋塞阀-卡套旋塞阀-中升阀门首页| 浙江皓格药业有限公司| 安全光栅|射频导纳物位开关|音叉料位计|雷达液位计|两级跑偏开关|双向拉绳开关-山东卓信机械有限公司 | 特材真空腔体_哈氏合金/镍基合金/纯镍腔体-无锡国德机械制造有限公司 | 恒温恒湿箱(药品/保健品/食品/半导体/细菌)-兰贝石(北京)科技有限公司 | 东莞画册设计_logo/vi设计_品牌包装设计 - 华略品牌设计公司 | 青岛成人高考_山东成考报名网 | 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 中国产业发展研究网 - 提供行业研究报告 可行性研究报告 投资咨询 市场调研服务 | 天津热油泵_管道泵_天津高温热油泵-天津市金丰泰机械泵业有限公司【官方网站】 | 汽车润滑油厂家-机油/润滑油代理-高性能机油-领驰慧润滑科技(河北)有限公司 | 浩方智通 - 防关联浏览器 - 跨境电商浏览器 - 云雀浏览器 | led太阳能路灯厂家价格_风光互补庭院灯_农村市政工程路灯-中山华可路灯品牌 | 许昌奥仕达自动化设备有限公司| 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 流水线电子称-钰恒-上下限报警电子秤-上海宿衡实业有限公司 | 小型气象站_便携式自动气象站_校园气象站-竞道气象设备网 | 综合管廊模具_生态,阶梯护坡模具_检查井模具制造-致宏模具厂家 | 德州万泰装饰 - 万泰装饰装修设计软装家居馆| 无线联网门锁|校园联网门锁|学校智能门锁|公租房智能门锁|保障房管理系统-KEENZY中科易安 |