Contents of graphics_demo.aspx:
<%@ page language="vb" %> <%@ import namespace="system" %> <%@ import namespace="system.IO" %> <%@ import namespace="system.Collections" %> <%@ import namespace="system.drawing" %> <%@ import namespace="system.drawing.text" %> <%@ import namespace="system.drawing.imaging" %> <%@ import namespace="system.drawing.drawing2d" %> <script runat="server"> Public Sub Page_Load(Source As Object, E As EventArgs) If NOT Page.IsPostBack Then ' '******* put the image choices in the select box ' imgType.Items.Add(New ListItem("png","png")) imgType.Items.Add(New ListItem("jpeg","jpeg")) imgType.Items.Add(New ListItem("bmp","bmp")) imgType.Items.Add(New ListItem("gif","gif")) else ' '*** second pass, create and return the image ' call generate_image End If End Sub sub generate_image ' response.clear ' make sure Nothing has gone to the client ' dim imgOutput as New bitmap(600, 480, pixelformat.format24bpprgb) dim g as graphics = graphics.fromimage(imgOutput) ' create a New graphic object from the above bmp Dim MemStream As New MemoryStream() '*** for png images dim i as Integer ' '**** set basic drawing parameters ' g.clear(color.darkgray) ' blank the image g.smoothingMode = smoothingMode.Antialias ' antialias objects ' '**** create color brushes, pens and fonts ' Dim blueBrush as New SolidBrush(Color.blue) '***a blue brush Dim redBrush as New SolidBrush(Color.red) '***a red brush Dim greenBrush as New SolidBrush(Color.green) '***a green brush Dim whiteBrush as New SolidBrush(Color.white) '***a white brush Dim blackBrush as New SolidBrush(Color.black) '***a black brush ' Dim whitepen as New Pen(color.white,1) '*** a white pen Dim blackpen as New Pen(color.black,1) '*** a black pen ' dim afont as New Font("Arial",10,fontstyle.bold) dim bfont as New Font("Comic Sans MS",14,fontstyle.bold) dim cfont as New Font("Times New Roman",14,fontstyle.bold) dim dfont as New Font("Arial",18,fontstyle.bold) ' '***** create some rectangles for demos tasks 1-6 and 16 ' Dim r1 As Rectangle = New Rectangle( 15, 30, 180, 20) '*** x,y,width,height Dim r2 As Rectangle = New Rectangle(210, 30, 180, 20) '*** x,y,width,height Dim r3 As Rectangle = New Rectangle(405, 30, 180, 20) '*** x,y,width,height ' Dim r4 As Rectangle = New Rectangle( 15, 60, 180, 30) '*** x,y,width,height Dim r5 As Rectangle = New Rectangle(210, 60, 180, 30) '*** x,y,width,height Dim r6 As Rectangle = New Rectangle(405, 60, 180, 30) '*** x,y,width,height ' Dim r7 As Rectangle = New Rectangle(360, 270, 215, 193) '*** x,y,width,height ' ' task 1-3 ' g.DrawRectangle(whitepen,r1) g.drawString("1. This is a Rectangle",afont, blueBrush, 18,33) ' g.DrawRectangle(whitepen,r2) g.drawString("2. Arial Font 10pt",afont, redBrush, 213,33) ' g.DrawRectangle(whitepen,r3) g.drawString("3. 160 x 20 pixels",afont, greenBrush, 408,33) ' ' task 4-6 draw rectangles ' g.DrawRectangle(whitepen,r4) g.drawString("4.Comic Sans MS:",bfont, blueBrush, 18,63) ' g.DrawRectangle(whitepen,r5) g.drawString("5. 14pt",bfont, redBrush, 213,63) ' g.DrawRectangle(whitepen,r6) g.drawString("6. Rect is 160x30",bfont, greenBrush, 408,63) ' '*** tasks 7-9 fill rectangles ' g.FillRectangle (redBrush, 15, 100, 180, 30) '*** x,y,width,height g.FillRectangle (bluebrush, 210, 100, 180, 30) '*** x,y,width,height g.FillRectangle (greenbrush, 405, 100, 180, 30) '*** x,y,width,height ' g.drawString("7. Filled Red", afont, whitebrush, New pointF( 18,103)) g.drawString("8. Filled Blue", bfont, whitebrush, New pointF(213,103)) g.drawString("9. Filled Green", cfont, whitebrush, New pointF(408,103)) ' '******** tasks 10 - 14 get an image, flip and rotate ' dim img_filename as string ' dim captain_image as System.Drawing.Image img_filename = server.mappath("captsm.gif") captain_image = System.Drawing.Image.FromFile(img_filename) ' g.drawimage(captain_image,New point(15,140)) g.drawString("10. Orig. Image", afont, whitebrush, New pointF(15,245)) ' captain_image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone) g.drawimage(captain_image,New point(130,140)) g.drawString("11. Rotate 90", afont, whitebrush, New pointF(130,245)) ' captain_image.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipX) g.drawimage(captain_image,New point(245,140)) g.drawString("12. Flip Horiz.", afont, whitebrush, New pointF(245,245)) ' captain_image.RotateFlip(System.Drawing.RotateFlipType.RotateNoneFlipY) g.drawimage(captain_image,New point(360,140)) g.drawString("13. Flip Vertical", afont, whitebrush, New pointF(360,245)) ' captain_image.RotateFlip(System.Drawing.RotateFlipType.Rotate90FlipNone) g.drawimage(captain_image,New point(470,140)) g.drawString("14. Rotate 90", afont, whitebrush, New pointF(470,245)) ' '*** task 15. Build (tile) a brick wall ' dim brick_image as System.Drawing.Image img_filename = server.mappath("brick.gif") brick_image = System.Drawing.Image.FromFile(img_filename) ' g.drawimage(brick_image,New point(15,270)) g.drawString("15. Here is brick.gif", afont, whitebrush, New pointF(70,270)) g.drawString("Build a brick wall", afont, whitebrush, New pointF(70,285)) g.drawString("(i.e., tile an area)", afont, whitebrush, New pointF(70,300)) ' Dim hor as Integer Dim vert as Integer ' for hor=15 to 324 step 54 for vert = 320 to 408 step 44 g.drawimage(brick_image,New point(hor,vert)) next next ' '*** task 16 rectangle andd fill with a gradient brush ' Dim gradBrush As new LinearGradientBrush (r7,Color.white,Color.Red,LinearGradientMode.Horizontal) g.FillRectangle (gradBrush, r7) ' g.drawString("16. Gradient Brush", bfont, blackBrush, New pointF(362,272)) g.drawString("Rectangle Fill", bfont, blackBrush, New pointF(402,292)) ' '*** task 17 single pixel shadow ' g.drawString("17. Drop Shadow", dfont, blackBrush, New pointF(362,316)) g.drawString("Effect", dfont, blackBrush, New pointF(402,336)) ' g.drawString("17. Drop Shadow", dfont, blueBrush, New pointF(363,317)) g.drawString("Effect", dfont, blueBrush, New pointF(403,337)) ' '*** task 18 three pixel deep shadow (right and down one pixel) ' g.drawString("18. Drop Shadow", dfont, blackBrush, New pointF(362,362)) g.drawString("Effect -- Right", dfont, blackBrush, New pointF(402,382)) ' g.drawString("18. Drop Shadow", dfont, blackBrush, New pointF(363,363)) g.drawString("Effect -- Right", dfont, blackBrush, New pointF(403,383)) ' g.drawString("18. Drop Shadow", dfont, blackBrush, New pointF(364,364)) g.drawString("Effect -- Right", dfont, blackBrush, New pointF(404,384)) ' g.drawString("18. Drop Shadow", dfont, blueBrush, New pointF(365,365)) g.drawString("Effect -- Right", dfont, blueBrush, New pointF(405,385)) ' '*** task 19 three pixel deep shadow left (left and down one pixel) ' g.drawString("19. Drop Shadow", dfont, blackBrush, New pointF(362,407)) g.drawString("Effect -- Left", dfont, blackBrush, New pointF(402,427)) ' g.drawString("19. Drop Shadow", dfont, blackBrush, New pointF(361,408)) g.drawString("Effect -- Left", dfont, blackBrush, New pointF(401,428)) ' g.drawString("19. Drop Shadow", dfont, blackBrush, New pointF(360,409)) g.drawString("Effect -- Left", dfont, blackBrush, New pointF(400,429)) ' g.drawString("19. Drop Shadow", dfont, blueBrush, New pointF(359,410)) g.drawString("Effect -- Left", dfont, blueBrush, New pointF(399,430)) ' '**** 20. graffiti transparency ' '*** get as bitmap not image ' Dim graffiti_bitmap as New Bitmap("c:\faculty\students\parks\graffiti.gif") Dim transparent_color As Color = graffiti_bitmap.GetPixel(1,1) ' graffiti_bitmap.MakeTransparent(transparent_color) g.drawimage(graffiti_bitmap,New point(30,330)) ' '*** display save instructions ' g.drawString("Right Click in the image", afont, whiteBrush, New pointF(40,410)) g.drawString("Then Click 'SAVE PICTURE AS...'", afont, whiteBrush, New pointF(40,430)) ' '*** Find chosen file type and complete title string ' Dim itype as String Dim imgItem as ListItem For Each imgItem in imgType.Items if imgItem.Selected then itype=imgItem.Text end if Next ' Dim title_string as String title_string = "VB.NET GRAPHICS DEMONSTRATION (graph_demo.aspx) Image is:" title_string=title_string+itype g.drawString(title_string,afont, whiteBrush, 33,5) ' '**** return the image ' select case itype case "png" ' ' png save ' Response.ContentType = "image/png" ' send the image to the memory stream then output imgOutput.Save(MemStream, ImageFormat.Png) MemStream.WriteTo(Response.OutputStream) ' case "jpeg" ' ' jpg save ' imgOutput.save(response.outputstream, imageformat.jpeg) ' output to the user ' case "gif" ' ' gif save ' imgOutput.save(response.outputstream, imageformat.gif) ' output to the user ' case "bmp" ' ' bmp save ' Response.ContentType = "image/bmp" ' send the image to the memory stream then output imgOutput.Save(MemStream, ImageFormat.bmp) MemStream.WriteTo(Response.OutputStream) ' end select ' g.dispose() ' imgOutput.dispose() brick_image.dispose() captain_image.dispose() graffiti_bitmap.dispose() ' response.end ' end sub </script> <html> <body> <center> <Table border="1"> <tr><td valign='middle' bgcolor='#999999'><img src="captsm.gif"></td> <td valign='middle' bgcolor='#999999'><center><font color="#ffffff"><font face="Comic Sans MS"> <b> Graphics Using VB.Net <br>Making an Image</b></td></tr></table></center> <font face="Comic Sans MS"> <p> This apsx program creates and returns a single image using VB.Net. It does 20 demonstrations that place text, change fonts and colors, draws and fills rectangles, places other images in the new image, rotates an image, flips an image, uses gradient brushes, demonstrates transparency, tiles a space and demonstrates text shadowing. <p> <ol> <li>Here are three images that are used in the creation of the new image: <p> <ol type="a"> <li> <img src="captsm.gif" align="top"><br><b>captsm.gif</b> for flips and rotates (100 x 100 pixels) <P> <li> <img src="brick.gif" align="top"><br><b>brick.gif</b> for tiling (54 x 44 pixels) <p> <li> <img src="graffiti.gif" align="top"><br><b>graffiti.gif</b> for transparency - black and white - (200 x 75 pixels) <p> </ol> <form name="gform" runat="server"> <li> Choose the type of Image File Format to create: <b> <asp:dropdownlist id="imgType" runat="server"></asp:dropdownlist> <P> <li> <input type="submit" value="Generate the Image"> <p> <li> Click <a href="bdoc.asp?fl=gr"> here </a> for the source code. </ol> </form> </font> </body> </html>
***** end of file *** printed: 306 lines on: 11/23/2009 5:57:49 AM