By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

Retrieving an Embedded Image from a DLL

P: n/a
I think I'm going insane, but thought I'd check with you all first before I
get myself commited!

Here's a method I've built to retrieve an image:

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

Assembly assembly = Assembly.GetCallingAssembly();

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

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

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

stream.Close();

return image;
}
The idea behind this method is that it can be used to retrieve an image for
a one time usage or use it to retrieve one frame within an animation. What's
driving me crazy is this:

If I use it in its simplest form then it works fine. Here's an example:

GetImage("Multimedia.Anim1", -1); // This works perfectly

But if I try to retrieve the same embedded image in animation mode then it
fails:

GetImage("Multimedia.Anim", 1); // This does not work
In the second case, I triple-checked that "fullName" is correct and yet
"stream" ends up being a nul value. HOW ON EARTH CAN THAT BE?!?!?

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

Nov 17 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

Are you sure about this:
Assembly assembly = Assembly.GetCallingAssembly();
That is, is it always the case this method resides in an assembly other that
calls it?

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...I think I'm going insane, but thought I'd check with you all first before I
get myself commited!

Here's a method I've built to retrieve an image:

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

Assembly assembly = Assembly.GetCallingAssembly();

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

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

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

stream.Close();

return image;
}
The idea behind this method is that it can be used to retrieve an image
for
a one time usage or use it to retrieve one frame within an animation.
What's
driving me crazy is this:

If I use it in its simplest form then it works fine. Here's an example:

GetImage("Multimedia.Anim1", -1); // This works perfectly

But if I try to retrieve the same embedded image in animation mode then it
fails:

GetImage("Multimedia.Anim", 1); // This does not work
In the second case, I triple-checked that "fullName" is correct and yet
"stream" ends up being a nul value. HOW ON EARTH CAN THAT BE?!?!?

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


Nov 17 '05 #2

P: n/a
Hi,
Here is a working code for that:

static string ExtractResource( string resourceName)
{
//look for the resource name
foreach( string currentResource in
System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceNames()
)
if ( currentResource.LastIndexOf( resourceName) != -1 )
{
string fqnTempFile = System.IO.Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName( fqnTempFile);
string rootName= System.IO.Path.GetFileNameWithoutExtension(
fqnTempFile);
string destFile = path + @"\" + rootName + "." +
System.IO.Path.GetExtension( currentResource);

System.IO.Stream fs =
System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream(
currentResource);

byte[] buff = new byte[ fs.Length ];
fs.Read( buff, 0, (int)fs.Length);
fs.Close();

System.IO.FileStream destStream = new System.IO.FileStream ( destFile,
FileMode.Create);
destStream.Write( buff, 0, buff.Length);
destStream.Close();

return destFile;
}

throw new Exception("Resource not found : " + resourceName);

}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:03**********************************@microsof t.com...
I think I'm going insane, but thought I'd check with you all first before I
get myself commited!

Here's a method I've built to retrieve an image:

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

Assembly assembly = Assembly.GetCallingAssembly();

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

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

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

stream.Close();

return image;
}
The idea behind this method is that it can be used to retrieve an image
for
a one time usage or use it to retrieve one frame within an animation.
What's
driving me crazy is this:

If I use it in its simplest form then it works fine. Here's an example:

GetImage("Multimedia.Anim1", -1); // This works perfectly

But if I try to retrieve the same embedded image in animation mode then it
fails:

GetImage("Multimedia.Anim", 1); // This does not work
In the second case, I triple-checked that "fullName" is correct and yet
"stream" ends up being a nul value. HOW ON EARTH CAN THAT BE?!?!?

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

Nov 17 '05 #3

P: n/a
Dmytro,

I have two top level projects:
- Desktop
- Pocket PC

Each references a class library project called "Multimedia". This project
contains many embedded images, plus a class called "ImageTools", with many
methods inside it.

My intention is to be able to simply call Multimedia.GetImage(imageName)
from either Desktop or Pocket PC and have the requested image returned for
use.

So I don't know if GetCallingAssembly is the right method to use but if you
have suggestions about what I should do then I'd very much welcome them.

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

"Dmytro Lapshyn [MVP]" wrote:
Hi,

Are you sure about this:
Assembly assembly = Assembly.GetCallingAssembly();


That is, is it always the case this method resides in an assembly other that
calls it?

--
Sincerely,
Dmytro Lapshyn [Visual Developer - Visual C# MVP]


Nov 17 '05 #4

P: n/a
Ignacio,

Thank you for that code. It works perfectly. Here's how I implemented it:

string imgFile =
Multimedia.Images.ExtractResource("Multimedia.Test .jpg");
pictureLogo1.Image = new Bitmap(imgFile);
File.Delete(imgFile);

// pictureLogo.Image = Multimedia.Images.GetImage("Multimedia.Test", -1);
// Works with Emulator, not with PPC
I deliberately included my original calling code too. What's SO strange is
that it works fine with the emulator, but not with the Pocket PC itself. I
get a TypeLoadException which I *think* has to do with that I'm passing back
an Image object. I'm surmising that the "Image" object in question is a
WinForms type, not a CF type.

I'm still at a loss as to how to compile a class library as a CF type when
I'm working with my CF project and as a WinForms type when I'm working with
my WinForms project. But your solution sidesteps this problem, so I do thank
you!

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

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
Here is a working code for that:

static string ExtractResource( string resourceName)
{
//look for the resource name
foreach( string currentResource in
System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceNames()
)
if ( currentResource.LastIndexOf( resourceName) != -1 )
{
string fqnTempFile = System.IO.Path.GetTempFileName();
string path = System.IO.Path.GetDirectoryName( fqnTempFile);
string rootName= System.IO.Path.GetFileNameWithoutExtension(
fqnTempFile);
string destFile = path + @"\" + rootName + "." +
System.IO.Path.GetExtension( currentResource);

System.IO.Stream fs =
System.Reflection.Assembly.GetExecutingAssembly(). GetManifestResourceStream(
currentResource);

byte[] buff = new byte[ fs.Length ];
fs.Read( buff, 0, (int)fs.Length);
fs.Close();

System.IO.FileStream destStream = new System.IO.FileStream ( destFile,
FileMode.Create);
destStream.Write( buff, 0, buff.Length);
destStream.Close();

return destFile;
}

throw new Exception("Resource not found : " + resourceName);

}
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 17 '05 #5

P: n/a
Hi,
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Ignacio,

Thank you for that code. It works perfectly. Here's how I implemented it:

string imgFile =
Multimedia.Images.ExtractResource("Multimedia.Test .jpg");
pictureLogo1.Image = new Bitmap(imgFile);
File.Delete(imgFile);

// pictureLogo.Image =
Multimedia.Images.GetImage("Multimedia.Test", -1);
// Works with Emulator, not with PPC
Hi, in what line it fails?

I'm still at a loss as to how to compile a class library as a CF type when
I'm working with my CF project and as a WinForms type when I'm working
with
my WinForms project. But your solution sidesteps this problem, so I do
thank
you!


You cannot there is no a template that be a DLL targeting the CF, you have
two options:
1- Develop it as a win app and when ready create a new dll project and copy
the code
2- Create a dll prokect from start and make sure that all features you use
are implemented in the CF.

I use the first approach btw.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #6

P: n/a
Ignacio,

My original code fails right when I'm trying to call GetImage. The error
message has something to do with Image not being supported in System.Drawing.

As to your two methods, my dilemma is that I *seem* to be using code that
should work in both the CF and WinForms. But there appears to be some
discrepancy between a CF Image object and a WinForms Image object. I've
noticed the same thing with DataSets.

My hope is that someone from Microsoft might read this because surely this
is something that should be addressed. It just shouldn't be this difficult.
It should be much more seamless.

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

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
Ignacio,

Thank you for that code. It works perfectly. Here's how I implemented it:

string imgFile =
Multimedia.Images.ExtractResource("Multimedia.Test .jpg");
pictureLogo1.Image = new Bitmap(imgFile);
File.Delete(imgFile);

// pictureLogo.Image =
Multimedia.Images.GetImage("Multimedia.Test", -1);
// Works with Emulator, not with PPC


Hi, in what line it fails?

I'm still at a loss as to how to compile a class library as a CF type when
I'm working with my CF project and as a WinForms type when I'm working
with
my WinForms project. But your solution sidesteps this problem, so I do
thank
you!


You cannot there is no a template that be a DLL targeting the CF, you have
two options:
1- Develop it as a win app and when ready create a new dll project and copy
the code
2- Create a dll prokect from start and make sure that all features you use
are implemented in the CF.

I use the first approach btw.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation


Nov 17 '05 #7

P: n/a
Hi,
Probably you are trying to use some method or a constructor for Image that
is not supported by the CF, to be honest with you I have never used the
emulator , I always use the device. It's possible also than may exist a
difference between the code for both platform., you better post this
question in the CF group

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:52**********************************@microsof t.com...
Ignacio,

My original code fails right when I'm trying to call GetImage. The error
message has something to do with Image not being supported in
System.Drawing.

As to your two methods, my dilemma is that I *seem* to be using code that
should work in both the CF and WinForms. But there appears to be some
discrepancy between a CF Image object and a WinForms Image object. I've
noticed the same thing with DataSets.

My hope is that someone from Microsoft might read this because surely this
is something that should be addressed. It just shouldn't be this
difficult.
It should be much more seamless.

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

"Ignacio Machin ( .NET/ C# MVP )" wrote:
Hi,
"Robert W." <Ro*****@discussions.microsoft.com> wrote in message
news:EE**********************************@microsof t.com...
> Ignacio,
>
> Thank you for that code. It works perfectly. Here's how I implemented
> it:
>
> string imgFile =
> Multimedia.Images.ExtractResource("Multimedia.Test .jpg");
> pictureLogo1.Image = new Bitmap(imgFile);
> File.Delete(imgFile);
>
> // pictureLogo.Image =
> Multimedia.Images.GetImage("Multimedia.Test", -1);
> // Works with Emulator, not with PPC


Hi, in what line it fails?

> I'm still at a loss as to how to compile a class library as a CF type
> when
> I'm working with my CF project and as a WinForms type when I'm working
> with
> my WinForms project. But your solution sidesteps this problem, so I do
> thank
> you!


You cannot there is no a template that be a DLL targeting the CF, you
have
two options:
1- Develop it as a win app and when ready create a new dll project and
copy
the code
2- Create a dll prokect from start and make sure that all features you
use
are implemented in the CF.

I use the first approach btw.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

Nov 17 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.