In this section we create a simple picture as an introductory training for asp.net graphics processing.
First Use VisualStudio.NET 2003 to create a new Web application, named Gditec, create a new Web Form named Gdi_sample1.aspx, and we write logical code for the form:
'-----Code begin-----
Imports System.Drawing
Imports System.Drawing.Bitmap
Imports System.Drawing.Graphics
Public Class WebForm1
Inherits System.Web.UI.Page
#Region the code generated by the Web Forms Designer
' The code generated by the form designer is omitted here
#End Region
Private Sub Page_Load (ByVal sender as System.Object, ByVal e as System.EventArgs) Handles MyBase.Load
' The user code to place the initialization page here
' Declare Bitmap object
' Bitmap object is a canvas, and we can make graphics on it with a canvas.
Dim Objbitmap as Bitmap
Dim I as Integer
' declares a random seed that is used to randomly generate coordinates of 1000 pixel points.
Dim Objrandom as Random
' Create Bitmap
Objbitmap = New Bitmap (300, 100)
' Set 1000 Pixels
Objrandom = New Random
For i = 1 to 5000
Objbitmap.setpixel (Objrandom.next), Objrandom.next (MB), Color.White)
Next
' Saves the Objgraphics object to the specified stream object in the specified graphic format (here is GIF).
' and output to the client. The Save method has 5 overloads that you can save to your hard disk, and you can choose a different picture format
Objbitmap.save (Response.outputstream, Drawing.Imaging.ImageFormat.Gif)
End Sub
End Class
'---------Code End-----
After you save the compilation, the gdi_sample1.aspx run the results as shown in Figure 11.1.
|
| Figure 11.1 Gdi_sample1.aspx Run Results |
When browsing through IE browser, we can see that there is a black background with white dots on the page. Of course this is just a simple application, and in the next section we will continue to introduce the use of classes under System.Drawing.