Exile
Exile

Reputation: 9603

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

I am trying to develop an application for a Windows Mobile PDA, but I am having a problem with getting .png images from a resource folder.

I have a number of images in the project Resources folder, and all I want to do draw an image box programmatically (i.e just using code) with a background image from the project Resources folder.

For example:

  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);

As it currently stands the above code give me an 'TargetInvocationException was unhandled' error, and I don't have any idea how to fix it!

How can I resolve this?

Here is the full TargetInvocationException information:

  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()

Upvotes: 3

Views: 4576

Answers (5)

Quibblesome
Quibblesome

Reputation: 25409

Did you previously dispose of the resource? You get the same kind of stacktrace on the inner exception if you dispose of a bitmap from this kind of resources file. I did this today, and it was fun!

Well... kind of

BTW, you get a TargetInvocationException if an exception occurs during reflection that dynamically invokes a function. You will note that ResourceReader.CreateResource() does this. It wraps the original exception (gettable via the .InnerException property)

Upvotes: 1

Exile
Exile

Reputation: 9603

OK, as usual I have made a mountain out of a mole hill!

Here's how I solved my problem:

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)
{

}

It turns out I needed a paint canvas (like I would in J2ME), but unlike J2ME this paint canvas doesn't actually have to do anything.

I have no idea why this works, but it does!

Also I would just like to say a big thanks to Qberticus and Nick Guerrera for your efforts!

Upvotes: 2

Quibblesome
Quibblesome

Reputation: 25409

I had another one of these this week. NO IDEA what caused it, didn't dig into it as I was in a rush. I also got the cryptic "Exception" message.

Some things I observed.

  • Only happened when doing attached debugging.
  • Always the same image (640x310)
  • Image was a .png like the rest (another was 640x310 that was fine).
  • Converting the image to a jpeg seemed to make the problem go away.

Upvotes: 0

Rich Schuler
Rich Schuler

Reputation: 41972

Should Controls.Add(pictureBoxBoard); be Controls.Add(pictureBoxBlueCounter); ?

EDIT:

Maybe it's a control.Handle issue. Try referencing the pictureBoxBlueCounter.Handle before you set the pictureBoxBlueCounter.Image and the this.Handle before you Add and see if that's the problem.

EDIT2:

Check out the Resources.Designer.cs file and make sure everything is ok there. Maybe the filename changed and is not reflected in the Resources.resx

EDIT3:

Does your device have a gdiplus.dll ? Hint from here

EDIT4:

Are you doing this on the UI thread? If not, that may be the issue.

Upvotes: 1

Nick Guerrera
Nick Guerrera

Reputation: 2549

I looked at the stack and it's clearly .NET Compact Framework specific code as the last few methods are nowhere to be found in the standard assemblies. I have never worked with .NET CF on Windows Mobile, but my guess would be that the PDA ran out of memory while loading the Bitmap.

Try using a tiny .png file and see if that makes a difference.

Upvotes: 0

Related Questions