C# 单击图片框事件如何以新形式打开图像
我从文件夹中获取图像,并使用以下代码将它们显示在图片框中C# 单击图片框事件如何以新形式打开图像,c#,winforms,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
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
}
}
您可以在代码中手动添加事件处理程序,如下所示:
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
}
您可以在创建
PictureBox
i、 e
正如我在评论中所说的那样。您可以像这样创建自定义表单构造函数
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();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
FormtoOpen form = new FormtoOpen();
form.setPicture(((PictureBox)sender).Image);
form.Show();
}
或者在FormtoOpen上创建一个属性/方法来执行相同的操作
public void setPicture(Image img)
{
this.BackgroundImage = img;
}
如果您这样做,您会将pictureBox\u单击更改为类似的内容
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();
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
FormtoOpen form = new FormtoOpen();
form.setPicture(((PictureBox)sender).Image);
form.Show();
}
在评论中阐述你的问题。图像有一个标记
属性,您可以将路径添加到图像标记,然后以第二种形式提取它。将您填写列表的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
方法作为示例)
请告诉我如何在FormtoOpen
@Durga中传递特定图像的路径。如果要传递路径,需要使用PictureBox的Tag属性。将图像添加到PictureBox中时,只需将图像路径的值指定给它即可。能否用代码显示上面注释的解决方案,以便传递path@Durga不是用电脑,今天晚上当我再次使用电脑时,我会更新。让我们一起来吧