将图片从文件加载到表单 [英] Load pictures from a file to a form

查看:79
本文介绍了将图片从文件加载到表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

M使用SQL Server(数据库).

我在Windows应用程序窗体上有一个组合框,因此,当用户选择像南非这样的国家时,该标志必须贴在窗体上.

Images文件位于bin中.

请帮助我没有任何想法,我必须提交此项目

M using SQL Server(Database).

i have a combobox on my Windows Application Form so when a user select a country like South Afrika, the flag must apper on a Form.

The Images file is on a bin .

Please help i don''t have any idea and i have to submit this project

推荐答案

在您的表单中,我确定您有一个名为comboBox1的组合框和一个图像组合框旁边的名为countryImageBox的控件-

假定您在bin文件夹中有标志文件,并且在组合框中有国家名称或文件名,则可以执行以下操作:

In your form I am assuimg you have a combo box called comboBox1 and a image control called countryImageBox next to the combo box -

Given that you have the flag files in bin folder and in your combo box you have the country name or the file name, you can do like this:

namespace CountryList
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Add(new ListItem() { Name = "Australia", FlagName = "as.gif" });
            comboBox1.Items.Add(new ListItem() { Name = "England", FlagName = "england.gif" });
            comboBox1.Items.Add(new ListItem() { Name = "US", FlagName = "us-s.gif" });

            comboBox1.DisplayMember = "Name";
            comboBox1.ValueMember = "Name";
            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
        }

        void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedItem != null)
            {
                this.countryImageBox.Image = Image.FromFile(((ListItem)comboBox1.SelectedItem).FlagName);
            }
        }
    }

    public class ListItem
    {
        public string Name { get; set; }
        public string FlagName { get; set; }
    }
}



代码非常简单,基本上在组合框的selectedIndex更改事件上,我们可以找到选定的项目及其相关的标志文件名.然后我们只显示该文件中的图像.



The code is very simple, basically on selectedIndex change event of the combo box we find out what is the item selected and it''s related flag file name. Then we just display image from that file.


希望这会有所帮助

hope this helps

private void button1_Click(object sender, EventArgs e)
       {
           openFileDialog1.ShowDialog();
           Image image = Image.FromFile(openFileDialog1.FileName);
           // Set the PictureBox image property to this image.
           // ... Then, adjust its height and width properties.
           pictureBox1.Image = image;
           pictureBox1.Height = image.Height;
           pictureBox1.Width = image.Width;
       }


这篇关于将图片从文件加载到表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆