我有一个充满图片的文件夹等,我想在程序运行后将它们添加到我的gui里面。
步骤进行:
[] [] []
[] [] []
[] []
不需要在3 ^中可分,这很好
我的问题是,我甚至没有找到一种方法来添加图像到gui而不使用设计师,而且对于我来说这是一个相当新的#所以这对我来说是一个巨大的挑战:(
在AHK中我只会gui,add,picture但它在c#中不起作用帮助或某种形式的提示/指导非常感谢
图片需要“有价值”的原因是,一旦点击它们就知道哪个图片被点击了,因为他们应该将他们的点击事件发送到同一个功能,检查是什么号码和更改相应的变量值
感谢帮助SO C#压倒性的
public static void ProcessDirectory(string targetDirectory)
{
Console.WriteLine("Processed folder '{0}'.", targetDirectory);
// Process the list of files found in the directory.
string[] fileEntries = Directory.GetFiles(targetDirectory);
foreach (string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach (string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
if (!ImageExtensions.Contains(Path.GetExtension(path).ToUpperInvariant()))
{
Console.WriteLine("not image");
return;
}
FlowLayoutPanel flowLayoutPanel1 = new FlowLayoutPanel();
Image i = new Bitmap(path);
PictureBox p = new PictureBox();
p.Image = i;
p.Tag = "im1";
p.Click += OnImageClick; // this will let you have the same event for all of the pictures
flowLayoutPanel1.Controls.Add(p);
}
// this is what handles the clicks
private static void OnImageClick(object sender, EventArgs e)
{
MessageBox.Show("hey");
// I will leave this for you to implement... the 'sender' is the picturebox that was clicked.
// you can get it back to a PictureBox by casting, like (PictureBox)sender
// throw new NotImplementedException();
}
答案 0 :(得分:2)
这篇MSDN文章讨论了如何获取特定目录(“文件夹”)中文件的所有文件路径
他们的例子:
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;
public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}
// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);
// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}
// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}
这个article(也是MSDN)谈到在运行时动态添加控件,这是他们的示例代码:
// C#
private void button1_Click(object sender, System.EventArgs e)
{
TextBox myText = new TextBox();
myText.Location = new Point(25,25);
this.Controls.Add (myText);
}
最后,这篇article(再次MSDN)文章讨论了如何从文件设置图像,这是他们的示例代码:
// C#
// You should replace the bolded image
// in the sample below with an icon of your own choosing.
// Note the escape character used (@) when specifying the path.
pictureBox1.Image = Image.FromFile
(System.Environment.GetFolderPath
(System.Environment.SpecialFolder.MyPictures)
+ @"\Image.gif");
编辑:在评论中询问如何连接控件点击事件,因为我们以编程方式添加这些控件,我们需要以编程方式添加click事件。出于演示目的,假设有一个标识为Button1
的控件,要在单击它时触发函数,您将使用类似这样的代码:
Button1.Click += new System.EventHandler(this.myEventHandler);
点击myEventHandler
时会激活Button1
函数
那就是很多代码!
虽然它可能看起来不像,但如果你看一下每一件并考虑它们如何组合在一起,这应该可以帮助你走上正确的道路(大提示),动态获取一个清单对图像添加图像控件,然后设置该图像控件以显示特定图像,最后在图像控件上连接点击事件
答案 1 :(得分:2)
要在Winforms中以编程方式将图像,文本或其他任何内容添加到GUI(您正在使用 sound ),您将创建一个源自Control
的内容的实例并将其添加到表单的Controls
ControlCollection中。您可以使用FLowLayoutPanel
为其提供类似于网格的网格。
// get the list of paths to the files from your directory into an array
var fileList = Directory.GetFiles(@"C:\Images","*.jpg");
// create a flowlayout panel
FlowLayoutPanel f = new FlowLayoutPanel();
foreach (string path in fileList)
{
Image i = new Bitmap(path);
PictureBox p = new PictureBox();
p.Image = i;
p.Click += OnImageClick; // this will let you have the same event for all of the pictures
f.Controls.Add(p);
}
// add the panel to the form
this.Controls.Add(p);
// this is what handles the clicks
private void OnImageClick(object sender, EventArgs e)
{
// I will leave this for you to implement... the 'sender' is the picturebox that was clicked.
// you can get it back to a PictureBox by casting, like (PictureBox)sender
throw new NotImplementedException();
}
这种实现很快而且很脏:也就是说,如果找不到目录,如果没有任何文件,它就不会处理,等等。它应该让你知道去哪里。我建议更多地阅读 ,获取一本关于C#的书,并通过一些示例/教程可以获得极大的帮助。你是对的,C#可能会让人感到压力......这就是为什么Google是你最好的朋友!你不需要立即学会如何做所有事情;我发现通过选择一个我想做的小范围项目并在我找到我需要的东西时学习,我学得很好。
答案 2 :(得分:0)
您可以使用Image.Tag属性向图像添加额外信息。