繁体   English   中英

在Visual Studio中,如何从项目Resources文件夹中加载.png图像?

[英]In Visual Studio how can I load a .png image from the project Resources folder?

我正在尝试开发Windows Mobile PDA的应用程序,但我遇到从资源文件夹中获取.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()

好吧,像往常一样,我从鼹鼠山上做了一座山!

这是我解决问题的方法:

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和Nick Guerrera的努力表示衷心的感谢!

应该Controls.Add(pictureBoxBoard); Controls.Add(pictureBoxBlueCounter);

编辑:

也许这是一个控制。手头问题。 尝试引用pictureBoxBlueCounter.Handle您之前设置的pictureBoxBlueCounter.Imagethis.Handle你之前Add ,看看是否这就是问题所在。

EDIT2:

查看Resources.Designer.cs文件,确保一切正常。 也许文件名已更改,并且未反映在Resources.resx中

EDIT3:

你的设备有gdiplus.dll吗? 提示来自这里

EDIT4:

你是在UI线程上做这个吗? 如果没有,那可能就是问题所在。

你以前处理过这个资源吗? 如果从这种资源文件中处理位图,则会在内部异常上获得相同类型的堆栈跟踪。 我今天这样做了,很有趣!

嗯...有点儿

顺便说一句,如果在动态调用函数的反射期间发生异常,则会得到TargetInvocationException。 您将注意到ResourceReader.CreateResource()执行此操作。 它包装了原始异常(通过.InnerException属性获取)

我查看了堆栈,它显然是.NET Compact Framework特定的代码,因为最后几个方法在标准程序集中找不到。 我从未使用过Windows Mobile上的.NET CF,但我的猜测是PDA在加载Bitmap时内存不足。

尝试使用一个很小的.png文件,看看是否有所作为。

本周我还有另外一个。 没有想法是什么造成了它,没有像我匆忙那样深入挖掘它。 我也得到了神秘的“异常”消息。

我观察到的一些事情。

  • 仅在进行附加调试时发生。
  • 始终是相同的图像(640x310)
  • 图像是一个像其他人一样的.png(另一个是640x310,很好)。
  • 将图像转换为jpeg似乎会使问题消失。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM