473,507 Members | 5,060 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I make, store in memory and return spacer image in C#2.0/aspx?

Hello,

On a high volume page we have the following javascript:
img = new Image()
img.src = 'http://myserver.com/count.aspx?x=1';

and it works fine, but now we've added:
img.onload = myImgLoaded

and it fails on IE and FF. The reason being it's not returning a proper
image, simply text. When a gif/jpg is returned it fires the onload event
every time.

Below I've sketched out how I might get the aspx page to return as an image,
but I would greatly appreciate help flushing it out.

void Application_Start(object sender, EventArgs e)
{
// i think i have to start with a bmp, not sure how to get it to gif or jpg
Bitmap bitmapSpacer = new Bitmap(1, 1);
}

private void Page_Load(object sender, System.EventArgs e)
{
// set the header
Response.ContentType = "image/gif";

// misc biz logic here

// return the image
// keep in mind I don't want to create this image each page loads nor load
it from the hard drive nor create a img tag
Response.Write(bitmapSpacer); // Can it can be returned this way?
}

Thank you.
Oct 14 '06 #1
4 1793
Assign onload event before you assign src.

var img = new Image();
img.onload = myImageLoad;
img.src = 'http://myserver.com/count.aspx?x=1';

It always happens when image is cached on the client machine and because of
this it's loaded before event handler is known.

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Mark S." wrote:
Hello,

On a high volume page we have the following javascript:
img = new Image()
img.src = 'http://myserver.com/count.aspx?x=1';

and it works fine, but now we've added:
img.onload = myImgLoaded

and it fails on IE and FF. The reason being it's not returning a proper
image, simply text. When a gif/jpg is returned it fires the onload event
every time.

Below I've sketched out how I might get the aspx page to return as an image,
but I would greatly appreciate help flushing it out.

void Application_Start(object sender, EventArgs e)
{
// i think i have to start with a bmp, not sure how to get it to gif or jpg
Bitmap bitmapSpacer = new Bitmap(1, 1);
}

private void Page_Load(object sender, System.EventArgs e)
{
// set the header
Response.ContentType = "image/gif";

// misc biz logic here

// return the image
// keep in mind I don't want to create this image each page loads nor load
it from the hard drive nor create a img tag
Response.Write(bitmapSpacer); // Can it can be returned this way?
}

Thank you.
Oct 14 '06 #2
Milosz;

Thank you, but I wasn't looking for feedback on the Javascript. For the
record, the code doesn't have to live in an onload, to avoid caching we use
img.src = 'http://myserver.com/count.aspx?x=1&cacheBuseter='+Math.random();

The Javascript works just fine, there's no problem firing the img onload
event IF an image is returned.

Thanks anyways, and to the rest of the group, please consider this question
still open for feedback.


"Milosz Skalecki" <mi*****@REMOVEITwp.plwrote in message
news:05**********************************@microsof t.com...
Assign onload event before you assign src.

var img = new Image();
img.onload = myImageLoad;
img.src = 'http://myserver.com/count.aspx?x=1';

It always happens when image is cached on the client machine and because
of
this it's loaded before event handler is known.

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Mark S." wrote:
>Hello,

On a high volume page we have the following javascript:
img = new Image()
img.src = 'http://myserver.com/count.aspx?x=1';

and it works fine, but now we've added:
img.onload = myImgLoaded

and it fails on IE and FF. The reason being it's not returning a proper
image, simply text. When a gif/jpg is returned it fires the onload event
every time.

Below I've sketched out how I might get the aspx page to return as an
image,
but I would greatly appreciate help flushing it out.

void Application_Start(object sender, EventArgs e)
{
// i think i have to start with a bmp, not sure how to get it to gif or
jpg
Bitmap bitmapSpacer = new Bitmap(1, 1);
}

private void Page_Load(object sender, System.EventArgs e)
{
// set the header
Response.ContentType = "image/gif";

// misc biz logic here

// return the image
// keep in mind I don't want to create this image each page loads nor
load
it from the hard drive nor create a img tag
Response.Write(bitmapSpacer); // Can it can be returned this way?
}

Thank you.

Oct 14 '06 #3
Sorry, i was misled by 'we added onload and it stoped working'. I created an
example that should fixed the problem.

1. page generating the counter gif:

using System;
using System.Web;
using System.Drawing;

public partial class Counter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
const int DefaultCounterValue = 0;
int counter;

if (!int.TryParse(Request.QueryString["x"], out counter))
counter = DefaultCounterValue;
System.Drawing.Image image = GenerateCounterImage(counter);
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

Response.ContentType = "image/gif";
Response.End();

}

private System.Drawing.Image GenerateCounterImage(int counter)
{
const int Width = 100;
const int Height = 30;

Bitmap image = new Bitmap(Width, Height);
Graphics graphics = Graphics.FromImage(image);
Font font = new Font("Arial", 10.0f);

graphics.FillRectangle(Brushes.Yellow, 0, 0, Width, Height);
graphics.DrawString(counter.ToString(), font, Brushes.Red, new PointF(1,
1));
graphics.Dispose();
font.Dispose();

return image;
}
}
and test html on another page:

<script language="javascript">
var g_img = new Image();
g_img.onload = myImageLoad;
g_img.src = 'counter.aspx?x=1';

function myImageLoad()
{
var counterImage = document.getElementById('<%=counterImage.ClientID %>');
counterImage.src = g_img.src;
}
</script>

<img runat="server" id="counterImage" alt="loading..."/>
hope this helps
--
Milosz Skalecki
MCP, MCAD
"Mark S." wrote:
Milosz;

Thank you, but I wasn't looking for feedback on the Javascript. For the
record, the code doesn't have to live in an onload, to avoid caching we use
img.src = 'http://myserver.com/count.aspx?x=1&cacheBuseter='+Math.random();

The Javascript works just fine, there's no problem firing the img onload
event IF an image is returned.

Thanks anyways, and to the rest of the group, please consider this question
still open for feedback.


"Milosz Skalecki" <mi*****@REMOVEITwp.plwrote in message
news:05**********************************@microsof t.com...
Assign onload event before you assign src.

var img = new Image();
img.onload = myImageLoad;
img.src = 'http://myserver.com/count.aspx?x=1';

It always happens when image is cached on the client machine and because
of
this it's loaded before event handler is known.

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Mark S." wrote:
Hello,

On a high volume page we have the following javascript:
img = new Image()
img.src = 'http://myserver.com/count.aspx?x=1';

and it works fine, but now we've added:
img.onload = myImgLoaded

and it fails on IE and FF. The reason being it's not returning a proper
image, simply text. When a gif/jpg is returned it fires the onload event
every time.

Below I've sketched out how I might get the aspx page to return as an
image,
but I would greatly appreciate help flushing it out.

void Application_Start(object sender, EventArgs e)
{
// i think i have to start with a bmp, not sure how to get it to gif or
jpg
Bitmap bitmapSpacer = new Bitmap(1, 1);
}

private void Page_Load(object sender, System.EventArgs e)
{
// set the header
Response.ContentType = "image/gif";

// misc biz logic here

// return the image
// keep in mind I don't want to create this image each page loads nor
load
it from the hard drive nor create a img tag
Response.Write(bitmapSpacer); // Can it can be returned this way?
}

Thank you.


Oct 15 '06 #4
Thank you, your code showed me the way to succesfully returning the gif.

"Milosz Skalecki" <mi*****@REMOVEITwp.plwrote in message
news:78**********************************@microsof t.com...
Sorry, i was misled by 'we added onload and it stoped working'. I created
an
example that should fixed the problem.

1. page generating the counter gif:

using System;
using System.Web;
using System.Drawing;

public partial class Counter : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
const int DefaultCounterValue = 0;
int counter;

if (!int.TryParse(Request.QueryString["x"], out counter))
counter = DefaultCounterValue;
System.Drawing.Image image = GenerateCounterImage(counter);
image.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Gif);

Response.ContentType = "image/gif";
Response.End();

}

private System.Drawing.Image GenerateCounterImage(int counter)
{
const int Width = 100;
const int Height = 30;

Bitmap image = new Bitmap(Width, Height);
Graphics graphics = Graphics.FromImage(image);
Font font = new Font("Arial", 10.0f);

graphics.FillRectangle(Brushes.Yellow, 0, 0, Width, Height);
graphics.DrawString(counter.ToString(), font, Brushes.Red, new PointF(1,
1));
graphics.Dispose();
font.Dispose();

return image;
}
}
and test html on another page:

<script language="javascript">
var g_img = new Image();
g_img.onload = myImageLoad;
g_img.src = 'counter.aspx?x=1';

function myImageLoad()
{
var counterImage = document.getElementById('<%=counterImage.ClientID %>');
counterImage.src = g_img.src;
}
</script>

<img runat="server" id="counterImage" alt="loading..."/>
hope this helps
--
Milosz Skalecki
MCP, MCAD
"Mark S." wrote:
>Milosz;

Thank you, but I wasn't looking for feedback on the Javascript. For the
record, the code doesn't have to live in an onload, to avoid caching we
use
img.src =
'http://myserver.com/count.aspx?x=1&cacheBuseter='+Math.random();

The Javascript works just fine, there's no problem firing the img onload
event IF an image is returned.

Thanks anyways, and to the rest of the group, please consider this
question
still open for feedback.


"Milosz Skalecki" <mi*****@REMOVEITwp.plwrote in message
news:05**********************************@microso ft.com...
Assign onload event before you assign src.

var img = new Image();
img.onload = myImageLoad;
img.src = 'http://myserver.com/count.aspx?x=1';

It always happens when image is cached on the client machine and
because
of
this it's loaded before event handler is known.

Hope this helps
--
Milosz Skalecki
MCP, MCAD
"Mark S." wrote:

Hello,

On a high volume page we have the following javascript:
img = new Image()
img.src = 'http://myserver.com/count.aspx?x=1';

and it works fine, but now we've added:
img.onload = myImgLoaded

and it fails on IE and FF. The reason being it's not returning a
proper
image, simply text. When a gif/jpg is returned it fires the onload
event
every time.

Below I've sketched out how I might get the aspx page to return as an
image,
but I would greatly appreciate help flushing it out.

void Application_Start(object sender, EventArgs e)
{
// i think i have to start with a bmp, not sure how to get it to gif
or
jpg
Bitmap bitmapSpacer = new Bitmap(1, 1);
}

private void Page_Load(object sender, System.EventArgs e)
{
// set the header
Response.ContentType = "image/gif";

// misc biz logic here

// return the image
// keep in mind I don't want to create this image each page loads nor
load
it from the hard drive nor create a img tag
Response.Write(bitmapSpacer); // Can it can be returned this way?
}

Thank you.



Oct 15 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
11983
by: Versteijn | last post by:
Hello all I have a ASPX file called scaleimage.aspx that scales my images down to a given width. This page is used very much in web project I am working on. But due to the large size of the...
6
14673
by: Selden McCabe | last post by:
I have a form with a bunch of image buttons. When the user moves the mouse over a button, I want to do two things: 1. change the Imagebutton's picture, and 2. make another control visible. I'm...
5
3140
by: drdave | last post by:
Hi, I have 6 forms being generated using coldFusion, they are named special1, special2 special3 and so on.. in these forms I have a link to open a new window. I am trying to pickup the...
6
4841
by: scottyman | last post by:
I can't make this script work properly. I've gone as far as I can with it and the rest is out of my ability. I can do some html editing but I'm lost in the Java world. The script at the bottom of...
0
1170
by: Samy | last post by:
Hi There, I am trying to display images in a gridview and display only valid images from the html in the database (and not display spacers, 1x1 pixel images etc). For this, I have a gridview...
0
987
by: Roland Schwarz | last post by:
Most probably what I am asking for already has been answered somewhere, still I was not able to find:-( I want to encapsulate a class ( a primitive type for the beginning ) to behave like a...
2
2687
by: Jay | last post by:
I have a web app running on the windows CE device. In one of the asp.net pages - it has javascript code. That seems to have a memory leak. When I run the web app - in about one hour, the app hangs....
3
2821
by: Ken Fine | last post by:
This is a question that someone familiar with ASP.NET and ADO.NET DataSets and DataTables should be able to answer fairly easily. The basic question is how I can efficiently match data from one...
1
47347
KevinADC
by: KevinADC | last post by:
Note: You may skip to the end of the article if all you want is the perl code. Introduction Many websites have a form or a link you can use to download a file. You click a form button or click...
0
7110
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
7314
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
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...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5041
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.