简体   繁体   中英

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 ? It does not exist.

Here is 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!

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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