views:

124

answers:

2

I've just added a ttf file to the project (c# 2008 express) as "file" and build option to embedded resource.

I'm having problems when trying to set this font like this: (I know the next line is wrong...)

this.label1.Font = AlarmWatch.Properties.Resources.Baby_Universe;

Error 1 Cannot implicitly convert type 'byte[]' to 'System.Drawing.Font' C:\Users\hongo\Documents\Visual Studio 2008\Projects\AlarmWatch\AlarmWatch\Form1.Designer.cs 57 32 AlarmWatch

I know it is byte[] cause I've set the option build as embedded resource, but comparing with this line that is correct:

this.label1.Font = new System.Drawing.Font("OCR A Extended",
                           24F, System.Drawing.FontStyle.Regular,
                           System.Drawing.GraphicsUnit.Point, ((byte)(0)));

How can I set this.label1 to use the new font?

+1  A: 

There is a AddMemoryFont method in the System.Drawing.Text namespace, which loads a font from a memory (it takes a pointer to the memory block, so you'll need to do some unsafe operation to get pointer to your byte array - I found an example here). More about the method on MSDN.

There is also a related StackOverflow question showing how to import Win API function to load the font directly (in case the above .NET method doesn't work).

EDIT A translation of the key part from Visual Basic might look like this (haven't checked it though):

// This should be probably a field of some class
PrivateFontCollection pfc = new PrivateFontCollection();

// allocate memory and copy byte[] to the location
IntPtr data = Marshal.AllocCoTaskMem(yourByteArray.Length);
Marshal.Copy(yourFontArray, 0, data, yourFontArray.Length);

// pass the font to the font collection
pfc.AddMemoryFont(data, fontStream.Length)

// Free the unsafe memory
Marshal.FreeCoTaskMem(data)

Once you have this, you should be able to refer to the font using its usual name.

Tomas Petricek
hi, thanks for your answer. I went to that page but, I don't know how to put that code in my application because it is in visual basic and cannot translate it well...
HoNgOuRu
Added example. BTW: It would be nice if you marked one of the answers to your questions as 'Accepted' after someone provides an answer that helps you. It is a good SO manner and if you have 'acceptance rate: 0%', people may not bother answering you.
Tomas Petricek
How do I set a post as answered or accepted or whatever ?
HoNgOuRu
There is a tick-like symbol below the voting buttons (on the left-side of the answer). By clicking on the symbol, you say that this was the right answer for your question. (I'm not asking you to mark my answer here - I think somebody can still give you a better answer - but you could do that for your earlier questions).
Tomas Petricek
thanks, I'll do that right now!
HoNgOuRu
I've marked as answered all my previous posts, correct me if I'm wrong...
HoNgOuRu
I get the correct name, size and style...but if I try to set the font to a label it won't do it, it goes back to the default font instead.the line i like thislabel1.Font = new Font(pfc.Families[0].Name,15f);I placed the lines you gave me in the form_load method, and try to set the label in that block of code right after those lines....
HoNgOuRu
@HoNgOuRu: Glad you find a solution! and thanks for marking the answers!
Tomas Petricek
A: 

I solved the problem by reading this previous answer at http://stackoverflow.com/questions/1955629/c-using-an-embedded-font-on-a-textbox

thanks Tomas for the link.

HoNgOuRu