Laden...

Fonts aus Resources

Letzter Beitrag vor 14 Jahren 6 Posts 3.495 Views
Fonts aus Resources

Hallo liebe Community,

weiß jemand, ob es möglich ist eigene Fonts mit in die Resources aufzunehmen und von dort zu benutzen? Hab gerade schon im Netz gestöbert und hier gesucht, aber habe irgendwie nichts passendes gefunden.

Grüße,
Phreakadelle

...

Hallo phreakadelle,

also erstmal: du kannst beliebige Dateien in die Ressourcen packen und auslesen.

Das direkte Verwenden könnte schon schwieriger werden. Aber zumindest True-Type-Fonts muss mal wohl nicht unbedingt installieren.

Zu dem Thema müsste es aber eigentlich auch schon Beiträge hier im Forum, mindestens aber irgendwo im Netz, geben.

herbivore

Das leidige Thema hatte ich leider auch schon:


private PrivateFontCollection pfc = new PrivateFontCollection();

 [DllImport("gdi32.dll")] 
        private static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts); 

 private void LoadIt()
        {

            Stream resStream = this.GetType().Assembly.GetManifestResourceStream ("WindowsFormsApplication7.Resources.FONTNAME.TTF");
            IntPtr fontptr = Marshal.AllocCoTaskMem (Convert.ToInt32(resStream.Length));
            byte[] theFont = new byte[resStream.Length];
            resStream.Read(theFont, 0, Convert.ToInt32(resStream.Length));
            Marshal.Copy(theFont, 0, fontptr, Convert.ToInt32(resStream.Length));
            uint ret = 0;
            AddFontMemResourceEx(fontptr, (uint)theFont.Length, IntPtr.Zero, ref ret);
            pfc.AddMemoryFont(fontptr, Convert.ToInt32(resStream.Length));

            resStream.Close();
            Marshal.FreeCoTaskMem(fontptr);

            this.label1.Font = new Font(this.pfc.Families[0],12,FontStyle.Bold);
                     
        }


Ohne den externen Call geht es nicht, frag mich nicht warum. Try catch vielleicht noch und Resourcen Name anpassen - steht noch auf meinem, hier klappt es so...

Jawoll... Sowas hab ich gesucht. Vielen Dank! 😃 Dann kann ich ja weitermachen... 😃

...

Hier ist noch eine Alternative (bzw. zwei, eine mit und eine ohne unsafe, letztendlich läuft es auch auf eine PrivateFontCollection hinaus, aber der Vollständigkeit halber sei es hier auch erwähnt, weil die Herangehensweise anders ist (kein DllImport)):

http://blog.andreloker.de/post/2008/07/03/Load-a-font-from-disk-stream-or-byte-array.aspx

gruß
sth_Weird

++++++++++++++++++++~+
Fluchen ist die einzige Sprache, die jeder Programmierer perfekt beherrscht


Linux is for free...if your time is worth nothing
++++++++++++++++++++~+

Offenbar soll es einen Bug in
PrivateFontCollection.AddMemoryFont geben, ob das stimmt, weiß ich nicht. Scheint so, weil der explizite externe Call irgendwie überflüßig erscheint.

Don't use AddMemoryFont, but extract the font to the temp directory and load it from there using AddFontFile

Scheint ein akzeptabler Work-around zu sein, wenn man die Ressource entpacken möchte statt sie im Speicher zu haben.