Question problems with drawing a font using DrawString

DanielFL

Member
Joined
Feb 15, 2009
Messages
5
Programming Experience
3-5
hello. I have tested many fonts but one that I need (a barcode 128 to download here: http://www.jtbarton.com/Barcodes/code128.ttf ) does not work with DrawString. I tried with encoded barcode128 code as well as with regular string. In both examples it works in Word, Excel, also font after being install comes out as tru type font. I am confused why it wont work with VB (it simple shows Arial font instead).
Here is my code:
VB.NET:
Expand Collapse Copy
    Dim bm As New Bitmap(1256, 256)
    Dim gr As Graphics = Graphics.FromImage(bm)

        Dim barcodeFontSize As Integer = 40
        Dim numericFontSize As Integer = 12

        gr.SmoothingMode = Drawing2D.SmoothingMode.None
        gr.TextRenderingHint = TextRenderingHint.SingleBitPerPixelGridFit

        Dim pfc As PrivateFontCollection = New PrivateFontCollection
        pfc.AddFontFile("c:/windows/fonts/code128.ttf")

        Dim barcodeText As String = "aaaaaacxcjwedhqwsidasd"

        gr.Clear(Color.White)

        Dim ff As FontFamily = pfc.Families(0)
        Dim f As New Font(ff, barcodeFontSize)

        gr.DrawString(barcodeText, f, Brushes.Black, 10, 10)

       bm.Save("c:\test.gif", ImageFormat.Gif)


'... I also tried without adding font this way:

      Dim fontFamily As New FontFamily("Code 128")
        Dim fn As New Font(fontFamily, 36, FontStyle.Bold, GraphicsUnit.Point)

       gr.DrawString("aaaaaacxcjwedhqwsidasd", fn, Brushes.Black, 15, 10)

'.. but also just plain Arial comes out.
Any help will be gladly appreciated!!
Thank you
 
Last edited by a moderator:
Why use a font at all? Write your own function to draw the barcode directly - it's a lot easier in the long run :D Besides, when you get to 2D barcodes like DataMatrix, you cant *easily* do it using a font.

Have a look at The code 128
 
Why use a font at all? Write your own function to draw the barcode directly - it's a lot easier in the long run :D Besides, when you get to 2D barcodes like DataMatrix, you cant *easily* do it using a font.

Have a look at The code 128

Excellent link. The DataMatrix example should save me some Googling in the near future.
 
Back
Top