Help with writing System.drawing in server control

T

ThatsIT.net.au

I have this code that writes a pie chart in a asp.net page, but I want to
use it in a server control.

When I try I get a error on the last line "Response.OutputStream"
Obviously there is no response object but how do I write it to screen?

Dim objBitmap As New System.Drawing.Bitmap(400, 440)
Dim objGraphics As System.Drawing.Graphics
objGraphics = System.Drawing.Graphics.FromImage(objBitmap)
objGraphics.Clear(Drawing.Color.White)

Dim p As New Drawing.Pen(Drawing.Color.Yellow, 0)
Dim rect As New Drawing.Rectangle(10, 10, 280, 280)
'objGraphics.DrawEllipse(p, rect)

Dim b1 As New Drawing.SolidBrush(Drawing.Color.Red)
Dim b2 As New Drawing.SolidBrush(Drawing.Color.Green)
Dim b3 As New Drawing.SolidBrush(Drawing.Color.Blue)
objGraphics.FillPie(b1, rect, 0.0, 90.0)
objGraphics.FillPie(b2, rect, 90.0, 60.0)
objGraphics.FillPie(b3, rect, 150.0, 210.0)

Dim fontfml As New
Drawing.FontFamily(Drawing.Text.GenericFontFamilies.Serif)
Dim font As New Drawing.Font(fontfml, 14)
Dim brush As New Drawing.SolidBrush(Drawing.Color.Black)
objGraphics.DrawString("Missy Mosley", font, brush, 100, 300)
Dim i As Image = New Image

Dim mStream As MemoryStream = New MemoryStream
Dim httpApp As HttpApplication = New HttpApplication
objBitmap.Save(Response.OutputStream,
Drawing.Imaging.ImageFormat.Gif)
 
A

Alberto Poblacion

ThatsIT.net.au said:
I have this code that writes a pie chart in a asp.net page, but I want to
use it in a server control.

When I try I get a error on the last line "Response.OutputStream"
Obviously there is no response object but how do I write it to screen?

The server control is expected to produce some HTML that will be merged
into the page that contains the control. You cannot directly serve to the
page the bits that form the image, since HTML does not provide for an image
to be embedded inside the page source.
One thing that you can do is make your server control generate an <img
....> tag into its TextWriter. The img will have a src attribute pointing to
an .ashx handler, which is the one that contains your GDI+ code and writes
the bits to its output.
 
N

Nathan Sokalski

To use a System.Drawing.Bitmap object on a Page, you basically need to
create a separate Page that returns ContentType="image/gif" (or whatever
type of image you are creating). You then use that *.aspx file the same way
you would use a regular *.gif file. I wish there was a way to build this
into the Control itself also, but to the best of my knowledge there isn't,
since HTML needs a value to assign to the img tag's src attribute (or
wherever the image is going to be used). What you can do, however, is have
this separate Page accept a querystring so that you can use the same file to
create all your images so that you don't need to create a million extra
Pages. Something else you can do, if there are a couple static *.gif files
you will be using that you don't want the user to need to copy, is create a
resource file (*.resx) with the images in it and then have a Page that
simply loads one of them and writes it out the same way you would if you
were generating the image. Yeah, it's not what we want for writing custom
controls when we want to include images, but to the best of my knowledge,
it's the best we have write now.
 
S

support

You can use the following code in an aspx file to display your chart:

<%@ Page Language="vb" Debug="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<script runat="server" language="vb" >
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs)
Dim objBitmap As New System.Drawing.Bitmap(400, 440)
Dim objGraphics As System.Drawing.Graphics

objGraphics = System.Drawing.Graphics.FromImage(objBitmap)
objGraphics.Clear(Drawing.Color.White)

' your code...

objBitmap.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Gif)
End Sub
</script>
<HTML>
</HTML>
 
Top