473,395 Members | 1,762 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Retrieving an Embedded Image from a DLL

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
7 16695
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Tom Foster | last post by:
This should be painfully simple but I have spent two days on it and I can't figure it out. I am NEW to vb.net, so please forgive my ignorance I have a label that I assign an image to through an...
6
by: Dean Slindee | last post by:
Does anybody have an actual example of retrieving an Image data type column from a SQL Server table using a dataset (not a datareader)? I would like to see the statements that would move the...
1
by: Doug Handler | last post by:
I'm using VS2k5 and i've added a bunch of images to the project's resource.resx file and thus they reside in the Resources folder. I want to dynamically add an image to my project. I have no...
2
by: Pete | last post by:
I'm sure the answer is simple, but the solution is eluding me-I'm not too good at the particulars of scripting. Maybe someone can point me on the right path... Here is the problem: I have a...
1
by: EricBlair | last post by:
Hello, I have a response that is solely comprised of a gif image. Does anyone have ideas about how to retrieve that gif and save it as a file? So far I have this, but I'm stuck: ...
2
by: Nhoung Ar | last post by:
Can anyone outhere advise me about a best practice of embedded the image into form or report. I have several forms and report which embedded images (logo) to the header. I observed that the...
1
kaleeswaran
by: kaleeswaran | last post by:
hi! i want to know that how to getting the image from the my sql data base and stored it into some temprory file.i have no idea about retrive the image .but i upload image into the my sql data...
20
by: salmanjavaheri | last post by:
Hi we have a piece of php based (i think) software that exports to an rtf file, these files contain an image, this image makes the rtfs too large...so the solution is to reduce the...
3
by: Michael R | last post by:
Hello all. I created an Image field in a table. I set it to be an OLE Object data type. Later I viewed the table in datasheet view and insereted an image to each row of the Image field. Then I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.