This article is not finished (apologies if you are reading it)
The following code snippet demonstrated how to load a bitmap, write text over it and save it back to a memory stream. This function can be used in a web page to return a bitmap response to a web request, see below.
The following code snippet demonstrated how to load a bitmap, write text over it and save it back to a memory stream. This function can be used in a web page to return a bitmap response to a web request, see below.
Imports System.Drawing Imports System.Drawing.Imaging Imports System.IO ... Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim strName As String ' get the text from the querystring strName = Request.QueryString.Item("name") If strName Is Nothing Then strName = "" ' if the string If strName.Length > 0 Then Dim objImage As MemoryStream = RenderImage(strName) ' clear the response, and tell the client ' we're sending an image Response.Clear() Response.ContentType = "image/jpeg" Response.AddHeader("Content-Length", objImage.Length.ToString) ' write the image binary as the response objImage.WriteTo(Response.OutputStream) objImage.Close() ' finish the response Response.End() End If End Sub Private Function RenderImage(ByVal strName As String) As MemoryStream Dim objBitmap As New Bitmap("C:\temp\example.jpg") Dim objGraphics As Graphics = Graphics.FromImage(objBitmap) Dim objImage As New IO.MemoryStream Dim objFont As New Font("Arial", "20") Dim objBrush As New SolidBrush(Color.Black) ' have it!! objGraphics.TextRenderingHint = Text.TextRenderingHint.ClearTypeGridFit objGraphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias objGraphics.DrawString(strName, objFont, objBrush, 200, 150) ' save output objBitmap.Save(objImage, ImageFormat.Jpeg) ' tidy up objBitmap.Dispose() objGraphics.Dispose() objFont.Dispose() objBrush.Dispose() Return objImage End Function
308 comments,
VB.Net, Monday, June 6, 2005 21:29


