472,121 Members | 1,517 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,121 software developers and data experts.

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 1741
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Versteijn | last post: by
6 posts views Thread by Selden McCabe | last post: by
6 posts views Thread by scottyman | last post: by
reply views Thread by Roland Schwarz | last post: by
2 posts views Thread by Jay | last post: by

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.