Building button graphics from a "template" bitmap

R

RSH

Hi,

I am building a Win Forms application that I would like to feed a function a
collection of strings (button text) that the program will go through and
generate and save the generated buttons.

The issue Im having is that I cant find any tutorials on how to do the
following:

I have three graphics the Right End Cap the center (which I want to tile to
fit the text) and the left end cap. I want to generate the text calculate
the width of the text, tile the center section and center the button text
then put the left and right end cap graphics on either side and save the
graphic.

So far this is what I came up with (simple text on graphic then saving it):

Sub createButtons()

Dim imgPath As String = "\DynamicButtonMaker\DynamicButtonMaker\"

Dim bmp As Bitmap = New Bitmap(imgPath & "Middle.jpg")

Dim e As Graphics = Graphics.FromImage(bmp)

e.TextRenderingHint = Text.TextRenderingHint.AntiAlias

e.DrawString("Test Button", New Font("Verdana", 24, FontStyle.Bold), New
SolidBrush(Color.White), 0, 0)

bmp.Save(imgPath & "TestButton.jpg")

End Sub



Thanks for any help you might be able to provide.



Ron
 
R

RSH

Okay I have it almost working, the only problem is the right end cap wont
draw correctly, it seems to be drawing a half of itself and then drawing the
other half in the alotted area, it is very peculiar!

What am I doing wrong with regards to the right cap image?

Sub Main()

Dim aList As New ArrayList

aList.Add("Button 1 large")

aList.Add("Button 2 Larger")

aList.Add("Button 3 Even Much Larger")

For i As Integer = 0 To aList.Count - 1

createButtons(aList(i), "Verdana", 6, Color.White)

Next

End Sub

Sub createButtons(ByVal strButtonText As String, ByVal strFont As String,
ByVal iSize As Integer, ByVal clrColor As System.Drawing.Color)

Dim imgPath As String = "C:\Documents and Settings\rherhuth\My
Documents\Visual Studio
2005\Projects\DynamicButtonMaker\DynamicButtonMaker\"

Dim inputbmp As Bitmap

Dim eWidth As Integer = 0

Dim eHeight As Integer = 0

Dim bmpHeight As Integer = 0

Dim bmpWidth As Integer = 0

Dim ine As Graphics

Dim oute As Graphics

Dim eFont As Font

Dim stringFormat As StringFormat

Dim rect1 As Rectangle

Dim texture As TextureBrush

Dim buttonCanvas As Rectangle

Dim outputbmp As Bitmap

Dim leftcapbmp As Bitmap

Dim rightcapbmp As Bitmap



inputbmp = New Bitmap(imgPath & "Middle.jpg")

leftcapbmp = New Bitmap(imgPath & "LeftEnd.jpg")

rightcapbmp = New Bitmap(imgPath & "RightEnd.jpg")

ine = Graphics.FromImage(inputbmp)

stringFormat = New StringFormat()

stringFormat.Alignment = StringAlignment.Center

stringFormat.LineAlignment = StringAlignment.Center

eFont = New Font(strFont, iSize, FontStyle.Bold)

eWidth = ine.MeasureString(strButtonText, New Font(strFont, iSize,
FontStyle.Bold)).Width

eHeight = ine.MeasureString(strButtonText, New Font(strFont, iSize,
FontStyle.Bold)).Height

bmpHeight = inputbmp.Height

bmpWidth = eWidth + leftcapbmp.Width + rightcapbmp.Width + 10

outputbmp = New Bitmap(bmpWidth - 2, bmpHeight)

oute = Graphics.FromImage(outputbmp)

oute.CompositingQuality = Drawing2D.CompositingQuality.HighQuality

oute.TextRenderingHint = Text.TextRenderingHint.AntiAlias

buttonCanvas = New Rectangle(0, 0, bmpWidth, bmpHeight)

oute.FillRectangle(New TextureBrush(inputbmp), buttonCanvas)

oute.FillRegion(New TextureBrush(leftcapbmp), New Region(New Rectangle(0, 0,
leftcapbmp.Width, leftcapbmp.Height)))

oute.FillRegion(New TextureBrush(rightcapbmp), New Region(New
Rectangle(bmpWidth - rightcapbmp.Width, 0, rightcapbmp.Width,,
rightcapbmp.Height)))

oute.DrawString(strButtonText, eFont, New SolidBrush(clrColor),
buttonCanvas, stringFormat)

outputbmp.Save(imgPath & "BTN_" & Replace(strButtonText, " ", "_") & ".jpg")

ine.Dispose()

oute.Dispose()

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top