Loading images as embedded resource: How to optimize

Hi!!!

I have a pda application that uses a dll that I made.

That dll has some images that i load as an embedded resource, when i run the application, it takes some time t load all images and i can see in the emulator the images loading... What can I do to make the images load at the same time?
Thanks in advance

[320 byte] By [Mónica] at [2008-1-12]
# 1
Can you give us some details on the characteristics images you're loading? In particular,
  • Dimensions
  • Color depth
  • File size
  • Image format
NeilCowburn-MVP at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 2
Hi, there and thanks for replying..
I've already tryed it on a pda and it works fine, i have no problem loading the images, the Emulator is not as fast (didn't know that, my first project)...
I was wondering if thre is another way to laod the images:
This is the one I use :
I have a panel that contains the images (embedded resource) and this is how get them to show in the panel:
this.pictureBox0d.Image = new Bitmap( assembly.GetManifestResourceStream(this.GetType(),
"Images.0d.gif") );
The problem is that this doesn't work in all pdas
so I've tryed this way :
this.pictureBox0.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox10.Image")));
But this gives an exception :
An unhandled exception of type 'System.TypeLoadException' occurred in mscorlib.dll
Additional information: System.CodeDom.MemberAttributes, System, Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
The project is a Class Library. If I use the statement in a windows application it works fine.
Whats the problem here?
Thanks in advance
Mónica
Mónica at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 3
The first method you state is almost correct. Instead of doing this:

this.pictureBox0d.Image = new Bitmap(asm.GetManifestResourceStream(this.GetType(), "Images.0d.gif"));

try this:

Assembly asm = Assembly.GetExecutingAssembly();
this.pictureBox0d.Image = new Bitmap(asm.GetManifestResourceStream("Images.0d.gif"));


Also, are you using Visual Studio .NET 2003 or Visual Studio 2005? The emulators that came with VS .NET 2003 are notoriously slow, even on a fast PC. The VS 2004 emulators are MUCH better
NeilCowburn-MVP at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 4
Thaks for the quick reply!!!!
Maybe i wasn′t clear ( bad english Smile)
I know that the statement I used works fine, I've already tryed it, but thanks for the tip, the problems is that in some pdas it gives an exception, so I dont want to use it....
So, I tryed the second choice, that works great.... in a windows application Sad, but it doesn't work in my project that is a class library ( the panel is a control that i created) , it gives the exception that i shoewd in my last post, and i can't figure out the problem.

In answer to yor question I'm using VS .NET 2003.
Thanks!
Mónica

Mónica at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 5
Shouldn't this:

this.pictureBox0.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox10.Image")));

actually be this:

this.pictureBox0.Image = ((System.Drawing.Image)(resources.GetObject("Images.Image0.gif")));

Alternatively, you can try this:

this.pictureBox0.Image = new Bitmap(this.GetType().Assembly.GetManifestResourceStream("Images.Image0.gif");

Incidently, how are you adding the images to the assembly's resources: are you adding them to the project and setting their build action to embedded resource or are you adding them to a resource file?
NeilCowburn-MVP at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 6
Ok, I've tryed what you said:

this.pictureBox0.Image = ((System.Drawing.Image)(resources.GetObject("Images.Image0.gif")));
I get the same exception Sad

The second line of code "this.pictureBox0.Image = new Bitmap(this.GetType().Assembly.GetManifestResourceStream("Images.Image0.gif");"
is what I have and it does work, but not in all pdas, and I want something that doesn't give me an exception...
I've been working on this for days, and I've got nothing, don′t know what to do..
I'm starting to think it's because of dlls (mscorlib) but I don't know what to do !!!!
I'm adding the images to the project and setting buid action as embedded resource...
Thanks for taking the time...
Mónica

Mónica at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 7
Could you explain what you mean by "it works, but not in all PDAs"? What is the .NET Compact Framework version in the PDAs that don't work?
Are you referencing the image resource correctly? "Images.Image0.gif" implies that the assembly's full namespace is "Images" -- is this correct?
I need to see more code to help you through this.
NeilCowburn-MVP at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 8
A coleague of mine has made another pda application and when it was tested in some models of pdas it gave an exception on the lineof the function "...new Bitmap( assembly.GetManifestResourceStream(this.GetType(),
"Images.0d.gif") );"
So, i wanto to try some other thing to workaround this problem, although it runs perfectly fine almost of the cases...
I'll give yuo a sample of my code:
I have a class libary ( that is a windows application ) that as a class: The images are located in the folder named Images:
using System;
using System.Drawing;
using System.Resources;
using System.Windows.Forms;
namespace Keyboard
{

public class PanelKeyboard : Panel
{
private System.Windows.Forms.PictureBox pictureBox0;
public PanelKeyboard( NumericInput numericInput, Image img )
{
//
// TODO: Add constructor logic here
//
_NumericInput = numericInput;

//InitializeComponent();
AddControls(img);

this.Visible = false;
this.Location = new System.Drawing.Point( numericInput.Location.X + 10, numericInput.Location.Y + numericInput.Height);

}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint (e);
}

protected override void Dispose( bool disposing )
{
base.Dispose( disposing );
}

private void AddPicturesToParent()
{
this.pictureBox0.Parent = this;
}
private void AddControls(Image img)
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(PanelKeyboard));
this.pictureBox0 = new System.Windows.Forms.PictureBox();
AddPicturesToParent();
//this is the line where i get the exception
this.pictureBox0.Image = ((System.Drawing.Image)(resources.GetObject("Images.Image0.gif")));
this.pictureBox0.Location = new Point ( 0, 96 );
this.pictureBox0.Size = new System.Drawing.Size(64, 32);
this.pictureBox0.MouseUp += new System.Windows.Forms.MouseEventHandler(this.pictureBox0_MouseUp);
this.pictureBox0.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pictureBox0_MouseDown);
}
The I have a SmartDeviceApplication that calls imports Keyboard.dll and instatiates it
I'm sorry if i am wasting your time, thank you very much Neil
Mónica

Mónica at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 9
Change this line:

this.pictureBox0.Image = ((System.Drawing.Image)(resources.GetObject("Images.Image0.gif")));

to this:

this.pictureBox0.Image = new Bitmap(this.GetType().Assembly.GetManifestResourceStream("Keyboard.Images.Image0.gif"));

That should do it. The reason is that you must specify the assembly namespace when referencing an embedded resource. If you where to disassemble the compiled assembly and take a look at the generated IL, you would see this in the assembly's metadata:

.mresource public Keyboard.Images.Image0.gif
{
// Offset: 0x00000000 Length: 0x000004F6
}

NeilCowburn-MVP at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 10
Hi again Neil,
I already tryed the code you putted in your last reply, and it worked great, but as I said, a coleague of mine used this
"this.GetType().Assembly.GetManifestResourceStream(...) " and in some pdas it gives an exception (don't know why), so I wanted to use another aproach, but I can't seem to get it right... Well, i'm not quite shure how to solve this problem, but thanks a lot for your help

Mónica

Mónica at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 11
"in some pdas it gives an exception" is a vague statement. Can you expand on this please -- Which PDAs (brand and model) did this not work on? What is the version of the .NET Compact Framework running on these exception-throwing PDAs? Is the exception being thrown consistently (i.e. every single time you run the app) or in an ad-hoc manner (i.e. only sometimes)?
Please also note that the "this.GetType().Assembly.GetResourceManifestStream(...) line of code in my example is different from your example in that I reference the resource using the fully qualified resource name which includes the assembly's namespace. You must do this or you will get an exception.
NeilCowburn-MVP at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 12
Hi again Neil!!!

Sorry if you where expecting a reply sooner, but we had holliday here in Portugal (25-04)...
I don't know what models of PDAs it gives an exception, because it was a co-worker that catched that exception...

The exception is thrown consistently, but I arranjed a workaround Smile

I use a try catch statement (in this I use GetResourceManifestStream(...) to get the pictures and if it gives an exception I use buttons instead of pictureboxes... It's a way to make it work always Smile...

Thanks a lot for your help, Neil

Mónica at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 13
A useful method of determining the full name of all embedded resources in your assembly is to use GetManifestResourceNames which returns an array of strings containing each resource filename e.g.

string[] resourceNames = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()

Peter

PeterFoot at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...
# 14
Thanks a lot!
Your answer is really VERY helpfull!!!
WildWildWind at 2007-8-21 > top of Msdn Tech,Smart Device Development,.NET Compact Framework...