1

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

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 eventwinform

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

2 回答 2

2

您可以在创建您的PictureBox's

IE

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对象是PictureBox发起点击事件的对象,因此您可以将发送者对象转换为 PictureBox 并提取图像这样的内容。

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
}
于 2013-11-05T04:13:58.563 回答
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
    }
于 2013-11-05T04:01:15.377 回答