drawing image without saving to file

M

Marc Pelletier

Hello all,

I have a class which includes a method to create a chart. I want to be able
to call this method from asp.net code as well as windows application code,
so I have sketched it out as returning a bitmap instance.

In my asp.net code I think I should call this method to return a bitmap and
then somehow stream it using the response object to the Webcontrols.image.
Is that right? The image object is on a page with a number of other
components. Can someone please point me to an example of how to do this?
I've been scouring the web for hours already.

thanks

Marc Pelletier
 
E

Eliyahu Goldin

What's wrong with saving image to file? You can then pass file name to the image control. There is a number of issues you should take care of though:
a.. In ASP.NET you have to provide unique file name. You can use session id and/or current time for that.
b.. You should take care of removing the files when they are not needed anymore.
c.. There could be some other issues depending on which way you take.
Eliyahu
 
P

Patrice

You stream the image to the browser by using the Save method using the
Response.OutputStream as an argument...

Patrice
 
M

Marc Pelletier

You stream the image to the browser by using the Save method using the
Response.OutputStream as an argument...

Yes, this is what I want to do, but how does it end up in my control? For
example, I have a datagrid and 2 images on my page, doesn't the response
object belong to the page? Maybe I'm dense but I need an actual example
that doesn't involve a page with nothing but an image on it.

Thanks

Marc Pelletier
 
M

Marc Pelletier

What's wrong with saving image to file?

Because it's just wrong! Why involve files when it is created in memory and
is immediately being transmitted and never used again. If this application
is a success there could be millions of hits a day. That's a lot of
unnecessary file access.

thanks

Marc Pelletier
 
P

Patrice

In a new page, suppress all the HTML code and add the following code or its
C# counterpart in Page.Load :

Dim b As New System.Drawing.Bitmap(100, 100,
System.Drawing.Imaging.PixelFormat.Format16bppRgb565)
Dim g As System.drawing.Graphics = System.Drawing.Graphics.FromImage(b)
Dim f As New System.Drawing.Font("Arial", 10)
g.FillRectangle(System.Drawing.Brushes.Bisque, 0, 0, 100, 100)
g.DrawString("Hello world !", f, System.Drawing.Brushes.Blue, 0, 0)
f.Dispose()
g.Dispose()
Response.ContentType = "image/jpeg"
b.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
b.Dispose()

You can now use this page wherver you want (for example as value for the src
attribute of an img tag) :
<img src="graphics.aspx">

You could add some parameter to control the image generation .

Hope it helps...
 
M

Marc Pelletier

You can now use this page wherver you want (for example as value for
the src attribute of an img tag) :
<img src="graphics.aspx">

OK, I think I am getting it. I create one aspx page for each image, and use
them as the source of the image components I place on my main page.In my
case I will be getting the finished bitmap from a function call so it will
look like:

b = MyClass.drawChart(100, 100,
Response.ContentType = "image/jpeg"
b.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
b.Dispose()

Simple! Can't wait to try it.

thanks

Marc Pelletier
 

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

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top