Symbian C ++ - Loading and displaying an image from a .mbm file
I have a .mbm file that I copy to my device using this line in the .pkg file
"$(EPOCROOT)epoc32\data\z\resource\apps\MyApp.mbm" -"!:\resource\apps\MyApp.mbm"
Then in the draw function of my container, I do this.
_LIT(KMBMFile , "\\resource\\apps\\MyApp.mbm" );
CFbsBitmap* iBitmap;
iBitmap->Load(KMBMFile, 0);
gc.BitBlt(Rect().iTl, iBitmap);
However, the line iBitmap->Load(KMBMFile, 0);
calls KERN-EXEC: 0 PANIC
"This panic occurs when the kernel cannot find an object in the object index for the current process or current thread using the specified object index (raw descriptor number).
Can anyone identify where I am going wrong?
Thank!
source to share
You have dereferenced an uninitialized pointer, you can also use this:
// remember to include the EIK environemnt include file
#include <eikenv.h>
_LIT(KMBMFile , "\\resource\\apps\\MyApp.mbm" );
CFbsBitmap* iBitmap;
iBitmap = iEikonEnv->CreateBitmapL( KMBMFile, 0 );
gc.BitBlt( Rect().iTl, iBitmap );
source to share
I solved this problem, so I will post an answer here for future users.
Create MBM file in your MMP file using this snippet, for example
START BITMAP MyApp.mbm
HEADER
TARGETPATH \resource\apps
SOURCEPATH ..\gfx
SOURCE c24 background.bmp
END
make sure your .bmp images are saved in 32-bit format from photoshop or similar
Then make sure your MBM file is copied to your device in your PKG file
"$(EPOCROOT)epoc32\data\z\resource\apps\MyApp.mbm" -"!:\resource\apps\MyApp.mbm"
Then, in your container's paint function, use code like this.
_LIT(KMBMFile , "C:\\RESOURCE\\APPS\\MyApp.mbm" );
CFbsBitmap* iBitmap = new (ELeave) CFbsBitmap;
TInt retval = iBitmap->Load(KMBMFile, 0);
gc.BitBlt(Rect().iTl, iBitmap);
This will paint the bitmap at the top left of the screen (useful for a background image).
source to share
CCoeControl::Draw()
the code shouldn't fail and certainly shouldn't go away (it has no end ... L
). In the above code snippet, there are two potentially error-causing calls - constructor iBitmap
and iBitmap->Load()
. The bitmap must be preselected, not selected in Draw()
- if bad behavior occurs.
Also, by convention, only class member variables start with 'i', which iBitmap
is not above.
See Symbian coding standards for details .
source to share
You should definitely not create an iBitmap in the :: Draw function as it might go away. The best way to do this is in ConstructL CoeControl. In theory, a :: Load call can be handled in :: Draw, as it can fail for a variety of reasons, not all of which can be fatal. You could just as easily do this by creating a Control, although it might be better to think about it a bit. I would say that if the control basically exists to contain the bitmap, then you should do it in ConstructL. If there is something the control does, then you can render it in :: Draw.