I've posted a msg about this before, thought I had a solution, but didn't.
So I'm going to try again ...

I have a C# Pocket PC app with which I want to retrieve an embedded image
from a separate DLL. I don't know if it matters, but I call this DLL
"Multimedia.dll" and I use it to hold various images that are used in both
the Pocket PC app and the associated Desktop app.

Within Multimedia.dll I have a class called ImageTools in which I have
various methods. One of them is as follows:

public static Image GetImage(string fullImageName)
{
System.Drawing.Bitmap image = null;
Assembly assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(fullImageName);

if (stream != null)
{
image = new System.Drawing.Bitmap(stream);
}
stream.Close();

return image;
}


But I get an error if I call this method from my Pocket PC app. I think the
problem lies with GetExecutingAssembly but I'm not sure.

Does anyone know what I need to change to make this method retrieve images
from the DLL that contains it?

--
Robert W.
Vancouver, BC
www.mwtech.com

RE: Retrieving an Image from a DLL by RobertBurdickeMVP

RobertBurdickeMVP
Mon Oct 10 23:34:02 CDT 2005

You need to make sure that the name of the resource you are interested in
loading is correct. Embedded resources like bitmaps follow the following
naming convention:
Namespace.Filename

So a bitmap named myBitmap.map in an assemby with nampace foo would be
loaded as foo.myBitmap.bmp. Try passing this to your GetImage method and see
if it works. You can also prepend the namespace name in the method if you
don't want your client applications to have DLL specific namespace names in
them.

Look here for more details:

http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75

"Robert W." wrote:

> I've posted a msg about this before, thought I had a solution, but didn't.
> So I'm going to try again ...
>
> I have a C# Pocket PC app with which I want to retrieve an embedded image
> from a separate DLL. I don't know if it matters, but I call this DLL
> "Multimedia.dll" and I use it to hold various images that are used in both
> the Pocket PC app and the associated Desktop app.
>
> Within Multimedia.dll I have a class called ImageTools in which I have
> various methods. One of them is as follows:
>
> public static Image GetImage(string fullImageName)
> {
> System.Drawing.Bitmap image = null;
> Assembly assembly = Assembly.GetExecutingAssembly();
> Stream stream = assembly.GetManifestResourceStream(fullImageName);
>
> if (stream != null)
> {
> image = new System.Drawing.Bitmap(stream);
> }
> stream.Close();
>
> return image;
> }
>
>
> But I get an error if I call this method from my Pocket PC app. I think the
> problem lies with GetExecutingAssembly but I'm not sure.
>
> Does anyone know what I need to change to make this method retrieve images
> from the DLL that contains it?
>
> --
> Robert W.
> Vancouver, BC
> www.mwtech.com
>

RE: Retrieving an Image from a DLL by RobertW

RobertW
Tue Oct 11 01:33:02 CDT 2005

Robert,

Thank you for your feedback. You know what my ultimate problem turned out
to be? I [stupidly] didn't know that there was such a thing as a "Smart
Device Class Library". I had seen people refer to this but just assumed it
meant "a class library that is reference by a CF project".

So what I had inadvertently done is create a WinForms class library project
(called "Multimedia" in my case) and add embedded images and a class file of
assorted methods to it. This worked fine with my WinForms app but kept on
failing with my CF app. I just couldn't understand why. Especially when I'd
copy the same code into my CF project and it worked fine!

Only today did I learn what I had done wrong. And voila, after creating a
Smart Device Class Library and copying everything back into it, I discovered
that my original code worked fine! In both my CF app and my WinForms app no
less!!!

The following probably won't benefit you but maybe it'll save someone else
some time. These two GetImage methods reside inside my Multimedia class
library. They're designed to allow the retrieval of a single image (ie. once
and never again) or an image that is part of an animation.

One great part about it is that in the calling code I don't need to worry
about how many frames there are in the animation. For because I'm passing to
the [2nd] GetImage an integer parameter By Reference, it automatically resets
it back to 1 after it reaches the highest number in the sequence.


public static Image GetImage(string imageName)
{
int imageNum = -1;
return GetImage(imageName, ref imageNum);
}

public static Image GetImage(string imageBaseName, ref int imageNum)
{
string fullName = "";
Bitmap image = null;
Stream stream;

Assembly assembly = Assembly.GetExecutingAssembly();

string assemblyName = assembly.GetName().ToString();
assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(","));
// ie. "Multimedia"

// Is this just a single (ie. one-time) image?
if (imageNum == -1)
{
fullName = assemblyName + "." + imageBaseName + ".jpg";
stream = assembly.GetManifestResourceStream(fullName);
}
else // Or is it one of many in an animation
{
fullName = assemblyName + "." + imageBaseName + imageNum.ToString()
+ ".jpg";
stream = assembly.GetManifestResourceStream(fullName);

if (stream == null)
{
imageNum = 1; // Reset sequence
fullName = assemblyName + "." + imageBaseName + "1.jpg";
stream = assembly.GetManifestResourceStream(fullName);
}
}

if (stream != null)
image = new Bitmap(stream);

stream.Close();

return image;
}


--
Robert W.
Vancouver, BC
www.mwtech.com



"Robert Burdick [eMVP]" wrote:

> You need to make sure that the name of the resource you are interested in
> loading is correct. Embedded resources like bitmaps follow the following
> naming convention:
> Namespace.Filename
>
> So a bitmap named myBitmap.map in an assemby with nampace foo would be
> loaded as foo.myBitmap.bmp. Try passing this to your GetImage method and see
> if it works. You can also prepend the namespace name in the method if you
> don't want your client applications to have DLL specific namespace names in
> them.
>
> Look here for more details:
>
> http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75


RE: Retrieving an Image from a DLL by RobertBurdickeMVP

RobertBurdickeMVP
Tue Oct 11 11:55:30 CDT 2005

Yep!! Another sure sign of this is if you look at the properties of any
assemblies that your own DLL references, and they point to the big Windows
.NET framework versions vs. the compact framework versions, you'll have this
kind of problem.

Robert


"Robert W." wrote:

> Robert,
>
> Thank you for your feedback. You know what my ultimate problem turned out
> to be? I [stupidly] didn't know that there was such a thing as a "Smart
> Device Class Library". I had seen people refer to this but just assumed it
> meant "a class library that is reference by a CF project".
>
> So what I had inadvertently done is create a WinForms class library project
> (called "Multimedia" in my case) and add embedded images and a class file of
> assorted methods to it. This worked fine with my WinForms app but kept on
> failing with my CF app. I just couldn't understand why. Especially when I'd
> copy the same code into my CF project and it worked fine!
>
> Only today did I learn what I had done wrong. And voila, after creating a
> Smart Device Class Library and copying everything back into it, I discovered
> that my original code worked fine! In both my CF app and my WinForms app no
> less!!!
>
> The following probably won't benefit you but maybe it'll save someone else
> some time. These two GetImage methods reside inside my Multimedia class
> library. They're designed to allow the retrieval of a single image (ie. once
> and never again) or an image that is part of an animation.
>
> One great part about it is that in the calling code I don't need to worry
> about how many frames there are in the animation. For because I'm passing to
> the [2nd] GetImage an integer parameter By Reference, it automatically resets
> it back to 1 after it reaches the highest number in the sequence.
>
>
> public static Image GetImage(string imageName)
> {
> int imageNum = -1;
> return GetImage(imageName, ref imageNum);
> }
>
> public static Image GetImage(string imageBaseName, ref int imageNum)
> {
> string fullName = "";
> Bitmap image = null;
> Stream stream;
>
> Assembly assembly = Assembly.GetExecutingAssembly();
>
> string assemblyName = assembly.GetName().ToString();
> assemblyName = assemblyName.Substring(0, assemblyName.IndexOf(","));
> // ie. "Multimedia"
>
> // Is this just a single (ie. one-time) image?
> if (imageNum == -1)
> {
> fullName = assemblyName + "." + imageBaseName + ".jpg";
> stream = assembly.GetManifestResourceStream(fullName);
> }
> else // Or is it one of many in an animation
> {
> fullName = assemblyName + "." + imageBaseName + imageNum.ToString()
> + ".jpg";
> stream = assembly.GetManifestResourceStream(fullName);
>
> if (stream == null)
> {
> imageNum = 1; // Reset sequence
> fullName = assemblyName + "." + imageBaseName + "1.jpg";
> stream = assembly.GetManifestResourceStream(fullName);
> }
> }
>
> if (stream != null)
> image = new Bitmap(stream);
>
> stream.Close();
>
> return image;
> }
>
>
> --
> Robert W.
> Vancouver, BC
> www.mwtech.com
>
>
>
> "Robert Burdick [eMVP]" wrote:
>
> > You need to make sure that the name of the resource you are interested in
> > loading is correct. Embedded resources like bitmaps follow the following
> > naming convention:
> > Namespace.Filename
> >
> > So a bitmap named myBitmap.map in an assemby with nampace foo would be
> > loaded as foo.myBitmap.bmp. Try passing this to your GetImage method and see
> > if it works. You can also prepend the namespace name in the method if you
> > don't want your client applications to have DLL specific namespace names in
> > them.
> >
> > Look here for more details:
> >
> > http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=75
>