I want to use custom ttf Font in winform designer. I tried to search but, there is only add with programmatic way. But the programmatic way does not be reflected in designer. The wpf project can do this with blend for visual studio but winform can not set ttf font which i add the project with designer. How sould i gonna do?
Asked
Active
Viewed 729 times
-2
-
If it's not installed, how is the designer supposed to know it exists in order to be able to use it? Where does it come from for the designer to use it? Use some common sense. Your code can load it when you app starts, which makes it available to your app. If it's not installed, where does the designer load it from? How does it know it's there? – Ken White Dec 04 '17 at 02:50
-
i don't know if this help. you should search first before post it. https://stackoverflow.com/questions/1297264/using-custom-fonts-on-a-label-on-winforms – chopperfield Dec 04 '17 at 03:31
-
@Ken White : I had a font when i wrote this project . But i found the font problem when released the project. So i delete the ttf font wich i used this project. – heapoverflow Dec 04 '17 at 04:24
-
@chopperfield I already used PrivateFontCollection. But the ttf font did not show in designer. – heapoverflow Dec 04 '17 at 04:25
-
So your question is *How do I use a font in the designer that I deleted?* now? This is making less sense. You can't use a font that doesn't exist in the designer. I'm not sure why you're not understanding. If the designer does not know about the font, it can't use that font. If you deleted it, not only can the designer not use it, but your app can't use it. – Ken White Dec 04 '17 at 04:29
-
@Ken White Uhm.. Then how can i use(set) ttf font in designer which i dinamically loaded? – heapoverflow Dec 04 '17 at 04:35
-
@KenWhite It is not unusual to want to package a font with an application so that font installation and management is not required. If the font exists in the project's resource file then the designer SHOULD be aware of it. This is not a difficult concept. – Malcolm O Mar 27 '19 at 19:50
1 Answers
0
try this
// specify embedded resource name
string resource = "embedded_font.PAGAP___.TTF";
// receive resource stream
Stream fontStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resource);
// create an unsafe memory block for the font data
System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
// create a buffer to read in to
byte[] fontdata = new byte[fontStream.Length];
// read the font data from the resource
fontStream.Read(fontdata, 0, (int)fontStream.Length);
// copy the bytes to the unsafe memory block
Marshal.Copy(fontdata, 0, data, (int)fontStream.Length);
// pass the font to the font collection
private_fonts.AddMemoryFont(data, (int)fontStream.Length);
// close the resource stream
fontStream.Close();
// free up the unsafe memory
Marshal.FreeCoTaskMem(data);

Muhammad Usama Alam
- 209
- 4
- 13