Loading a JPEG from Internet URL?

  • Thread starter Thread starter Boris Nienke
  • Start date Start date
I've used the following method to retreive an image using the FULL
..NET, not sure if the same can be used on th CF tho...

internal Image LoadImageFromURL(string URL)
{

HttpWebRequest hrqURL = (HttpWebRequest) HttpWebRequest.Create(URL);

try
{
HttpWebResponse hrspURL = (HttpWebResponse) hrqURL.GetResponse();

BinaryReader br = new BinaryReader((hrspURL.GetResponseStream()));

byte[] buff = new byte[256];
MemoryStream iStream = new MemoryStream();

int len =0;

do
{
len = br.Read(buff,0,256);
iStream.Write(buff,0,len);
}
while (len >0);

m_image = new byte[iStream.Length];
m_image = iStream.ToArray();
return (Image)new Bitmap(iStream);
}
catch(Exception e)
{
//MessageBox.Show(e.ToString());
return null;
}

}


Cheers

Chris
 
Thanks!
This works on a PocketPC too - but i have a strange effect:
When i call this function the 3rd time the pocketpc hangs up (soft-reset
needed)

It hangs at the line with
HttpWebResponse hrspURL = (HttpWebResponse) hrqURL.GetResponse();

why is that?

This is how i call it:

this.pic1.Image = LoadImageFromURL(dfURL.Text);
pic1.Update();
Application.DoEvents();


Oh, and i had to change this line:
m_image = new byte[iStream.Length];
to
byte[] m_image = new byte[iStream.Length];

is that OK?

thanks

Boris

internal Image LoadImageFromURL(string URL)
{

HttpWebRequest hrqURL = (HttpWebRequest) HttpWebRequest.Create(URL);

try
{
HttpWebResponse hrspURL = (HttpWebResponse) hrqURL.GetResponse();

BinaryReader br = new BinaryReader((hrspURL.GetResponseStream()));

byte[] buff = new byte[256];
MemoryStream iStream = new MemoryStream();

int len =0;

do
{
len = br.Read(buff,0,256);
iStream.Write(buff,0,len);
}
while (len >0);

m_image = new byte[iStream.Length];
m_image = iStream.ToArray();
return (Image)new Bitmap(iStream);
}
catch(Exception e)
{
//MessageBox.Show(e.ToString());
return null;
}

}
 
Make sure that you call Stream.Close, WebRequest.Close and
WebRequest.Dispose

--
Alex Feinman
---
Visit http://www.opennetcf.org
Boris Nienke said:
Thanks!
This works on a PocketPC too - but i have a strange effect:
When i call this function the 3rd time the pocketpc hangs up (soft-reset
needed)

It hangs at the line with
HttpWebResponse hrspURL = (HttpWebResponse) hrqURL.GetResponse();

why is that?

This is how i call it:

this.pic1.Image = LoadImageFromURL(dfURL.Text);
pic1.Update();
Application.DoEvents();


Oh, and i had to change this line:
m_image = new byte[iStream.Length];
to
byte[] m_image = new byte[iStream.Length];

is that OK?

thanks

Boris

internal Image LoadImageFromURL(string URL)
{

HttpWebRequest hrqURL = (HttpWebRequest) HttpWebRequest.Create(URL);

try
{
HttpWebResponse hrspURL = (HttpWebResponse) hrqURL.GetResponse();

BinaryReader br = new BinaryReader((hrspURL.GetResponseStream()));

byte[] buff = new byte[256];
MemoryStream iStream = new MemoryStream();

int len =0;

do
{
len = br.Read(buff,0,256);
iStream.Write(buff,0,len);
}
while (len >0);

m_image = new byte[iStream.Length];
m_image = iStream.ToArray();
return (Image)new Bitmap(iStream);
}
catch(Exception e)
{
//MessageBox.Show(e.ToString());
return null;
}

}
 
Make sure that you call Stream.Close, WebRequest.Close and
WebRequest.Dispose

don't know why - but "hrqULR" (which is the HttpWebRequest) does not
have a .Close or .Dispose function...

Boris
 
don't know why - but "hrqULR" (which is the HttpWebRequest) does not
have a .Close or .Dispose function...

well well...

now i have added WebResponse.Close (no .Dispose available) and of course
the Stream.close.
Seems to work now.. hope that was it - thank you!

Boris
 
Back
Top