0

I'm trying to access to a font programmatically, because i cant install fonts on the sharing hosting

I use this code

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim collection As New PrivateFontCollection()
    collection.AddFontFile(Server.MapPath("~\ShadowsIntoLight.ttf"))

    Dim image As New Bitmap(500, 500)
    Dim g As Graphics = Graphics.FromImage(image)

    Dim RedBrush As New SolidBrush(Color.Black)
    Dim drawFont As New Font("Shadows Into Light", 36)

    g.DrawString("the lazy fox jumped over the brown log", drawFont, RedBrush, 0, 100)

    Dim path As String = Server.MapPath("~/image.png")
    image.Save(path, ImageFormat.Png)
    g.Dispose()
    image.Dispose()

    Response.Write("<img src=""image.png""/>")

End Sub

but it always displays the Arial font. How can I make it display the specific font

Thank you

Yusuf
  • 140
  • 11

1 Answers1

0

Could you not use CSS in the background? Add the custom font to your solution in a 'Fonts folder' > add existing item to fonts folder in visual studio

Example: (Using a random font I downloaded)

@font-face{
font-family: myFont;
src: url(/Fonts/Belleza-Regular.ttf);

}

.MyClass{
color:green;
font-family: myFont;
 }

Then append this font anywhere within your code?

myControl.CssClass = "MyClass" etc..



Might be a slightly longer way than you wanted and would only work if you're appending to controls and such however might be a nice workaround.

Edit:

maybe something like this? Using custom TTF font for DrawString image rendering

Yusuf
  • 140
  • 11
dan6657
  • 117
  • 2
  • 11
  • I want to access the font using GDI+ but not using CSS – Yusuf Nov 03 '17 at 13:46
  • 1
    Ah okay, maybe something like this? https://stackoverflow.com/questions/523246/using-custom-ttf-font-for-drawstring-image-rendering – dan6657 Nov 03 '17 at 14:01
  • 1
    this fix the problem i edit the code to be: g.DrawString("the lazy fox...", myCustomFont, RedBrush, 0, 100) – Yusuf Nov 03 '17 at 16:11
  • glad you fixed - sorry my answers couldnt help! – dan6657 Nov 03 '17 at 16:12
  • 1
    Your second answer was helpful. and for a long time I was looking for an answer, I am new to the stackoverflow and i dont know how to set your answer as the most helpfull. – Yusuf Nov 03 '17 at 17:08