Using Drawstring to have text with filled rectangle and border?

  • Thread starter Thread starter Mark B
  • Start date Start date
M

Mark B

In ASP.NET VB.NET, I have a variable, strMyText that can be any text up to 255 characters.

I want be able to create a bitmap that has the text inside a bordered rectangle like these:



So the rectangle needs to stretch depending on the text length.

If anyone can help me out with the code in fGetNewLabelImage below I appreciate it.

I have made a start using another example I had but am trying to figure where to go next with it:


--------------------------------------------------------------------------------


The default.aspx page (which is used as the image source for an image-control in any other page):
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="display.aspx.vb" Inherits="pages_new_label" Theme="" StyleSheetTheme="" %>


--------------------------------------------------------------------------------


The default.aspx.vb page:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Response.ContentType = "image/png"
Response.BufferOutput = True

Dim bmp As Bitmap = fGetNewLabelImage()
Dim ms As MemoryStream = New MemoryStream()

bmp.Save(ms, ImageFormat.Png)
ms.WriteTo(Response.OutputStream)

bmp.Dispose()
Response.Flush()

End Sub


Public Function fGetNewLabelImage() As Image

Dim imgBackground As Bitmap
Dim g As Graphics = Graphics.FromImage(imgBackground)

g.SmoothingMode = SmoothingMode.HighQuality
Dim rectangleFont8 As New Font("Arial", 8, FontStyle.Regular)
g.DrawString("Hello", rectangleFont8, Brushes.Black, 5, 5)

g.Dispose()
Return imgBackground

End Function


--------------------------------------------------------------------------------
 
"Mark B" wrote
In ASP.NET VB.NET, I have a variable, strMyText that can be any text up to
255 characters.
I want be able to create a bitmap that has the text inside a bordered
rectangle like these:
So the rectangle needs to stretch depending on the text length.

I think using Graphics.MeasureString will enable you to determine the size
of the rectangle to create.

Andrew
 
Back
Top