422,911 Members | 1,861 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,911 IT Pros & Developers. It's quick & easy.

Performance issue part 2 (revised with all code) Please help

P: n/a
i have a need to load image from URL. the image is really small (gif) and i
use the following code.

but the code is too slow any1 have any alternative way(S)?
here is the url
http://phone.avioninc.com/asterisk/w...oneringing.gif

-----here is the performance counter out put when i use
the code .-------

creating extensions -----------------
creating extensions Done ------------------ 3.34650549161975
creating extensions -----------------
creating extensions Done ------------------ 0.192390932367103
creating extensions -----------------
creating extensions Done ------------------ 0.185763833112868
creating extensions -----------------
creating extensions Done ------------------ 0.186087058550738
creating extensions -----------------
creating extensions Done ------------------ 0.187169039640513

----here is the performance counter output when i DO NOT use the
code. --------
creating extensions -----------------
creating extensions Done ------------------ 0.0457695042246989
creating extensions -----------------
creating extensions Done ------------------ 0.0147314812357436
creating extensions -----------------
creating extensions Done ------------------ 0.0162037353909505
creating extensions -----------------
creating extensions Done ------------------ 0.0166747449745708
creating extensions -----------------
creating extensions Done ------------------ 0.0188892722399076
creating extensions -----------------
creating extensions Done ------------------ 0.0150602939759103
creating extensions -----------------
creating extensions Done ------------------ 0.0156684718309171
-------------------- CODE ------------------------
private void SetCallerId(string url)
{
try
{
if(url == null)
{
CallerPicture.Image = null;
return;
}

Stream s = MakeStream(url);
if(s != null)
{
Bitmap temp = new Bitmap(s);
Bitmap image = new Bitmap(temp);
temp.Dispose();
s.Close();
CallerPicture.Image = image;
}
else
CallerPicture.Image = null;
CallerPicture.Refresh();
}
catch
{
return;
}
}
private Stream MakeStream(string url)

{

Stream s = null;

if( url.ToLower().StartsWith("http") )

{

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

// Turn off connection keep-alives.

request.KeepAlive = false;

request.AllowAutoRedirect = true;

HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();

s = HttpWResp.GetResponseStream();

}

else

s = new FileStream( url, FileMode.Open );

return s;

}
Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Raj,

I'm not sure what you are expecting. First off, the numbers don't
really mean anything (to me at least) without some context as to how you
have obtained them.

Second, you are downloading a file from a URL. That is going to be
orders of magnitude slower than opening a file stream.

Also, you are not properly disposing of any resources that you are
allocating. Your code should look like this:

private Bitmap MakeBitmap(string url)
{
// If the url is null, get out.
if (url == null)
{
// Return null.
return null;
}

// Trim the url, this assumes it is user input.
url = url.Trim();

// If the string is empty, get out.
if (url.Length == 0)
{
// Return null.
return null;
}

// Just create the request. If this is a FILE url, the static Create
method
// on WebRequest will handle it.
// If the URL is a file and doesn't have a file scheme in front, you can
add it here.
WebRequest request = WebRequest.Create(url);

// The HttpWebRequest.
HttpWebRequest httpRequest = null;

// If you have http specific stuff to do here, set it.
if ((httpWebRequest = request as HttpWebRequest) != null)
{
// Set the http specific stuff.
httpWebRequest.KeepAlive = false;
httpWebRequest.AllowAutoRedirect = true;
}

// Now get the response and create the bitmap.
using (WebResponse response = request.GetResponse())
{
// Get the response stream.
using (Stream stream = response.GetResponseStream())
{
// Create the bitmap.
return new Bitmap(stream);
}
}
}

Your SetCallerId method then becomes:

private void SetCallerId(string url)
{
// Get the picture.
Bitmap b = MakeBitmap(url);

// Set the image.
CallerPicture.Image = MakeBitmap(url);

// Refresh.
CallerPicture.Refresh();
}

This should alleviate some of the resource problems you are having.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Raj Chudasama" <raj@asteriasgi_spamkiller.com> wrote in message
news:u9**************@TK2MSFTNGP15.phx.gbl...
i have a need to load image from URL. the image is really small (gif) and i
use the following code.

but the code is too slow any1 have any alternative way(S)?
here is the url
http://phone.avioninc.com/asterisk/w...oneringing.gif

-----here is the performance counter out put when i use
the code .-------

creating extensions -----------------
creating extensions Done ------------------ 3.34650549161975
creating extensions -----------------
creating extensions Done ------------------ 0.192390932367103
creating extensions -----------------
creating extensions Done ------------------ 0.185763833112868
creating extensions -----------------
creating extensions Done ------------------ 0.186087058550738
creating extensions -----------------
creating extensions Done ------------------ 0.187169039640513

----here is the performance counter output when i DO NOT use the
code. --------
creating extensions -----------------
creating extensions Done ------------------ 0.0457695042246989
creating extensions -----------------
creating extensions Done ------------------ 0.0147314812357436
creating extensions -----------------
creating extensions Done ------------------ 0.0162037353909505
creating extensions -----------------
creating extensions Done ------------------ 0.0166747449745708
creating extensions -----------------
creating extensions Done ------------------ 0.0188892722399076
creating extensions -----------------
creating extensions Done ------------------ 0.0150602939759103
creating extensions -----------------
creating extensions Done ------------------ 0.0156684718309171
-------------------- CODE ------------------------
private void SetCallerId(string url)
{
try
{
if(url == null)
{
CallerPicture.Image = null;
return;
}

Stream s = MakeStream(url);
if(s != null)
{
Bitmap temp = new Bitmap(s);
Bitmap image = new Bitmap(temp);
temp.Dispose();
s.Close();
CallerPicture.Image = image;
}
else
CallerPicture.Image = null;
CallerPicture.Refresh();
}
catch
{
return;
}
}
private Stream MakeStream(string url)

{

Stream s = null;

if( url.ToLower().StartsWith("http") )

{

HttpWebRequest request = (HttpWebRequest) WebRequest.Create(url);

// Turn off connection keep-alives.

request.KeepAlive = false;

request.AllowAutoRedirect = true;

HttpWebResponse HttpWResp = (HttpWebResponse)request.GetResponse();

s = HttpWResp.GetResponseStream();

}

else

s = new FileStream( url, FileMode.Open );

return s;

}

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.