简体   繁体   English

如何获得图像属性?

[英]How do I get image properties?

The most important property is the height and width of the image though other properties are needed as well. 尽管还需要其他属性,但最重要的属性是图像的高度和宽度。

I tried this code: 我尝试了这段代码:

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());
}

But what is GetFileProps ? 但是什么是GetFileProps It does not exist. 它不存在。

Here is GetFileProps : 这是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;
}

But this will require a few references to be added. 但这需要添加一些参考。 For more information, please check this forum from where I copied the method. 有关更多信息,请从复制方法的地方查看此论坛 And please start using Google! 并且请开始使用Google!

You can try this; 您可以尝试一下;

                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);

This works for any .bmp,.png,.jpg,.jpeg files but you can add more and here is sample project if needed. 这适用于任何.bmp,.png,.jpg,.jpeg文件,但您可以添加更多文件,如果需要, 这里是示例项目。

Hope it helps. 希望能帮助到你。

Modified to provide a more complete example: 进行修改以提供更完整的示例:

You're missing a method in your code. 您在代码中缺少一个方法。 You can recreate it yourself. 您可以自己重新创建它。

Use the System.Drawing namespace: 使用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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM