Mit folgener Klasse, kann man Fonts direkt aus den Resources verwenden,
ohne eine Physikalische Kopie auf die Festplatte schreiben zu müssen.
FontFromRecource
History
Anwendung
Wenn man Die Variable
paßt sich das ganze an die Standardgröße an.
Wenn man im Parameter
Achtung:
Wird von VS als "Nicht spezifisch genug" bemängelt, da es sich beim zweiten Parameter (
um den Datentyp
Also muß mindestens einer von den beiden Parametern, explizit mit Datentyp übergeben werden.
ohne eine Physikalische Kopie auf die Festplatte schreiben zu müssen.
VB.NET-Quellcode
- ' <version: 0.2 beta, source: "https://www.vb-paradise.de/index.php/Thread/127251-Fonts-aus-Resources-nutzen-Ohne-physikalische-Kopie-auf-Festplatte/">
- ' <licence: Creative Commons Public Domain (CC0 1.0), "https://creativecommons.org/publicdomain/zero/1.0/deed.en">
- Imports System.Runtime.InteropServices
- Imports System.Drawing.Text
- Public Class FontFromRecource
- #Region "Public Methods (Overloads)"
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, 0, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal graphicsUnit As GraphicsUnit,
- ByVal gdiCharSet As Byte,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle) As Font
- Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, 0, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal graphicsUnit As GraphicsUnit) As Font
- Return GetFont(fontData, emSize, fontStyle, graphicsUnit, 0, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal graphicsUnit As GraphicsUnit,
- ByVal gdiCharSet As Byte) As Font
- Return GetFont(fontData, emSize, fontStyle, graphicsUnit, gdiCharSet, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal graphicsUnit As GraphicsUnit,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, fontStyle, graphicsUnit, 0, gdiVerticalFont)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal gdiCharSet As Byte) As Font
- Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, gdiCharSet, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, 0, gdiVerticalFont)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal gdiCharSet As Byte,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, fontStyle, GraphicsUnit.Point, gdiCharSet, gdiVerticalFont)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal graphicsUnit As GraphicsUnit) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, 0, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal graphicsUnit As GraphicsUnit,
- ByVal gdiCharSet As Byte) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, gdiCharSet, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal graphicsUnit As GraphicsUnit,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, 0, gdiVerticalFont)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal graphicsUnit As GraphicsUnit,
- ByVal gdiCharSet As Byte,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, graphicsUnit, gdiCharSet, gdiVerticalFont)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal gdiCharSet As Byte) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet, False)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal gdiCharSet As Byte,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet, gdiVerticalFont)
- End Function
- Public Shared Function [Get](ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal gdiVerticalFont As Boolean) As Font
- Return GetFont(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, 0, gdiVerticalFont)
- End Function
- #End Region
- Private Shared Function GetFont(ByVal fontData As Byte(),
- ByVal emSize As Single,
- ByVal fontStyle As FontStyle,
- ByVal graphicsUnit As GraphicsUnit,
- ByVal gdiCharSet As Byte,
- ByVal gdiVerticalFont As Boolean) As Font
- Dim unsafeMemoryBlock As IntPtr = Marshal.AllocCoTaskMem(fontData.Length)
- Marshal.Copy(fontData, 0, unsafeMemoryBlock, fontData.Length)
- NativeMethods.AddFontMemResourceEx(unsafeMemoryBlock, CUInt(fontData.Length), IntPtr.Zero, 0)
- Dim privateFonts As New PrivateFontCollection
- privateFonts.AddMemoryFont(unsafeMemoryBlock, fontData.Length)
- Return New Font(privateFonts.Families(0), emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont)
- privateFonts.Dispose()
- Marshal.FreeCoTaskMem(unsafeMemoryBlock)
- End Function
- Private Class NativeMethods
- <DllImport("gdi32.dll")>
- Public Shared Function AddFontMemResourceEx(ByVal pbFont As IntPtr, ByVal cbFont As UInteger, ByVal pdv As IntPtr, <[In]> ByRef pcFonts As UInteger) As IntPtr
- End Function
- End Class
- End Class
Übersetzung von VB.Net zu C#, von Facebamm.
C#-Quellcode
- // <version: 0.2, source: "https://www.vb-paradise.de/index.php/Thread/127251-Fonts-aus-Resources-nutzen-Ohne-physikalische-Kopie-auf-Festplatte/">
- // <licence: Creative Commons Public Domain (CC0 1.0), "https://creativecommons.org/publicdomain/zero/1.0/deed.en">
- using System;
- using System.Drawing;
- using System.Drawing.Text;
- using System.Runtime.InteropServices;
- using System.Runtime.CompilerServices;
- public class FontFromRecource {
- #region Public Methods (Overloads)
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Font Get(byte[] fontData, float emSize, bool gdiVerticalFont) {
- return Get(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, 0, gdiVerticalFont);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Font Get(byte[] fontData, float emSize, byte gdiCharSet, bool gdiVerticalFont = false) {
- return Get(fontData, emSize, FontStyle.Regular, GraphicsUnit.Point, gdiCharSet, gdiVerticalFont);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Font Get(byte[] fontData, float emSize, GraphicsUnit graphicsUnit, byte gdiCharSet = 0, bool gdiVerticalFont = false) {
- return Get(fontData, emSize, FontStyle.Regular, graphicsUnit, gdiCharSet, gdiVerticalFont);
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static Font Get(byte[] fontData, float emSize, FontStyle fontStyle = FontStyle.Regular, GraphicsUnit graphicsUnit = GraphicsUnit.Point, byte gdiCharSet = 0, bool gdiVerticalFont = false) {
- return GetFont(fontData, emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont);
- }
- #endregion
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- private static Font GetFont(byte[] fontData, float emSize, FontStyle fontStyle, GraphicsUnit graphicsUnit, byte gdiCharSet, bool gdiVerticalFont) {
- IntPtr unsafeMemoryBlock = Marshal.AllocCoTaskMem(fontData.Length);
- Marshal.Copy(fontData, 0, unsafeMemoryBlock, fontData.Length);
- uint pcFont = 0;
- NativeMethods.AddFontMemResourceEx(unsafeMemoryBlock,(uint) fontData.Length, IntPtr.Zero, ref pcFont);
- PrivateFontCollection privateFonts = new PrivateFontCollection();
- privateFonts.AddMemoryFont(unsafeMemoryBlock, fontData.Length);
- return new Font(privateFonts.Families[0], emSize, fontStyle, graphicsUnit, gdiCharSet, gdiVerticalFont);
- privateFonts.Dispose();
- Marshal.FreeCoTaskMem(unsafeMemoryBlock);
- }
- private class NativeMethods {
- [DllImport("gdi32.dll")]
- public static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFont);
- }
- }
0.2 : Redundante Variable
0.1 : Release
pcFonts
ersatzlos entfernt.0.1 : Release
Wenn man Die Variable
emSize
auf Me.Font.Size
setzt,paßt sich das ganze an die Standardgröße an.
Wenn man im Parameter
fontStyle
mehrere FontStyle
verwenden will, muß man eine Variable erstellen:Achtung:
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.
bit.ly/DotNetSearch
Code Konverter:
Online: @ developerfusion.com, @ telerik.com
Offline stand alone: Convert .NET, C# to VB Converter
VS Extensions: Convert .NET (Based on .NET 4.5), Language Convert
Code Konverter:
Online: @ developerfusion.com, @ telerik.com
Offline stand alone: Convert .NET, C# to VB Converter
VS Extensions: Convert .NET (Based on .NET 4.5), Language Convert
Dieser Beitrag wurde bereits 8 mal editiert, zuletzt von „FormFollowsFunction“ ()