请问如何用GDI+方式创建图片?
! 问题点数:100、回复次数:1Top
1 楼szwebnet(赤水流星)回复于 2003-06-02 02:47:35 得分 100
Namespaces Used:
System
System.Drawing
System.Drawing.Text
System.Drawing.Drawing2D
System.Drawing.Imaging
Private Sub Page_Load(Sender as Object, E As Eventargs)
'First Lets Dim some variables....
'Width and Height of the image we are making
Dim aWidth As Integer = 300
Dim aHeight As Integer = 300
'Background color of the image
Dim aBgColor As Drawing.Color = Drawing.Color.White
'Text to "Draw" on the image
Dim aText As String = "Hello World"
'Font and font color to use
Dim aFont As New Drawing.Font("Comic Sans MS", 18, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Pixel)
Dim aFontColor As Drawing.Color = Drawing.Color.Black
'Rectangle's color (You can also use Drawing.Color.FromName(String) to get a "Color" from a string like "Gold"
Dim aRectangleColor As Drawing.Color = Drawing.Color.FromName("LightGray")
'now lets start the main work
'We start by Dim'ing a new Bitmap image with our Width, Height and Image Format
Dim b As New Drawing.Bitmap(aWidth, aHeight, Drawing.Imaging.PixelFormat.Format24bppRgb)
'Next we create a Graphics object from the bitmap image. This is used for drawing with, kinda like a tool.
Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(b)
'We will clear the image to the background color.
g.Clear(aBgColor)
'Set some SmoothingMode so that we arnt all pixelated.
g.SmoothingMode = Drawing.Drawing2D.SmoothingMode.AntiAlias
'StringFormat is used to align the text in this example.. It has other uses as well.
Dim SFormat As New Drawing.StringFormat()
SFormat.Alignment = Drawing.StringAlignment.Center
'now the fun part. The Drawing !!!
'First lets Draw our Text onto the bitmap
'This is done via DrawString(text As String, Font As Font, Brush As Brush, X, Y, StringFormat)
'The SFormat is set to align the text centered. It will be centered on the X position of the draw
'so we set the X value to half of the width of our image (b)
g.DrawString(aText, aFont, New Drawing.SolidBrush(aFontColor), b.Width / 2, 5, sFormat)
'Next lets draw our rectangle, We use FillRectangle to get a solid one, and Drawrectangle to get one with borders
'FillRectangle(Brush, X, Y, Width, Height)
g.FillRectangle(New Drawing.SolidBrush(aRectangleColor), 10, 100, b.Width - 20, 100)
'Now we are almost done...
'We Clear() The Response. and Set the ContentType to Jpeg
Response.Clear()
Response.ContentType = "image/jpeg"
'Then we Save the Bitmap to the OutputStream.
'Save(Stream as IO.Stream, Format as ImageFormat)
b.Save(Response.OutputStream, Drawing.Imaging.ImageFormat.Jpeg)
'Dispose of the Bitmap, Flush the response, And End()
b.Dispose()
Response.Flush()
Response.End()
'Thats It...
End SubTop




