Help with writing System.drawing in server control

Discussion in 'ASP .Net' started by ThatsIT.net.au, Oct 2, 2008.

  1. 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)
     
    ThatsIT.net.au, Oct 2, 2008
    #1
    1. Advertisements

  2. 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.
    --
    Nathan Sokalski

    http://www.nathansokalski.com/

    "ThatsIT.net.au" <me@work> wrote in message
    news:...
    >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)
     
    Nathan Sokalski, Oct 2, 2008
    #2
    1. Advertisements

  3. ThatsIT.net.au

    Guest

    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>



    "ThatsIT.net.au" wrote:

    > 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)
    >
     
    , Nov 26, 2008
    #3
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    858
    Kevin Spencer
    Jan 9, 2006
  2. darrel
    Replies:
    11
    Views:
    802
    Darren Kopp
    Feb 23, 2006
  3. byrd48
    Replies:
    0
    Views:
    3,783
    byrd48
    Jul 6, 2006
  4. Manuel

    System.Drawing, Server error

    Manuel, Jun 12, 2007, in forum: ASP .Net
    Replies:
    0
    Views:
    408
    Manuel
    Jun 12, 2007
  5. defn noob
    Replies:
    1
    Views:
    599
    Mark Space
    Jun 28, 2008
  6. Nathan Sokalski

    Control Properties of Type System.Drawing.Color

    Nathan Sokalski, Oct 27, 2008, in forum: ASP .Net
    Replies:
    5
    Views:
    1,186
    James Hahn
    Oct 27, 2008
  7. padawan

    System.Drawing and GDI+ Question - Please help

    padawan, Jan 25, 2004, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    209
    padawan
    Jan 26, 2004
  8. ThatsIT.net.au

    Help with writing System.drawing in server control

    ThatsIT.net.au, Oct 2, 2008, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    348
Loading...