在图片框的点击事件如何以新的形式打开图像

时间:2013-11-05 03:10:37

标签: c# winforms

我正在从文件夹中获取图像并使用以下代码将其显示在图片框中

protected void image()
{
    string str = System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetExecutingAssembly().Location);
    string path = str + "\\images\\";
    //Our target folder; change this to the folder to get the images from
    string GivenFolder = str + "\\images\\"; 
    //Initialize a new List of type Image as ImagesInFolder
    List<System.Drawing.Image> ImagesInFolder = new List<System.Drawing.Image>(); 

    // Initialize a new string of name JPEGImages for every string in the 
    // string array returned from the given folder as files 
    foreach (string JPEGImages in Directory.GetFiles(GivenFolder, "*.jpg")) 
    {
        //Add the Image gathered to the List collection
        ImagesInFolder.Add(System.Drawing.Image.FromFile(JPEGImages)); 
    }
    int x = 0; //Initialize X as int of value 0
    int y = 0; //Initialize Y as int of value 0
    // Initialize i as an int of value 0, continue if i is less than ImagesInFolder 
    // count. Increment i by 1 each time you continue
    for (int i = 0; i < ImagesInFolder.Count; i++) 
    {
        PictureBox I = new PictureBox(); //Initialize a new PictureBox of name I
        I.Location = new System.Drawing.Point(x, y); //Set the PictureBox location to x,y
        x += 50; //Sort horizontally; Increment x by 50
        //y += 50; //Sort vertically; Increment y by 50
        //Set the Image property of I to i in ImagesInFolder as index
        I.Image = ImagesInFolder[i]; 
        //Set the PictureBox Size property to 50,50
        I.Size = new System.Drawing.Size(80, 80); 
        //Stretch the image; maximum width and height are 50,50
        I.SizeMode = PictureBoxSizeMode.StretchImage; 

        flowLayoutPanel1.Controls.Add(I); //Add the PictureBox to the FlowLayoutPanel
    }
}

如何编写函数,以便我可以在新picture box's image的特定图片框的click event中打开特定的winform,因为我正在从代码创建图片框,所以我无法从propwerty窗口中选择事件,所以请指导我如何从代码中执行此操作

private void PictureboxClick_event()
{
FormtoOpen f=new FormtoOpen();
f.show();
//.....how that particular image will be displayed ?
}

2 个答案:

答案 0 :(得分:2)

您可以在创建PictureBox's

时添加公共事件处理程序

for (int i = 0; i < ImagesInFolder.Count; i++) 
{
    PictureBox I = new PictureBox(); //Initialize a new PictureBox of name I
    I.Location = new System.Drawing.Point(x, y); //Set the PictureBox location to x,y
    x += 50; //Sort horizontally; Increment x by 50
    //y += 50; //Sort vertically; Increment y by 50
    //Set the Image property of I to i in ImagesInFolder as index
    I.Image = ImagesInFolder[i]; 
    //Set the PictureBox Size property to 50,50
    I.Size = new System.Drawing.Size(80, 80); 
    //Stretch the image; maximum width and height are 50,50
    I.SizeMode = PictureBoxSizeMode.StretchImage; 
    //Add the Event handler to the click event
    I.Click += pictureBox_Click;

    flowLayoutPanel1.Controls.Add(I); //Add the PictureBox to the FlowLayoutPanel
}

在您的点击事件中,您会发现sender对象是发起click事件的PictureBox,因此您可以将发件人对象强制转换为PictureBox并将Image提取出来。

private void pictureBox_Click(object sender, EventArgs e)
{

    //This is supposing that you have created a custom constructor of your FormtoOpen that can take the Image
    //You could also create a Property to do the same thing.
    FormtoOpen f = new FormtoOpen(((PictureBox)sender).Image);
    f.Show();
}

正如我在评论中所说的那样。您可以创建这样的自定义表单构造函数。

public partial class FormtoOpen : Form
{
    public FormtoOpen( Image img)
    {
        this.BackgroundImage = img;
        InitializeComponent();
    }
}

或在FormtoOpen上创建一个属性/方法来做同样的事情。

public void setPicture(Image img)
{
    this.BackgroundImage = img;
}

如果您这样做,则会将pictureBox_Click更改为此类内容。

private void pictureBox1_Click(object sender, EventArgs e)
{
    FormtoOpen form = new FormtoOpen();
    form.setPicture(((PictureBox)sender).Image);
    form.Show();
}

在评论中阐述您的问题。图像具有Tag属性,您可以添加图像标记的路径,然后以第二种形式提取它。将您填写列表的foreach循环更改为此类

foreach (string JPEGImages in Directory.GetFiles(GivenFolder, "*.jpg"))
{
    //Add the Image gathered to the List collection
    Image img = System.Drawing.Image.FromFile(JPEGImages);
    img.Tag = JPEGImages;
    ImagesInFolder.Add(img);
}

然后它将以您的第二种形式提供(我使用前一个示例中的setPicture方法作为示例

public void setPicture(Image img)
{
    this.BackgroundImage = img;
    this.Text = img.Tag.ToString(); //The image's Tag property is an object so it needs to be converted to a string
}

答案 1 :(得分:0)

您可以在代码中手动添加事件处理程序,例如:

    PictureBox pic = new PictureBox(); //create picturebox
    pic.Click += pic_Click; // hook click event on picturebox


    void pic_Click(object sender, EventArgs e)
    {
        //code here that opens the new form
    }