473,394 Members | 2,160 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,394 software developers and data experts.

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

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

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
10
by: **ham | last post by:
I know that's an old dirty issue; GDI+ almost -the slowest part of the framework - has bothered many developers using it in animations. Even in managed C++ the performance is awful. Now, any dude...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
6
by: Catch_22 | last post by:
Hi, I have a large SQL Server 2000 database with 3 core tables. Table A : 10 million + records Table B : 2 million + records Table C : 6 million + records One of the batch tasks that I...
4
by: skotapal | last post by:
Hello I manage a web based VB .net application. This application has 3 components: 1. Webapp (this calls the executibles) 2. database 3. business logic is contained in individual exe...
1
by: Billy | last post by:
Hi All, I'm attempting to use the MapNetworkDrive <snippedbelow from entire code below with very poor performance results. Basically, I have very small 73kb text files that are rewritten daily...
7
by: Nightcrawler | last post by:
Hi all, I am trying to use regular expressions to parse out mp3 titles into three different groups (artist, title and remix). I currently have three ways to name a mp3 file: Artist - Title ...
0
by: Shades799 | last post by:
Hi All, I was wondering if any of you could help me with a very difficult problem that I am having. I have an ASP site that works with an Oracle database using an ADODB connection. This...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...

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.