我正在尝试为 Windows Mobile 开发一个应用程序PDA ,但我在从资源文件夹获取 .png 图像时遇到问题。
我在项目 Resources 文件夹中有许多图像,我想做的就是以编程方式(即仅使用代码)使用项目 Resources 文件夹中的背景图像绘制图像框。
例如:
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 was unhandled”错误,我不知道如何修复它!
我该如何解决?
这里是完整的 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()
最佳答案
好吧,像往常一样,我小题大做了!
这是我解决问题的方法:
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)
{
}
事实证明我需要一个 Canvas (就像我在 J2ME 中那样),但与 J2ME 不同的是,这个 Canvas 实际上不需要做任何事情。
我不知道为什么会这样,但确实如此!
此外,我还要非常感谢 Qberticus 和 Nick Guerrera 所做的努力!
关于c# - 在 Visual Studio 中,如何从项目资源文件夹加载 .png 图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2035154/