views:

369

answers:

5

Hey Guys, Thanks for reading!

I am trying to develop an application for a windows mobile pda but 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 dont have any idea how to fix it!

does anyone have any idea how I can resolve this?

Thanks in advance, Exile!

EDIT: as requested here is the full TargetInvocationException info:

   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()
+1  A: 

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.

Qberticus
oops yeah your right, thats just a typo!it is pictureBoxBlueCounter in the code!I'm gonna fix that now!
Exile
I've made two edits that may or may not be of help. :)
Qberticus
Resources.Designer.cs seems to be ok (well all the file names match up and there's nothing that looks wrong)Also the referencing the pictureBoxBlueCounter.Handle doesn't seem to work!Thanks for your efforts tho!
Exile
what happens when you try to use the png non-programmaticly, i.e. in another form in the InitializeComponent() or constructor?
Qberticus
A: 

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.

Nick Guerrera
I dont think RAM is an issue here. The biggest .png I am trying to load is 7kb!!
Exile
Fair enough. I'm out of ideas in that case.
Nick Guerrera
A: 

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!

Exile
oh... no no. Don't do this. .CreateGraphics() is not the right way to do draw things. You will get visual glitches. See this link: http://www.bobpowell.net/creategraphics.htm
Quibblesome
+1  A: 

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, It was fun!

well... kinda

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)

Quibblesome
A: 

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.
Quibblesome