Fonts aus Resources nutzen (Ohne physikalische Kopie auf Festplatte)

    • VB.NET

    Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von Facebamm.

      Fonts aus Resources nutzen (Ohne physikalische Kopie auf Festplatte)

      Mit folgener Klasse, kann man Fonts direkt aus den Resources verwenden,
      ohne eine Physikalische Kopie auf die Festplatte schreiben zu müssen.

      FontFromRecource


      VB.NET-Quellcode

      1. ' <version: 0.2 beta, source: "https://www.vb-paradise.de/index.php/Thread/127251-Fonts-aus-Resources-nutzen-Ohne-physikalische-Kopie-auf-Festplatte/">
      2. ' <licence: Creative Commons Public Domain (CC0 1.0), "https://creativecommons.org/publicdomain/zero/1.0/deed.en">
      3. Imports System.Runtime.InteropServices
      4. Imports System.Drawing.Text
      5. Public Class FontFromRecource
      6. #Region "Public Methods (Overloads)"
      7. Public Shared Function [Get](ByVal fontData As Byte(),
      8. ByVal emSize As Single) As Font
      9. Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, 0, False)
      10. End Function
      11. Public Shared Function [Get](ByVal fontData As Byte(),
      12. ByVal emSize As Single,
      13. ByVal fontStyle As FontStyle,
      14. ByVal graphicsUnit As GraphicsUnit,
      15. ByVal gdiCharSet As Byte,
      16. ByVal gdiVerticalFont As Boolean) As Font
      17. Return GetFont(fontData, emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont)
      18. End Function
      19. Public Shared Function [Get](ByVal fontData As Byte(),
      20. ByVal emSize As Single,
      21. ByVal fontStyle As FontStyle) As Font
      22. Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, 0, False)
      23. End Function
      24. Public Shared Function [Get](ByVal fontData As Byte(),
      25. ByVal emSize As Single,
      26. ByVal fontStyle As FontStyle,
      27. ByVal graphicsUnit As GraphicsUnit) As Font
      28. Return GetFont(fontData, emSize, fontStyle, graphicsUnit, 0, False)
      29. End Function
      30. Public Shared Function [Get](ByVal fontData As Byte(),
      31. ByVal emSize As Single,
      32. ByVal fontStyle As FontStyle,
      33. ByVal graphicsUnit As GraphicsUnit,
      34. ByVal gdiCharSet As Byte) As Font
      35. Return GetFont(fontData, emSize, fontStyle, graphicsUnit, gdiCharSet, False)
      36. End Function
      37. Public Shared Function [Get](ByVal fontData As Byte(),
      38. ByVal emSize As Single,
      39. ByVal fontStyle As FontStyle,
      40. ByVal graphicsUnit As GraphicsUnit,
      41. ByVal gdiVerticalFont As Boolean) As Font
      42. Return GetFont(fontData, emSize, fontStyle, graphicsUnit, 0, gdiVerticalFont)
      43. End Function
      44. Public Shared Function [Get](ByVal fontData As Byte(),
      45. ByVal emSize As Single,
      46. ByVal fontStyle As FontStyle,
      47. ByVal gdiCharSet As Byte) As Font
      48. Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, gdiCharSet, False)
      49. End Function
      50. Public Shared Function [Get](ByVal fontData As Byte(),
      51. ByVal emSize As Single,
      52. ByVal fontStyle As FontStyle,
      53. ByVal gdiVerticalFont As Boolean) As Font
      54. Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, 0, gdiVerticalFont)
      55. End Function
      56. Public Shared Function [Get](ByVal fontData As Byte(),
      57. ByVal emSize As Single,
      58. ByVal fontStyle As FontStyle,
      59. ByVal gdiCharSet As Byte,
      60. ByVal gdiVerticalFont As Boolean) As Font
      61. Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, gdiCharSet, gdiVerticalFont)
      62. End Function
      63. Public Shared Function [Get](ByVal fontData As Byte(),
      64. ByVal emSize As Single,
      65. ByVal graphicsUnit As GraphicsUnit) As Font
      66. Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, 0, False)
      67. End Function
      68. Public Shared Function [Get](ByVal fontData As Byte(),
      69. ByVal emSize As Single,
      70. ByVal graphicsUnit As GraphicsUnit,
      71. ByVal gdiCharSet As Byte) As Font
      72. Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, gdiCharSet, False)
      73. End Function
      74. Public Shared Function [Get](ByVal fontData As Byte(),
      75. ByVal emSize As Single,
      76. ByVal graphicsUnit As GraphicsUnit,
      77. ByVal gdiVerticalFont As Boolean) As Font
      78. Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, 0, gdiVerticalFont)
      79. End Function
      80. Public Shared Function [Get](ByVal fontData As Byte(),
      81. ByVal emSize As Single,
      82. ByVal graphicsUnit As GraphicsUnit,
      83. ByVal gdiCharSet As Byte,
      84. ByVal gdiVerticalFont As Boolean) As Font
      85. Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, gdiCharSet, gdiVerticalFont)
      86. End Function
      87. Public Shared Function [Get](ByVal fontData As Byte(),
      88. ByVal emSize As Single,
      89. ByVal gdiCharSet As Byte) As Font
      90. Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet, False)
      91. End Function
      92. Public Shared Function [Get](ByVal fontData As Byte(),
      93. ByVal emSize As Single,
      94. ByVal gdiCharSet As Byte,
      95. ByVal gdiVerticalFont As Boolean) As Font
      96. Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet, gdiVerticalFont)
      97. End Function
      98. Public Shared Function [Get](ByVal fontData As Byte(),
      99. ByVal emSize As Single,
      100. ByVal gdiVerticalFont As Boolean) As Font
      101. Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, 0, gdiVerticalFont)
      102. End Function
      103. #End Region
      104. Private Shared Function GetFont(ByVal fontData As Byte(),
      105. ByVal emSize As Single,
      106. ByVal fontStyle As FontStyle,
      107. ByVal graphicsUnit As GraphicsUnit,
      108. ByVal gdiCharSet As Byte,
      109. ByVal gdiVerticalFont As Boolean) As Font
      110. Dim unsafeMemoryBlock As IntPtr = Marshal.AllocCoTaskMem(fontData.Length)
      111. Marshal.Copy(fontData, 0, unsafeMemoryBlock, fontData.Length)
      112. NativeMethods.AddFontMemResourceEx(unsafeMemoryBlock, CUInt(fontData.Length), IntPtr.Zero, 0)
      113. Dim privateFonts As New PrivateFontCollection
      114. privateFonts.AddMemoryFont(unsafeMemoryBlock, fontData.Length)
      115. Return New Font(privateFonts.Families(0), emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont)
      116. privateFonts.Dispose()
      117. Marshal.FreeCoTaskMem(unsafeMemoryBlock)
      118. End Function
      119. Private Class NativeMethods
      120. <DllImport("gdi32.dll")>
      121. Public Shared Function AddFontMemResourceEx(ByVal pbFont As IntPtr, ByVal cbFont As UInteger, ByVal pdv As IntPtr, <[In]> ByRef pcFonts As UInteger) As IntPtr
      122. End Function
      123. End Class
      124. End Class

      Übersetzung von VB.Net zu C#, von Facebamm. :thumbup:

      C#-Quellcode

      1. // <version: 0.2, source: "https://www.vb-paradise.de/index.php/Thread/127251-Fonts-aus-Resources-nutzen-Ohne-physikalische-Kopie-auf-Festplatte/">
      2. // <licence: Creative Commons Public Domain (CC0 1.0), "https://creativecommons.org/publicdomain/zero/1.0/deed.en">
      3. using System;
      4. using System.Drawing;
      5. using System.Drawing.Text;
      6. using System.Runtime.InteropServices;
      7. using System.Runtime.CompilerServices;
      8. public class FontFromRecource {
      9. #region Public Methods (Overloads)
      10. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      11. public static Font Get(byte[] fontData, float emSize, bool gdiVerticalFont) {
      12. return Get(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, 0, gdiVerticalFont);
      13. }
      14. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      15. public static Font Get(byte[] fontData, float emSize, byte gdiCharSet, bool gdiVerticalFont = false) {
      16. return Get(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet, gdiVerticalFont);
      17. }
      18. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      19. public static Font Get(byte[] fontData, float emSize, GraphicsUnit graphicsUnit, byte gdiCharSet = 0, bool gdiVerticalFont = false) {
      20. return Get(fontData, emSize, FontStyle.Regular, graphicsUnit, gdiCharSet, gdiVerticalFont);
      21. }
      22. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      23. public static Font Get(byte[] fontData, float emSize, FontStyle fontStyle = FontStyle.Regular, GraphicsUnit graphicsUnit = GraphicsUnit.Point, byte gdiCharSet = 0, bool gdiVerticalFont = false) {
      24. return GetFont(fontData, emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont);
      25. }
      26. #endregion
      27. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      28. private static Font GetFont(byte[] fontData, float emSize, FontStyle fontStyle, GraphicsUnit graphicsUnit, byte gdiCharSet, bool gdiVerticalFont) {
      29. IntPtr unsafeMemoryBlock = Marshal.AllocCoTaskMem(fontData.Length);
      30. Marshal.Copy(fontData, 0, unsafeMemoryBlock, fontData.Length);
      31. uint pcFont = 0;
      32. NativeMethods.AddFontMemResourceEx(unsafeMemoryBlock,(uint) fontData.Length, IntPtr.Zero, ref pcFont);
      33. PrivateFontCollection privateFonts = new PrivateFontCollection();
      34. privateFonts.AddMemoryFont(unsafeMemoryBlock, fontData.Length);
      35. return new Font(privateFonts.Families[0], emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont);
      36. privateFonts.Dispose();
      37. Marshal.FreeCoTaskMem(unsafeMemoryBlock);
      38. }
      39. private class NativeMethods {
      40. [DllImport("gdi32.dll")]
      41. public static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFont);
      42. }
      43. }


      History
      0.2 : Redundante Variable pcFonts ersatzlos entfernt.
      0.1 : Release

      Anwendung

      VB.NET-Quellcode

      1. Private MyFont As Font = FontFromRecource.Get(My.Resources.YOURFONT, 8)


      Wenn man Die Variable emSize auf Me.Font.Size setzt,
      paßt sich das ganze an die Standardgröße an.

      VB.NET-Quellcode

      1. Private MyFont As Font = FontFromRecource.Get(My.Resources.YOURFONT, Me.Font.Size, FontStyle.Bold)


      Wenn man im Parameter fontStyle mehrere FontStyle verwenden will, muß man eine Variable erstellen:

      VB.NET-Quellcode

      1. Private MyFontStyle As FontStyle = FontStyle.Bold Or FontStyle.Underline
      2. Private MyFont As Font = FontFromRecource.Get(My.Resources.YOURFONT, Me.Font.Size, MyFontStyle)


      Achtung:

      VB.NET-Quellcode

      1. FontFromRecource.Get(My.Resources.YOURFONT, 8, 0)

      Wird von VS als "Nicht spezifisch genug" bemängelt, da es sich beim zweiten Parameter (emSize)
      um den Datentyp Single handelt und beim dritten Parameter (gdiCharSet) um den Datentyp Byte.
      Also muß mindestens einer von den beiden Parametern, explizit mit Datentyp übergeben werden.

      VB.NET-Quellcode

      1. FontFromRecource.Get(My.Resources.YOURFONT, 8, CByte(0))
      Dateien

      Dieser Beitrag wurde bereits 8 mal editiert, zuletzt von „FormFollowsFunction“ ()

      @FormFollowsFunction Vielleicht schreibst Du rein, was anders / neu ist, damit die Leute nicht so lange nach den Unterschieden suchen müssen bzw. diese Version ignorieren können.
      Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
      Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
      Ein guter .NET-Snippetkonverter (der ist verfügbar).
      Programmierfragen über PN / Konversation werden ignoriert!
      Habe ich schon. Im spoiler "FontFromRecource.vb", befindet sich noch ein spoiler namens "History".
      Zu versteckt ?

      FormFollowsFunction schrieb:

      Zu versteckt ?
      Ja.
      Pack das in nen separaten Spoiler. ;)
      Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
      Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
      Ein guter .NET-Snippetkonverter (der ist verfügbar).
      Programmierfragen über PN / Konversation werden ignoriert!
      Ich habe alles soweit eingefügt und geändert, aber es kommt folgender Fehler:

      VB.NET-Quellcode

      1. Fehler BC30247 "Private" ist in der Deklaration von lokalen Variablen ungültig. ELW Dashboa xxxxx \fontcontroll.vb 75


      Der Code

      VB.NET-Quellcode

      1. Private MyFontStyle As FontStyle = FontStyle.Bold Or FontStyle.Underline
      2. Private MyFont As Font = FontFromRecource.Get(My.Resources.FruCe55Ro, Me.Font.Size, MyFontStyle)
      3. Label1.Font = FontFromRecource.Get(My.Resources.FruCe55Ro, 8, CByte(0))

      liegt im Load
      @proxy_sam OMG :/
      Innerhalb einer Prozedur kann nicht Private stehen, da muss Dim stehen.
      Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
      Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
      Ein guter .NET-Snippetkonverter (der ist verfügbar).
      Programmierfragen über PN / Konversation werden ignoriert!
      @proxy_sam

      Private MyFont As Font = FontFromRecource.Get(My.Resources.FruCe55Ro, Me.Font.Size, MyFontStyle)
      Ist eine globale Variable, die in der ganzen Klasse verfügbar ist, nicht jedoch außerhalb der Klasse (Das wäre dann Public)
      Innerhalb eine Prozedur (Methode) muß mit Dim deklariert werden (Globale Variablen können auch mir Dim deklariert werden).
      Methodenvariablen werden in der Regel (sollten zumindest) klein, globale Variablen groß geschrieben werden.
      Für die Personen, die auf C# stehen ;D

      C#-Quellcode

      1. using System;
      2. using System.Drawing;
      3. using System.Drawing.Text;
      4. using System.Runtime.InteropServices;
      5. using System.Runtime.CompilerServices;
      6. public class FontFromRecource {
      7. #region Public Methods (Overloads)
      8. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      9. public static Font Get(byte[] fontData, float emSize, bool gdiVerticalFont) {
      10. return Get(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, 0, gdiVerticalFont);
      11. }
      12. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      13. public static Font Get(byte[] fontData, float emSize, byte gdiCharSet, bool gdiVerticalFont = false) {
      14. return Get(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet, gdiVerticalFont);
      15. }
      16. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      17. public static Font Get(byte[] fontData, float emSize, GraphicsUnit graphicsUnit, byte gdiCharSet = 0, bool gdiVerticalFont = false) {
      18. return Get(fontData, emSize, FontStyle.Regular, graphicsUnit, gdiCharSet, gdiVerticalFont);
      19. }
      20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      21. public static Font Get(byte[] fontData, float emSize, FontStyle fontStyle = FontStyle.Regular, GraphicsUnit graphicsUnit = GraphicsUnit.Point, byte gdiCharSet = 0, bool gdiVerticalFont = false) {
      22. return GetFont(fontData, emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont);
      23. }
      24. [MethodImpl(MethodImplOptions.AggressiveInlining)]
      25. private static Font GetFont(byte[] fontData, float emSize, FontStyle fontStyle, GraphicsUnit graphicsUnit, byte gdiCharSet, bool gdiVerticalFont) {
      26. IntPtr unsafeMemoryBlock = Marshal.AllocCoTaskMem(fontData.Length);
      27. Marshal.Copy(fontData, 0, unsafeMemoryBlock, fontData.Length);
      28. uint pcFont = 0;
      29. NativeMethods.AddFontMemResourceEx(unsafeMemoryBlock,(uint) fontData.Length, IntPtr.Zero, ref pcFont);
      30. PrivateFontCollection privateFonts = new PrivateFontCollection();
      31. privateFonts.AddMemoryFont(unsafeMemoryBlock, fontData.Length);
      32. return new Font(privateFonts.Families[0], emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont);
      33. privateFonts.Dispose();
      34. Marshal.FreeCoTaskMem(unsafeMemoryBlock);
      35. }
      36. #endregion
      37. private class NativeMethods {
      38. [DllImport("gdi32.dll")]
      39. public static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFont);
      40. }
      41. }