Asp.net 2.0 generate image on the fly
Ok, this time I will be straight to work.
- aspx file
<
asp:Image ID="Image1" runat="server" /> <!-- only one control to show the image -->- aspx.vb file
' Importing needed name spaces
Imports
System.Drawing.ImagingImports
System.Drawing
Than in page load event I am generating the image and inserting text on it
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load ' Creating bitmap and giving size width = 80 and heigh = 50 pxDim Bitmap As Bitmap = New Bitmap(80, 50) ' Getting the bitmap to use it as graphic image but default the bitmap is black coloredDim Graphic As Graphics = Graphics.FromImage(Bitmap) ' Creating shaped graphic image in this case rectangle and setting it's background color and size to ' fit the bitmap sizeGraphic.FillRectangle(Brushes.Beige, 0, 0, 80, 50)
' Setting font name and sizeDim font As New Font("Arial", 13) 'Using the graphics drawstring property to set our customer text, font, font color and outstepping distances from the top left corner by x an y axisGraphic.DrawString("my text", font, Brushes.Black, 10, 10) 'Saving the image and choosing its file formatBitmap.Save(Server.MapPath("image.jpg"), ImageFormat.Jpeg) 'Setting the asp image control ImageUrl property to show us the generated imageImage1.ImageUrl =
"~/image.jpg" End SubHope it helps
P.S.
Any questions are welcome
Ok, here is how to use it with http response'Sending the image in http response
Response.ContentType =
"image/jpeg" 'Saving the image and choosing its file formatBitmap.Save(Response.OutputStream, ImageFormat.Jpeg)
You can do the formatting of html in your response stream
Regards