簡體   English   中英

如何獲得圖像屬性?

[英]How do I get image properties?

盡管還需要其他屬性,但最重要的屬性是圖像的高度和寬度。

我嘗試了這段代碼:

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 它不存在。

這是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;
}

但這需要添加一些參考。 有關更多信息,請從復制方法的地方查看此論壇 並且請開始使用Google!

您可以嘗試一下;

                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文件,但您可以添加更多文件,如果需要, 這里是示例項目。

希望能幫助到你。

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

您在代碼中缺少一個方法。 您可以自己重新創建它。

使用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