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

时间:2010-01-09 22:06:47

标签: c# .net visual-studio-2008 windows-mobile compact-framework

我正在尝试为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()

5 个答案:

答案 0 :(得分: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和Nick Guerrera的努力表示衷心的感谢!

答案 1 :(得分:1)

Controls.Add(pictureBoxBoard);Controls.Add(pictureBoxBlueCounter);吗?

编辑:

也许这是一个控制。手柄问题。在您pictureBoxBlueCounter.Handle之前设置pictureBoxBlueCounter.Imagethis.Handle之前,请尝试引用Add,看看是否存在问题。

EDIT2:

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

EDIT3:

您的设备是否有gdiplus.dll? Hint from here

EDIT4:

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

答案 2 :(得分:1)

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

嗯......有点

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

答案 3 :(得分:0)

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

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

答案 4 :(得分:0)

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

我观察到的一些事情。

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