【问题标题】:In Visual Studio how can I load a .png image from the project Resources folder?在 Visual Studio 中,如何从项目资源文件夹中加载 .png 图像?
【发布时间】:2011-01-03 08:34:53
【问题描述】:

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

【问题讨论】:

  • 你能发布完整的exception.ToString()吗? TargetInvocationException 的内部异常很可能表明真正的问题......
  • 感谢您抽出宝贵时间查看我的问题 nick!我现在已经添加了完整的异常信息!
  • 你是在 UI 线程上做这个吗?

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


【解决方案1】:

好的,像往常一样,我已经从鼹鼠山变成了一座山!

这是我解决问题的方法:

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 的努力!

【讨论】:

  • 哦...不,不。不要这样做。 .CreateGraphics() 不是绘制事物的正确方法。你会得到视觉故障。见此链接:bobpowell.net/creategraphics.htm
【解决方案2】:

Controls.Add(pictureBoxBoard); 应该是Controls.Add(pictureBoxBlueCounter); 吗?

编辑:

可能是 control.Handle 问题。尝试在设置pictureBoxBlueCounter.Image 和this.Handle 之前引用pictureBoxBlueCounter.Handle,然后再设置Add,看看是否是问题所在。

EDIT2:

查看 Resources.Designer.cs 文件并确保那里一切正常。可能文件名已更改且未反映在 Resources.resx 中

EDIT3:

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

EDIT4:

您是否在 UI 线程上执行此操作?如果不是,那可能就是问题所在。

【讨论】:

  • 哎呀你的权利,那只是一个错字!它是代码中的pictureBoxBlueCounter!我现在要解决这个问题!
  • Resources.Designer.cs 似乎没问题(所有文件名都匹配,并且没有任何错误)而且引用 pictureBoxBlueCounter.Handle 似乎不起作用!感谢您的努力!
  • 当您尝试以非编程方式使用 png 时会发生什么,即在 InitializeComponent() 或构造函数中以另一种形式使用?
【解决方案3】:

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

嗯……有点

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

【讨论】:

    【解决方案4】:

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

    尝试使用一个很小的 ​​.png 文件,看看是否会有所不同。

    【讨论】:

    • 我认为这里的 RAM 不是问题。我尝试加载的最大 .png 为 7kb!
    • 很公平。在那种情况下,我没有想法。
    【解决方案5】:

    这周我又吃了一个。不知道是什么原因造成的,因为我很着急,所以没有深入研究它。我还收到了神秘的“异常”消息。

    我观察到的一些事情。

    • 仅在进行附加调试时发生。
    • 始终使用相同的图像 (640x310)
    • 图片是 .png 和其他图片一样(另一个是 640x310,这很好)。
    • 将图像转换为 jpeg 似乎可以解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-27
      • 2013-03-12
      • 2015-09-12
      • 2012-03-08
      • 1970-01-01
      • 1970-01-01
      • 2011-03-24
      • 2022-08-23
      相关资源
      最近更新 更多