Sending Graphics

Team Fly 

Page 286

Sending Graphics

You can provide handsome backgrounds or dynamically generated graphs and other quick-response images using the new Response.Outputstream property. You can use this property to transmit a variety of different kinds of binary data to the client. In this example, you build a graphic using the powerful new GDI features in .NET, then you serialize the graphic to the Stream object returned by the Response.Outputstream property. Listing 11.6 generates the gradient and the Bezier curves shown in Figure 11.5:

LISTING 11.6: USING RESPONSCE.OUTPUTSTREAM FOR RAPID GRAPHICS

Imports System.Drawing
Imports System.Drawing.Drawing2D

Private Sub Page_Load(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles MyBase.Load

'set format
Dim b As New Bitmap(300, 500, Drawing.Imaging.PixelFormat.Format32bppRgb)
        Dim g As Graphics = Graphics.FromImage(b)

        'build gradient
        Dim rect As New Rectangle(0, 0, 300, 500)
        Dim b1 As New LinearGradientBrush(rect, Color.DarkGoldenrod, _
Color.PaleGoldenrod, LinearGradientMode.ForwardDiagonal)
        g.FillRectangle(b1, rect)

        'superimpose Bezier whip curves
        Dim p1 As New Point(54, 12)
        Dim p2 As New Point(212, 122)
        Dim p3 As New Point(134, 129)
        For i As Integer = 10 To 400 Step 100
            g.DrawBezier(Pens.BlueViolet, p1, p2, p3, New Point(i, 500))
        Next i

        'blank current contents and specify jpeg as response type
        Response.Clear()
        Response.ContentType = ''image/jpeg"

        b.Save(Response.OutputStream, Imaging.ImageFormat.Jpeg)

        g.Dispose()
        b.Dispose()
        Response.End()
    End Sub

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net