Exception when trying to draw a bitmap to an image

The following line generates an argument exception.


compassImage.Bitmap.DrawImage(0, 0, compassBitmap, 0, 0, compassBitmap.Width, compassBitmap.Height);

This is the exception:


 #### Exception System.ArgumentException - 0xfd000000 (1) ####
 #### Message:
 #### Microsoft.SPOT.Bitmap::DrawImage [IP: 0000] ####
 #### Microsoft.SPOT.Bitmap::DrawImage [IP: 0014] ####
 #### Tracker.Program::DrawCompass [IP: 008b] ####
 #### Tracker.Program::UpdateDisplay [IP: 002c] ####
 #### Tracker.Program::Main [IP: 006c] ####
A first chance exception of type 'System.ArgumentException' occurred in Microsoft.SPOT.Graphics.dll

But this one works


compassImage.Bitmap.DrawImage(0, 0, compassCopy, 0, 0, compassCopy.Width, compassCopy.Height);

compassCopy is created in the function that draws the compass and compassBitmap is loaded from the resources when the screen is created.

If I simply use the following it works to show the bitmap in the image but I need the use the Draw call so that I can render one image (a pointer) over the other


compassImage.Bitmap = compassBitmap;

It would seem that passing a globally declared static Bitmap to the Draw function is wrong. When I create the Bitmap locally on the fly it works.


using(Bitmap newBitmap = new Bitmap(Resources.GetBytes(Resources.BinaryResources.compass), Bitmap.BitmapImageType.Bmp)) {
    compassImage.Bitmap.DrawImage(0, 0, (Bitmap) compassBitmap, 0, 0, compassBitmap.Width, compassBitmap.Height);
    compassImage.Render();
}

This is not ideal as it means I need to create it each time I want to use it and it doesn’t change. I just want to overwrite what is in the current image. :frowning:

I’ve tried to create a class of bitmaps but this doesn’t work either :frowning:

Could it be because of some kind of recursivity when using the same global variable ?
You ask to draw a bitmap on the same bitmap while it is “building” its content. (if I understand correctly).

When you declare another global variable or a local one, a “copy” is made before and you are using another location in memory, hence the correct behaviour.

But maybe I am wrong, as I do not understand clearly what you want to achieve, here.

1 Like

Well spotted. Often a second pair of eyes is all it needs.

When I created the window I also applied the bitmap direct to it with image.Bitmap = compassBitmap so this would create recursion.

Cheers that just saved me 300ms in the drawing loop.