Image Refresh

T

Thom Little

ASP.NET 1.1 Web Form

The intended operation ...

Enter String 1 in Text Box
Hit Show button
String is shown as an image
Hit Reset button
String is reset to null
Enter String 2 in Text Box
Hit Show button
String should be shown as and Image

This works great once.
The second time it insists on displaying String 1 and not String 2.

What is the DUMB mistake I am overlooking?

private void Page_Load(object sender, System.EventArgs e )
{
Response.Cache.SetExpires( DateTime.Now.AddSeconds( 1 ) );
Response.Cache.SetNoServerCaching( );
}

private void btnDebugShow_Click(object sender, System.EventArgs e)
{
string strUrl = Server.MapPath( "/temp/Debug.jpg" );
Bitmap bm = new Bitmap( 369, 32 );
Graphics g = Graphics.FromImage( bm );
RectangleF rf = new RectangleF( 4.0F, 4.0F, 369.0F, 32.0F );
Font fnt = new Font( "Arial Black", 12 );
SolidBrush sb = new SolidBrush( Color.Black );
g.Clear( Color.White );
g.DrawString( tbDebug.Text, fnt, sb, rf );
bm.Save( strUrl, ImageFormat.Jpeg );
imgDebug.ImageUrl = "http://www.tlatla.net/temp/Debug.jpg" ;
imgDebug.Visible = true ;
}

private void btnDebugReset_Click(object sender, System.EventArgs e)
{
tbDebug.Text = "" ;
imgDebug.Visible = false ;
}
 
S

Steven Cheng[MSFT]

Hi Thom,

Welcome to ASPNET newsgroup.
From your description, you're using GDI+ interfaces to dynamically create
image file and displaying them in the asp.net page. However, you found that
the image will be cached on the page even you've changed the temp image
file, yes?

Based on my understanding, the problem is caused by the clientside
browser's caching. As you used the following path for all your dynamic
generated image:

string strUrl = Server.MapPath( "/temp/Debug.jpg" );

And the image control is rendered as <img src=.... /> at client side. When
the browser detect the <img ..> element and use the "src" attribute to
locate the image, since the strUrl is always the same, the browser will use
the existing one cached at clientside first , that's why your new updated
image won't be downloaded and displayed at clientside. For such scenario,
I'd suggest the following two approachs:

1. Still use the temp file means you currently used, however, we need to
append querystring parameter at the end of the image url so as to differ
the url for diffferent image. For example:

string strUrl = Server.MapPath( "/temp/Debug.jpg" );
strUrl = strUrl + "?name=string1"

or strUrl = strUrl + "?name=" + name;

thus, when the client browser find the querystring has changed, it'll
invalid the clientside cache and rerequest and download the image from
server.

2. Since you're dynamically creating the image through GDI+ and displaying
them through web page, you can also consider use a httphandler to generate
the image and return it as the response stream. Thus, we can use the
httphandler url as the ImageUrl, something like:

strUrl = "image.ashx?image=string1"

Here is a good msdn tech article discussing on this:

#Image Generation Service for ASP.NET 1.1
http://msdn.microsoft.com/msdnmag/issues/04/04/CuttingEdge/default.aspx

Hope helps. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security




--------------------
| Reply-To: "Thom Little" <[email protected]>
| From: "Thom Little" <[email protected]>
| Subject: Image Refresh
| Date: Wed, 5 Oct 2005 20:28:55 -0400
| Lines: 51
| Organization: Thom Little Associates, Ltd.
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 65.99.185.176
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:11183
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| ASP.NET 1.1 Web Form
|
| The intended operation ...
|
| Enter String 1 in Text Box
| Hit Show button
| String is shown as an image
| Hit Reset button
| String is reset to null
| Enter String 2 in Text Box
| Hit Show button
| String should be shown as and Image
|
| This works great once.
| The second time it insists on displaying String 1 and not String 2.
|
| What is the DUMB mistake I am overlooking?
|
| private void Page_Load(object sender, System.EventArgs e )
| {
| Response.Cache.SetExpires( DateTime.Now.AddSeconds( 1 ) );
| Response.Cache.SetNoServerCaching( );
| }
|
| private void btnDebugShow_Click(object sender, System.EventArgs e)
| {
| string strUrl = Server.MapPath( "/temp/Debug.jpg" );
| Bitmap bm = new Bitmap( 369, 32 );
| Graphics g = Graphics.FromImage( bm );
| RectangleF rf = new RectangleF( 4.0F, 4.0F, 369.0F, 32.0F );
| Font fnt = new Font( "Arial Black", 12 );
| SolidBrush sb = new SolidBrush( Color.Black );
| g.Clear( Color.White );
| g.DrawString( tbDebug.Text, fnt, sb, rf );
| bm.Save( strUrl, ImageFormat.Jpeg );
| imgDebug.ImageUrl = "http://www.tlatla.net/temp/Debug.jpg" ;
| imgDebug.Visible = true ;
| }
|
| private void btnDebugReset_Click(object sender, System.EventArgs e)
| {
| tbDebug.Text = "" ;
| imgDebug.Visible = false ;
| }
|
| --
| -- Thom Little -- www.tlanet.net -- Thom Little Associates, Ltd.
| --
|
|
|
|
 
T

Thom Little

This is great and EXACTLY what I needed.

I used your first option and after I realized the trick of specifying the
"?" suffix only on the reference to the item and not when creating it it
worked correctly.

The suffix I used was "?n=" + DateTime.Now.Second.ToString( )

Thank you for the help.

I will investigate your second option in the near future.
 
S

Steven Cheng[MSFT]

You're welcome Thom,

Good luck!

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
--------------------
| From: "Thom Little" <[email protected]>
| References: <[email protected]>
<[email protected]>
| Subject: Re: Image Refresh
| Date: Thu, 6 Oct 2005 16:31:30 -0400
| Lines: 74
| X-Priority: 3
| X-MSMail-Priority: Normal
| X-Newsreader: Microsoft Outlook Express 6.00.2900.2670
| X-RFC2646: Format=Flowed; Original
| X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2670
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
| NNTP-Posting-Host: 65.99.185.176
| Path: TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP10.phx.gbl
| Xref: TK2MSFTNGXA01.phx.gbl
microsoft.public.dotnet.framework.aspnet.webcontrols:11202
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
|
| This is great and EXACTLY what I needed.
|
| I used your first option and after I realized the trick of specifying the
| "?" suffix only on the reference to the item and not when creating it it
| worked correctly.
|
| The suffix I used was "?n=" + DateTime.Now.Second.ToString( )
|
| Thank you for the help.
|
| I will investigate your second option in the near future.
|
| --
| -- Thom Little -- www.tlanet.net -- Thom Little Associates, Ltd.
| --
|
| | > Hi Thom,
| >
| > Welcome to ASPNET newsgroup.
| > From your description, you're using GDI+ interfaces to dynamically
create
| > image file and displaying them in the asp.net page. However, you found
| > that
| > the image will be cached on the page even you've changed the temp image
| > file, yes?
| >
| > Based on my understanding, the problem is caused by the clientside
| > browser's caching. As you used the following path for all your dynamic
| > generated image:
| >
| > string strUrl = Server.MapPath( "/temp/Debug.jpg" );
| >
| > And the image control is rendered as <img src=.... /> at client side.
When
| > the browser detect the <img ..> element and use the "src" attribute to
| > locate the image, since the strUrl is always the same, the browser will
| > use
| > the existing one cached at clientside first , that's why your new
updated
| > image won't be downloaded and displayed at clientside. For such
scenario,
| > I'd suggest the following two approachs:
| >
| > 1. Still use the temp file means you currently used, however, we need to
| > append querystring parameter at the end of the image url so as to
differ
| > the url for diffferent image. For example:
| >
| > string strUrl = Server.MapPath( "/temp/Debug.jpg" );
| > strUrl = strUrl + "?name=string1"
| >
| > or strUrl = strUrl + "?name=" + name;
| >
| > thus, when the client browser find the querystring has changed, it'll
| > invalid the clientside cache and rerequest and download the image from
| > server.
| >
| > 2. Since you're dynamically creating the image through GDI+ and
displaying
| > them through web page, you can also consider use a httphandler to
generate
| > the image and return it as the response stream. Thus, we can use the
| > httphandler url as the ImageUrl, something like:
| >
| > strUrl = "image.ashx?image=string1"
| >
| > Here is a good msdn tech article discussing on this:
| >
| > #Image Generation Service for ASP.NET 1.1
| > http://msdn.microsoft.com/msdnmag/issues/04/04/CuttingEdge/default.aspx
| >
| > Hope helps. Thanks,
| >
| > Steven Cheng
| > Microsoft Online Support
| >
| > Get Secure! www.microsoft.com/security
|
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top