如何获取图像属性?

时间:2013-09-16 17:08:24

标签: c# winforms

最重要的属性是图像的高度和宽度,但也需要其他属性。

我试过这段代码:

private void getImageProperties()
{
  OpenFileDialog openFileDialog = new OpenFileDialog();
  openFileDialog.ShowDialog();
  Dictionary<int, KeyValuePair<string, string>> fileProps = 
    GetFileProps(openFileDialog.FileName);

  foreach (KeyValuePair<int, KeyValuePair<string, string>> kv in fileProps)
    Console.WriteLine(kv.ToString());
}

但是GetFileProps是什么?它不存在。

3 个答案:

答案 0 :(得分:3)

以下是GetFileProps

Dictionary<int, KeyValuePair<string, string>> GetFileProps(string filename)
{
  Shell shl = new ShellClass();
  Folder fldr = shl.NameSpace(Path.GetDirectoryName(filename));
  FolderItem itm = fldr.ParseName(Path.GetFileName(filename));
  Dictionary<int, KeyValuePair<string, string>> fileProps = new Dictionary<int, KeyValuePair<string, string>>();
  for (int i = 0; i < 100; i++)
  {
    string propValue = fldr.GetDetailsOf(itm, i);
    if (propValue != "")
    {
      fileProps.Add(i, new KeyValuePair<string, string>(fldr.GetDetailsOf(null, i), propValue));
    }
  }
  return fileProps;
}

但这需要添加一些参考。有关详细信息,请查看我复制方法的this forum。请开始使用Google!

答案 1 :(得分:2)

你可以试试这个;

                string path = "Path of image";
                Bitmap bmp = new Bitmap(path);
                StringBuilder sb = new StringBuilder();
                FileInfo fi = new FileInfo(path);
                sb.AppendLine("Name : " + fi.Name);
                sb.AppendLine("Width : " + bmp.Width);
                sb.AppendLine("Height : " + bmp.Height);
                sb.AppendLine("Horizontal Resolution : " + bmp.HorizontalResolution);
                sb.AppendLine("Vertical Resolution : " + bmp.VerticalResolution);
                string type = "";
                if (fi.Extension == ".bmp")
                {
                    type = "Bitmap Image";
                }
                else if (fi.Extension == ".jpg" || fi.Extension == ".jpeg")
                {
                    type = "Joint Photographic Experts Group Image File";
                }
                else if (fi.Extension == ".png")
                {
                    type = "Portable Network Graphic Image";
                }
                sb.AppendLine("Type : " + type);
                bmp.Dispose();
                MessageBox.Show(sb.ToString(), path, MessageBoxButtons.OK, MessageBoxIcon.Information);

这适用于任何.bmp,.png,.jpg,.jpeg文件,但您可以添加更多文件,here是示例项目(如果需要)。

希望它有所帮助。

答案 2 :(得分:1)

已修改以提供更完整的示例:

您错过了代码中的方法。您可以自己重新创建它。

使用System.Drawing命名空间:

Dictionary<int, KeyValuePair<string, string>> GetFileProps(string path)
{
    System.Drawing.Image image = System.Drawing.Image.FromFile(path);

    var dictionary = new Dictionary<int, KeyValuePair<string, string>>();
    dictionary.Add(1, new KeyValuePair<string, string>("Width", image.Width.ToString()));
    dictionary.Add(2, new KeyValuePair<string, string>("Height", image.Height.ToString()));

    //Implement the rest of the properties you deem important here.

    return dictionary;
}