none
Embedded Resources RRS feed

  • Question

  • Without installation font support only label1. but TextBox1 not supporting we must install font. How to support font without installation font in TextBox1. Code is under below kindly check this and solve this issue.

    **********************************************************************************************

    Imports System.Reflection
    Imports System.Drawing.Text
    Imports System.Runtime.InteropServices
    Imports System.IO
    Imports System.Resources

    Public Class Form1
        Public _pfc As PrivateFontCollection = Nothing
        Public ReadOnly Property GetFont(ByVal Size As Single, ByVal style As FontStyle) As Font
            Get
                If _pfc Is Nothing Then LoadFont()
                Return New Font(_pfc.Families(0), Size, style)
            End Get
        End Property

        Public Property StartingIndex As Integer

        Private Sub LoadFont()
            _pfc = New PrivateFontCollection
            Dim FontMemPointer As IntPtr = Marshal.AllocCoTaskMem(My.Resources.MyFont.Length)
            Marshal.Copy(My.Resources.MyFont, StartingIndex, FontMemPointer, My.Resources.MyFont.Length)
            _pfc.AddMemoryFont(FontMemPointer, My.Resources.MyFont.Length)
            Marshal.FreeCoTaskMem(FontMemPointer)
        End Sub

        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
              Label1.Font = GetFont(20, FontStyle.Regular)

              TextBox1.Font = GetFont(20, FontStyle.Regular)

        End Sub
    End Class

    Sunday, August 4, 2019 6:33 AM

Answers

  • A way =>

    (test with embedded "Amazing_Kids" font)

    Dim bytes() As Byte = My.Resources.Amazing_Kids
    Dim pResource As IntPtr = Marshal.AllocCoTaskMem(bytes.Length)
    Marshal.Copy(bytes, 0, pResource, bytes.Length)
    Dim nNumFonts As Integer = 0
    AddFontMemResourceEx(pResource, bytes.Length, IntPtr.Zero, nNumFonts)
    Dim fonts As PrivateFontCollection = New PrivateFontCollection
    fonts.AddMemoryFont(pResource, bytes.Length)
    Marshal.FreeCoTaskMem(pResource)
    TextBox1.Font = New Font(fonts.Families(0), 20)

    with :

       <DllImport("Gdi32.dll", SetLastError:=True)>
        Private Shared Function AddFontMemResourceEx(pFileView As IntPtr, cjSize As Integer, pvResrved As IntPtr, <Out> ByRef pNumFonts As Integer) As IntPtr


    • Edited by Castorix31 Sunday, August 4, 2019 8:11 AM
    • Marked as answer by CompuTechSide Sunday, August 4, 2019 12:04 PM
    Sunday, August 4, 2019 8:09 AM

All replies

  • A way =>

    (test with embedded "Amazing_Kids" font)

    Dim bytes() As Byte = My.Resources.Amazing_Kids
    Dim pResource As IntPtr = Marshal.AllocCoTaskMem(bytes.Length)
    Marshal.Copy(bytes, 0, pResource, bytes.Length)
    Dim nNumFonts As Integer = 0
    AddFontMemResourceEx(pResource, bytes.Length, IntPtr.Zero, nNumFonts)
    Dim fonts As PrivateFontCollection = New PrivateFontCollection
    fonts.AddMemoryFont(pResource, bytes.Length)
    Marshal.FreeCoTaskMem(pResource)
    TextBox1.Font = New Font(fonts.Families(0), 20)

    with :

       <DllImport("Gdi32.dll", SetLastError:=True)>
        Private Shared Function AddFontMemResourceEx(pFileView As IntPtr, cjSize As Integer, pvResrved As IntPtr, <Out> ByRef pNumFonts As Integer) As IntPtr


    • Edited by Castorix31 Sunday, August 4, 2019 8:11 AM
    • Marked as answer by CompuTechSide Sunday, August 4, 2019 12:04 PM
    Sunday, August 4, 2019 8:09 AM
  • Thanks Castorix31 I was in trouble last four to five months but now issue is resolve Thanksssssssssssssssssssssssssssssssssssssssssssssssssssssss again Best Regards
    Sunday, August 4, 2019 12:04 PM