2013-09-16 45 views
1

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

我试过这段代码:

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

+1

图像也可以有EXIF数据。你在找那个吗? – StingyJack

+2

'GetFileProps'是你错过了代码的一些方法,请尝试询问编写此代码的人? –

回答

1

你可以试试这个;

   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如果需要的是示例项目。

希望它有帮助。

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

我想我已经为此付出了代价。我编辑我的答案是更完整的。 – Khan

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!