如何获取图像列表中图像的实际大小
How to get the real size of the image in image list
我有一段代码可以显示特定尺寸的图片。我想获取图片的真实尺寸,并希望以真实尺寸显示这些图片。
谁能帮我解决这个问题。
这是我用来获取图片的代码。
private void btnZoom_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
imageList1.Images.Clear();
string[] pics = System.IO.Directory.GetFiles("TestFolder//");
listView1.View = View.SmallIcon;
listView1.SmallImageList = imageList1;
imageList1.ImageSize = new Size(64, 64);
foreach (string pic in pics)
{
imageList1.Images.Add(Image.FromFile(pic));
}
for (int j = 0; j < imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
listView1.Items.Add(item);
}
}
这是一个 Windows 表单应用程序。
这是另一种方法...使用 Tuple<Image, String>
.
将图像及其完整路径文件名存储在 ListViewItem 的 Tag
属性 中
类似于:
private void btnZoom_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
imageList1.Images.Clear();
string[] pics = System.IO.Directory.GetFiles(@"C:\Users\mikes\Pictures\Facebook\Backyard Wildlife"); //"TestFolder//");
listView1.View = View.SmallIcon;
listView1.SmallImageList = imageList1;
imageList1.ImageSize = new Size(64, 64);
for(int i = 0; i < pics.Length; i++)
{
Image img;
using (FileStream fs = new FileStream(pics[i], FileMode.Open))
{
try
{
img = Image.FromStream(fs);
imageList1.Images.Add(img);
ListViewItem item = new ListViewItem();
item.ImageIndex = imageList1.Images.Count - 1;
item.Text = System.IO.Path.GetFileNameWithoutExtension(pics[i]);
item.Tag = new Tuple<Image, String>(img, pics[i]);
listView1.Items.Add(item);
}
catch (Exception ex) { };
}
}
}
private void btnView_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
Tuple<Image, String> data = (Tuple < Image, String >)item.Tag;
label1.Text = data.Item2;
pictureBox1.Image = data.Item1;
Size sz = data.Item1.Size;
label2.Text = sz.ToString();
}
}
我有一段代码可以显示特定尺寸的图片。我想获取图片的真实尺寸,并希望以真实尺寸显示这些图片。
谁能帮我解决这个问题。 这是我用来获取图片的代码。
private void btnZoom_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
imageList1.Images.Clear();
string[] pics = System.IO.Directory.GetFiles("TestFolder//");
listView1.View = View.SmallIcon;
listView1.SmallImageList = imageList1;
imageList1.ImageSize = new Size(64, 64);
foreach (string pic in pics)
{
imageList1.Images.Add(Image.FromFile(pic));
}
for (int j = 0; j < imageList1.Images.Count; j++)
{
ListViewItem item = new ListViewItem();
item.ImageIndex = j;
listView1.Items.Add(item);
}
}
这是一个 Windows 表单应用程序。
这是另一种方法...使用 Tuple<Image, String>
.
Tag
属性 中
类似于:
private void btnZoom_Click(object sender, EventArgs e)
{
listView1.Items.Clear();
imageList1.Images.Clear();
string[] pics = System.IO.Directory.GetFiles(@"C:\Users\mikes\Pictures\Facebook\Backyard Wildlife"); //"TestFolder//");
listView1.View = View.SmallIcon;
listView1.SmallImageList = imageList1;
imageList1.ImageSize = new Size(64, 64);
for(int i = 0; i < pics.Length; i++)
{
Image img;
using (FileStream fs = new FileStream(pics[i], FileMode.Open))
{
try
{
img = Image.FromStream(fs);
imageList1.Images.Add(img);
ListViewItem item = new ListViewItem();
item.ImageIndex = imageList1.Images.Count - 1;
item.Text = System.IO.Path.GetFileNameWithoutExtension(pics[i]);
item.Tag = new Tuple<Image, String>(img, pics[i]);
listView1.Items.Add(item);
}
catch (Exception ex) { };
}
}
}
private void btnView_Click(object sender, EventArgs e)
{
if (listView1.SelectedItems.Count > 0)
{
ListViewItem item = listView1.SelectedItems[0];
Tuple<Image, String> data = (Tuple < Image, String >)item.Tag;
label1.Text = data.Item2;
pictureBox1.Image = data.Item1;
Size sz = data.Item1.Size;
label2.Text = sz.ToString();
}
}