2013-09-16 74 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!