In this section, we generate a simple image as an entry-level training for ASP. NET graphics processing.
First, use Visual Studio. NET 2003 to create a Web application named GDITec. Create a new Web form named GDI_Sample1.aspx. We will write logic 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 "code generated by 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
'User code to place the initialization page here
'Declare a bitmap object
'Bitmap object is equivalent to a canvas. With a canvas, we can make a graph on it.
Dim objBitmap As Bitmap
Dim I As Integer
'Declare Random Seed 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 1 To 5000
ObjBitmap. SetPixel (objRandom. Next (300), objRandom. Next (100), Color. White)
Next
'Save the objGraphics object to the specified Stream object in the specified image format (Gif here,
And output it to the client. The save method has five reloads and can be saved to the hard disk. You can select different image formats.
ObjBitmap. Save (Response. OutputStream, Drawing. Imaging. ImageFormat. Gif)
End Sub
End Class
'--------- Code end -----
After saving the compilation, GDI_Sample1.aspx running result 11.1 is shown.
|
Figure 11.1 GDI_Sample1.aspx running result |
When browsing through the IE browser, we can see that the page has a black background with a white dot. Of course, this is only a simple application. In the next section, we will continue to introduce the use of classes under System. Drawing.