我想開發一個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()
你可以發佈完整的'exception.ToString()'嗎? TargetInvocationException的內部異常很可能表明真正的問題... –
感謝您花時間看我的問題暱稱! 我現在添加了完整的異常信息! – Exile
你在UI線程上做這個嗎? – Qberticus