[英]How can you make an array of bytes with folder full of JPEG images and display them in a column of DataGridView?
Right now I am using the following code to populate my third column in DataGridView: 现在我使用以下代码填充DataGridView中的第三列:
Bitmap img = new
Bitmap(@"C:\Project\Images\Image1.jpg");
DataGridView [2, 0].Value = img; //2,0 is 1st row, 3rd column
Bitmap img2 = new
Bitmap(@"C:\Project\Images\Image2.jpg");
DataGridView [2, 1].Value = img2; //2,1 is 2nd row, 3rd column
The third column is correctly populated with the image, but I have this code duplicated 26 more times. 第三列正确填充了图像,但我将此代码重复了26次。 For simplicity sake I just want to know if their is a simpler way of doing this, such as making an array or inserting the images directly to my database. 为简单起见,我只想知道它们是否是一种更简单的方法,例如制作数组或将图像直接插入我的数据库。 I apologize if this is a novice question, by I am relatively new to C#. 如果这是一个新手问题我很抱歉,我对C#相对较新。
Thanks! 谢谢!
I would suggest using Directory.GetFiles(path);
我建议使用Directory.GetFiles(path);
which gives you array of all files in directory. 它为您提供目录中所有文件的数组。 Using this, you don't need to worry about the name of the files to be image1,image2,.. 使用它,您不必担心文件的名称是image1,image2,..
string path = @"C:\Project\Images\";
string[] arr = Directory.GetFiles(path);
for (int i = 0 ; i < arr.Length ; i++)
{
Bitmap img = new Bitmap(Path.Combine(path , arr[i]));
DataGridView [2, i].Value = img;
}
something like: 就像是:
int numberOfImages = 10; // Replace 10 with number of your images
for(int i=0; i<numberOfImages; i++)
{
using(Bitmap img = new Bitmap(string.Format(@"C:\Project\Images\Image{0}.jpg", i+1)))
{
DataGridView [2, i].Value = img;
}
}
You need to learn concept of loops/iteration. 您需要学习循环/迭代的概念。 Check articles on iteration statements like for
and foreach
. 检查迭代语句中的文章,例如for
和foreach
。
for (int i = 0; i < 26; i++)
{
DataGridView [i, 1].Value =
new Bitmap(String.Format(@"C:\Project\Images\Image{0}.jpg", i));
}
Try this 尝试这个
ASPX Gridview code ASPX Gridview代码
<asp:GridView ID="grdImages" runat="server" BorderStyle="None" GridLines="None"
ShowHeader="false" AutoGenerateColumns="False" Width="240">
<Columns>
<asp:ImageField DataImageUrlField="FileName"></asp:ImageField>
</Columns>
</asp:GridView>
cs code cs代码
protected void Page_Load(object sender, EventArgs e)
{
string path = HttpContext.Current.Request.PhysicalApplicationPath + @"\Images\Temple";
// string path = @"D:\Blog\ImageShow\Images"; // This statement also valid
string[] extensions = { "*.jpg", "*.png", "*.bmp" };
List<string> files = new List<string>();
foreach (string filter in extensions)
{
files.AddRange(System.IO.Directory.GetFiles(path, filter));
}
IList<ImageFileInfo> imageFileList = new List<ImageFileInfo>();
foreach (string strFileName in files)
{
// Change the Absolute path to relative path of File Name and add to the List
imageFileList.Add(new ImageFileInfo { FileName = ResolveUrl(strFileName.Replace(Server.MapPath("/"), "~/")) });
}
grdImages.DataSource = imageFileList;
grdImages.DataBind();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.