Can't use Image.FromStream in CF

  • Thread starter Thread starter Saya
  • Start date Start date
S

Saya

Hello,
This is a repost 'cause I haven't solve the problem: I can't use the
System.Drawing class 'Image.FromStream' in the CompactFramework
environment. What I've done with respect to Brendan's suggestion is as
follows:

Brendan, thanks for the reply!
I'm a little bit further now, but not yet finished.
I've come this far, see code below:
Stream s =
Assembly.GetExecutingAssembly().GetmanifestResourceStream("somefile.jpg");
Bitmap b = new Bitmap(s);
return b;

I'm stuck with the part ("somefile.jpg") - don't know how to fill this
parameter.

The (working-)XP version code looks like this:
Stream str=null;
HttpRequest hReq=(HttpWebRequest)WebRequest.Create(aURL);
HttpWebResponse hRes=(HttpWebResponse)(hReq).GetResponse();
str=hRes.GetResponseStream();
return Image.FromStream(str);

Can you help more, please?
Thank you, Saya

==============================================
Previous posting:

While System.Drawing.Image does not have static FromStream() member
function
under CE, System.Drawing.Bitmap does have something similar, one of
it’s
constructors takes a System.IO.Stream as an argument. See if you can’t
use
this constructor to create a new instance of your image with the
Bitmap class.

Brendan
 
Saya said:
Hello,
This is a repost 'cause I haven't solve the problem: I can't use the
System.Drawing class 'Image.FromStream' in the CompactFramework
environment. What I've done with respect to Brendan's suggestion is as
follows:

Brendan, thanks for the reply!
I'm a little bit further now, but not yet finished.
I've come this far, see code below:
Stream s =
Assembly.GetExecutingAssembly().GetmanifestResourceStream("somefile.jpg");
Bitmap b = new Bitmap(s);
return b;

I'm stuck with the part ("somefile.jpg") - don't know how to fill this
parameter.
[I think you have use full name space to prefix the file name here.

For example, in my code, I have
GetManifestResourceStream("Company.Components.Resources.somefile.jpg")

HTH

John
]
 
You seem to be looking at two different things... in the 2nd case at least,
try the following:

Stream str=null;
HttpWebRequest hReq=(HttpWebRequest)WebRequest.Create(aURL);
HttpWebResponse hRes=(HttpWebResponse)(hReq).GetResponse();
str=hRes.GetResponseStream();
return new System.Drawing.Bitmap(str);

I did change the type of the hReq reference to an HttpWebRequest as I could
not locate the HttpRequest type as part of the compact framework... probably
just my inability to find it.

As you see, other than the before mentioned change, the biggest change is
returning a new Bitmap instead of using Image.FromStream().

As for your first code block... one obvious issue I see is that you are
calling GetmanifestResourceStream(), not GetManifestResourceStream() (note
the capitalization of the M in Manifest).

If that doesn’t fix that issue, couple of other things are possibly to
blame. Is somefile.jpg a part of the project? Is the Build Action for it set
to Embedded Resource? The most likely culprit though is the namespace.

Try adding the name of the root namespace of the project into the filename
followed by a period, so that if somefile.jpg exists in the project whose
root namespace is MyTestApplication, you would want the string being passed
into GetManifestResourceStream to be "MyTestApplication.somefile.jpg".

Brendan
 
Brendan,
Appreciate it very much: your elaborate answer ! Today I wasn't able
to code anything - tomorrow I'll try your suggestions first thing in
the morning.
As for your 'changes': it's my fault .. all typos in getting code into
this usenet mail! Should have done ctrl-C-V!
I'll post the outcome; thanks again.

Greetz, Saya


===============================================
 
Thank you for your suggestion John! I'll use it.

=========================================
Saya said:
Hello,
This is a repost 'cause I haven't solve the problem: I can't use the
System.Drawing class 'Image.FromStream' in the CompactFramework
environment. What I've done with respect to Brendan's suggestion is as
follows:

Brendan, thanks for the reply!
I'm a little bit further now, but not yet finished.
I've come this far, see code below:
Stream s =
Assembly.GetExecutingAssembly().GetmanifestResourceStream("somefile.jpg");
Bitmap b = new Bitmap(s);
return b;

I'm stuck with the part ("somefile.jpg") - don't know how to fill this
parameter.
[I think you have use full name space to prefix the file name here.

For example, in my code, I have
GetManifestResourceStream("Company.Components.Resources.somefile.jpg")

HTH

John
]
The (working-)XP version code looks like this:
Stream str=null;
HttpRequest hReq=(HttpWebRequest)WebRequest.Create(aURL);
HttpWebResponse hRes=(HttpWebResponse)(hReq).GetResponse();
str=hRes.GetResponseStream();
return Image.FromStream(str);

Can you help more, please?
Thank you, Saya

==============================================
Previous posting:

While System.Drawing.Image does not have static FromStream() member
function
under CE, System.Drawing.Bitmap does have something similar, one of
it’s
constructors takes a System.IO.Stream as an argument. See if you can’t
use
this constructor to create a new instance of your image with the
Bitmap class.

Brendan
 
G R E A T !!!

Brendan, your one-liner tip: "return new System.Drawing.Bitmap(str);"
did gave me a giant leap forward!! This was the solution for my
situation.
After solving some minor resource leaks (Closing stream and Response
request), the app runs perfectly. I didn't need anything more from
the GetManifest stuff.

Once again, thanks for your help!
Regards, Saya

===============================================
 
Hi Everyone,

I'm hoping one of you can help me, because my problem sounds very similar.
I've successfully written code to load an embedded image from a resource file:

Assembly thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file =
thisExe.GetManifestResourceStream("Multimedia.Logo-large.jpg");
Image image = Image.FromStream(file);


The code works perfectly with a WinForms application But I've tried
accessing it from a Pocket PC CF application and it fails. Based on
research I've done, I believe the problem lies with "Image.FromStream" but I
don't know what else to put there.

Any ideas?
 
Back
Top