2017-02-23 63 views
0

MSDN ExtractMetaDatamsdn示例ExtractMetaData

試圖恢復速度。非常抱歉,如果這是一個非常簡單的問題。

我從MSDN創建了PictureViewer示例,並試圖添加一些附加功能。具體來說,我試圖從圖像文件中讀取和顯示特定的元數據。

在試圖理解這個例子中,似乎我需要鏈接一個表單控件來調用示例代碼。但我不明白這個電話應該是什麼樣子。將

ExtractMetadata(e); in an event handler giving me a error 

Argurment 1:無法從 'System.EventArgs' 轉換爲 'System.Windows.Forms.PaintEventArgs'

我失去了一個 'this' 指針的東西?

///////////////////
我想我需要更多的信息。如果您查看註釋頂部的LINK ExtractMetadata是從圖像文件調用GET EXIF數據(可交換圖像文件),但它需要PaintEventArgs e,它是Paint Event處理程序的參數,因此它需要系統。 Drawing.Imaging命名空間。

這裏是代碼的例子提供,我試圖執行:

private void ExtractMetaData(PaintEventArgs e) 
    { 
     try 
     { 
      // Create an Image object. 
      Image theImage = new Bitmap("C:\\Users\\DadPC\\Desktop\\testrunfiles\\fakePhoto.jpg"); 

      // Get the PropertyItems property from image. 
      PropertyItem[] propItems = theImage.PropertyItems; 

      // Set up the display. 
      Font font1 = new Font("Arial", 10); 
      SolidBrush blackBrush = new SolidBrush(Color.Black); 
      int X = 0; 
      int Y = 0; 

      // For each PropertyItem in the array, display the id, 
      // type, and length. 
      int count = 0; 
      foreach (PropertyItem propItem in propItems) 
      { 
       e.Graphics.DrawString("Property Item " + 
        count.ToString(), font1, blackBrush, X, Y); 
       Y += font1.Height; 

       e.Graphics.DrawString(" ID: 0x" + 
        propItem.Id.ToString("x"), font1, blackBrush, X, Y); 
       Y += font1.Height; 

       e.Graphics.DrawString(" type: " + 
        propItem.Type.ToString(), font1, blackBrush, X, Y); 
       Y += font1.Height; 

       e.Graphics.DrawString(" length: " + 
        propItem.Len.ToString() + 
        " bytes", font1, blackBrush, X, Y); 
       Y += font1.Height; 
       count += 1; 
      } 
      font1.Dispose(); 
     } 
     catch (Exception) 
     { 
      MessageBox.Show("There was an error." + 
       "Make sure the path to the image file is valid."); 
     } 
    } 

而在我的表單應用程序我一直在試圖找出如何調用該函數

private void metadataButton_Click(object sender, EventArgs e) 
    { 
     ExtractMetaData(e); 
    } 

但是,這是我沒有成功地弄清楚它期望我通過什麼,包括什麼都不嘗試ExtractMetaData();

長期來說,我想讀取和正確的圖像文件的特定元數據,但我使用MSDN的這個例子開始。

Link to Metadata reference table

+0

能告訴你從中調用處理程序'ExtractMetadata'? –

回答

0

此代碼是爲了從一個Paint事件處理程序調用。它使用當時可用的圖形對象直接繪製到顯示器上。例如:

private void Form1_Paint(object sender, PaintEventArgs e) 
{ 
    ExtractMetadata(e); 
} 

事件處理程序通常在Visual Studio Designer中添加。但它會將此行添加到Designer.cs文件:

private void InitializeComponent() 
{ 
    ... 
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint); 
    ... 
} 

您也可以在構造函數中手動添加此行的代碼(InitializeComponent調用後)