2010-01-09 103 views
3

我想開發一個Windows MobilePDA的應用程序,但我有從資源文件夾入門png格式圖像的問題。在Visual Studio中,如何從項目資源文件夾加載.png圖像?

我公司擁有一批在項目資源文件夾中的圖片,以及所有我想要做的編程方式繪製圖像框(即只用代碼),從項目資源文件夾的背景圖像。

例如:

PictureBox pictureBoxBlueCounter = new PictureBox(); 

    //pictureBoxBlueCounter = new System.Windows.Forms.PictureBox(); 
    pictureBoxBlueCounter.Image = global::StrikeOutMobile.Properties.Resources.counter_square_blue; 
    pictureBoxBlueCounter.Location = new System.Drawing.Point(30, 30); 
    pictureBoxBlueCounter.Name = "pictureBoxblueCounter"; 
    pictureBoxBlueCounter.Size = new System.Drawing.Size(240, 210); 
    pictureBoxBlueCounter.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage; 
    Controls.Add(pictureBoxBlueCounter); 

因爲它目前爲上面的代碼給我一個「TargetInvocationException是未處理」的錯誤,我沒有任何想法如何解決它!

我怎樣才能解決這個問題?

以下是完整的TargetInvocationException信息:

System.Reflection.TargetInvocationException was unhandled 
    Message="TargetInvocationException" 
    StackTrace: 
     at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
     at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) 
     at System.Resources.ResourceReader.CreateResource(Type objType, Type[] ctorParamTypes, Object[] ctorParameters) 
     at System.Resources.ResourceReader.LoadBitmap(Int32 typeIndex) 
     at System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) 
     at System.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode) 
     at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase) 
     at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture) 
     at StrikeOutMobile.Properties.Resources.get_counter_square_blue() 
     at StrikeOutMobile.FormGameBoard.drawBlue() 
     at StrikeOutMobile.FormGameBoard.menuItemPosition1_Click(Object sender, EventArgs e) 
     at System.Windows.Forms.MenuItem.OnClick(EventArgs e) 
     at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam) 
     at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam) 
     at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) 
     at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal) 
     at System.Windows.Forms.Form.ShowDialog() 
     at StrikeOutMobile.Main.menuItem1_Click(Object sender, EventArgs e) 
     at System.Windows.Forms.MenuItem.OnClick(EventArgs e) 
     at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam) 
     at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam) 
     at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) 
     at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) 
     at System.Windows.Forms.Application.Run(Form fm) 
     at StrikeOutMobile.Program.Main() 

    InnerException: 
     Message="Exception" 
     StackTrace: 
      at Microsoft.AGL.Common.MISC.HandleAr(PAL_ERROR ar) 
      at System.Drawing.Bitmap._InitFromMemoryStream(MemoryStream mstream) 
      at System.Drawing.Bitmap..ctor(Stream stream) 
      at System.Reflection.RuntimeConstructorInfo.InternalInvoke(RuntimeConstructorInfo rtci, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean isBinderDefault, Assembly caller, Boolean verifyAccess, StackCrawlMark& stackMark) 
      at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) 
      at System.Reflection.ConstructorInfo.Invoke(Object[] parameters) 
      at System.Resources.ResourceReader.CreateResource(Type objType, Type[] ctorParamTypes, Object[] ctorParameters) 
      at System.Resources.ResourceReader.LoadBitmap(Int32 typeIndex) 
      at System.Resources.ResourceReader.LoadObjectV2(Int32 pos, ResourceTypeCode& typeCode) 
      at System.Resources.ResourceReader.LoadObject(Int32 pos, ResourceTypeCode& typeCode) 
      at System.Resources.RuntimeResourceSet.GetObject(String key, Boolean ignoreCase) 
      at System.Resources.ResourceManager.GetObject(String name, CultureInfo culture) 
      at StrikeOutMobile.Properties.Resources.get_counter_square_blue() 
      at StrikeOutMobile.FormGameBoard.drawBlue() 
      at StrikeOutMobile.FormGameBoard.menuItemPosition1_Click(Object sender, EventArgs e) 
      at System.Windows.Forms.MenuItem.OnClick(EventArgs e) 
      at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam) 
      at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam) 
      at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) 
      at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal) 
      at System.Windows.Forms.Form.ShowDialog() 
      at StrikeOutMobile.Main.menuItem1_Click(Object sender, EventArgs e) 
      at System.Windows.Forms.MenuItem.OnClick(EventArgs e) 
      at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam) 
      at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam) 
      at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam) 
      at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain) 
      at System.Windows.Forms.Application.Run(Form fm) 
      at StrikeOutMobile.Program.Main() 
+0

你可以發佈完整的'exception.ToString()'嗎? TargetInvocationException的內部異常很可能表明真正的問題... –

+0

感謝您花時間看我的問題暱稱! 我現在添加了完整的異常信息! – Exile

+0

你在UI線程上做這個嗎? – Qberticus

回答

2

好吧,像往常一樣,我已經從一個鼴鼠山上取得了一座山!

以下是我解決我的問題:

private void menuItemPosition1_Click(object sender, EventArgs e) 
{ 
    Graphics graphicsCanvas = this.pictureBoxBoard.CreateGraphics(); 
    graphicsCanvas.DrawImage(global::StrikeOutMobile.Properties.Resources.counter_square_blue, 60, 60); 
} 

private void pictureBoxBoard_Paint(object sender, PaintEventArgs e) 
{ 

} 

原來我需要用畫布和油彩(就像我會在J2ME),但與J2ME這種漆的畫布實際上並沒有做任何事情。

我不知道爲什麼這個工程,但它!

此外,我只想說非常感謝Qberticus和尼克•格雷拉你的努力!

+2

哦......不,不。不要這樣做。 .CreateGraphics()不是繪製事物的正確方法。你會得到視覺上的毛刺。看到這個鏈接:http://www.bobpowell.net/creategraphics.htm – Quibblesome

1

應該Controls.Add(pictureBoxBoard);Controls.Add(pictureBoxBlueCounter);

編輯:

也許這是一個control.Handle問題。嘗試引用pictureBoxBlueCounter.Handle你之前,你Add設置pictureBoxBlueCounter.Imagethis.Handle之前,看看是否這就是問題所在。

EDIT2:

退房的Resources.Designer.cs文件,並確保一切正常存在。也許文件名改變,而不是體現在Resources.resx

EDIT3:

您的設備是否有gdiplus.dll的? Hint from here

EDIT4:

你在UI線程這樣做呢?如果不是,那可能是問題。

+0

哎呀呀你的權利,這只是一個錯字! 它是pictureBoxBlueCounter中的代碼! 我現在要解決這個問題! – Exile

+0

我做了兩個編輯,可能或可能不會有幫助。 :) – Qberticus

+0

Resources.Designer.cs似乎是好的(以及所有的文件名稱匹配,並沒有什麼看起來不對) 此外引用pictureBoxBlueCounter.Handle似乎並沒有工作! 謝謝你的努力壽! – Exile

0

我看着堆棧,它顯然是.NET Compact Framework的具體代碼的最後幾個方法是無處可在標準組件中找到。我從來沒有在Windows Mobile上使用過.NET CF,但我的猜測可能是PDA在加載位圖時耗盡內存。

嘗試使用一個微小的PNG文件,看看是否有差別。

+1

我不認爲RAM是一個問題在這裏。我想加載的最大的.png是7kb! – Exile

+0

夠公平的。在這種情況下,我沒有想法。 –

1

你以前是否處理過資源?如果您從這種資源文件中處理位圖,則會在內部異常上獲得相同類型的堆棧跟蹤。我今天做到了,很有趣!

嗯...那種

順便說一句,你會得到一個TargetInvocationException如果反射動態調用函數時發生異常。你會注意到ResourceReader.CreateResource()這樣做。它包裝了原始異常(通過.InnerException屬性獲取)

0

本週我還有其中一個。沒有想法是什麼造成的,沒有深入研究,因爲我很急。我也得到了神祕的「例外」消息。

我觀察到的一些事情。

  • 僅在進行連接調試時發生。
  • 總是一樣的圖像(640x310)
  • 圖像是一個.png像其餘的(另一個是640x310很好)。
  • 將圖像轉換爲JPEG格式似乎會使問題消失。
相關問題