2010-01-09 108 views
3

我想开发一个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() 
+0

你可以发布完整的'exception.ToString()'吗? TargetInvocationException的内部异常很可能表明真正的问题... –

+0

感谢您花时间看我的问题昵称! 我现在添加了完整的异常信息! – Exile

+0

你在UI线程上做这个吗? – Qberticus

回答

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和尼克•格雷拉你的努力!

+2

哦......不,不。不要这样做。 .CreateGraphics()不是绘制事物的正确方法。你会得到视觉上的毛刺。看到这个链接:http://www.bobpowell.net/creategraphics.htm – Quibblesome

1

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

编辑:

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

EDIT2:

退房的Resources.Designer.cs文件,并确保一切正常存在。也许文件名改变,而不是体现在Resources.resx

EDIT3:

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

EDIT4:

你在UI线程这样做呢?如果不是,那可能是问题。

+0

哎呀呀你的权利,这只是一个错字! 它是pictureBoxBlueCounter中的代码! 我现在要解决这个问题! – Exile

+0

我做了两个编辑,可能或可能不会有帮助。 :) – Qberticus

+0

Resources.Designer.cs似乎是好的(以及所有的文件名称匹配,并没有什么看起来不对) 此外引用pictureBoxBlueCounter.Handle似乎并没有工作! 谢谢你的努力寿! – Exile

0

我看着堆栈,它显然是.NET Compact Framework的具体代码的最后几个方法是无处可在标准组件中找到。我从来没有在Windows Mobile上使用过.NET CF,但我的猜测可能是PDA在加载位图时耗尽内存。

尝试使用一个微小的PNG文件,看看是否有差别。

+1

我不认为RAM是一个问题在这里。我想加载的最大的.png是7kb! – Exile

+0

够公平的。在这种情况下,我没有想法。 –

1

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

嗯...那种

顺便说一句,你会得到一个TargetInvocationException如果反射动态调用函数时发生异常。你会注意到ResourceReader.CreateResource()这样做。它包装了原始异常(通过.InnerException属性获取)

0

本周我还有其中一个。没有想法是什么造成的,没有深入研究,因为我很急。我也得到了神秘的“例外”消息。

我观察到的一些事情。

  • 仅在进行连接调试时发生。
  • 总是一样的图像(640x310)
  • 图像是一个.png像其余的(另一个是640x310很好)。
  • 将图像转换为JPEG格式似乎会使问题消失。
相关问题