How to return character of font family given index

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How to return character of font family given index



I have a font family name and an index of a specific character from that family.



Example: I have the font family "Wingdings 2" and index 33. If you go to http://www.alanwood.net/demos/wingdings-2.html and look at the first item, which is index 33, the character is a ball point pen.



My question is, how can I retrieve the character itself in C#? I need to draw this character in my application.



I've gone through all of the methods and properties of the Font and FontFamily class, but I don't see anything that could help.



Edit: I know how to draw the character using a graphics object, the issue is actually retrieving the character in the first place knowing only the font family and the index of the character in the given font family.





can't you just use a graphics object and a string representing only that character and draw it anywhere you want?
– Sebastian L
Aug 10 at 12:40





I know how to to draw stuff, the problem is getting the character to draw knowing only the font family and the index of that character in the font family.
– pfinferno
Aug 10 at 12:45




1 Answer
1



To draw a character you could use this snippet of code:


public static Image DawTextFromFontFamily(string text, FontFamily family, Color textColor, Color backColor)

return DrawText(text, new Font(family, 16), textColor, backColor);


public static Image DrawText(String text, Font font, Color textColor, Color backColor)

//first, create a dummy bitmap just to get a graphics object
Image img = new Bitmap(1, 1);
Graphics drawing = Graphics.FromImage(img);

//measure the string to see how big the image needs to be
SizeF textSize = drawing.MeasureString(text, font);

//free up the dummy image and old graphics object
img.Dispose();
drawing.Dispose();

//create a new image of the right size
img = new Bitmap((int)textSize.Width, (int)textSize.Height);

drawing = Graphics.FromImage(img);

//paint the background
drawing.Clear(backColor);

//create a brush for the text
Brush textBrush = new SolidBrush(textColor);

drawing.DrawString(text, font, textBrush, 0, 0);

drawing.Save();

textBrush.Dispose();
drawing.Dispose();

return img;



Now you have an image of whatever characters in whatever font you need





Hey thanks for the answer, but not quite what I need. I know how to to draw stuff, the problem is getting the character to draw knowing only the font family and the index of that character in the font family.
– pfinferno
Aug 10 at 12:45





well for the Fontfamily you can just create a font msdn.microsoft.com/de-de/library/6xaya1yk(v=vs.110).aspx
– Sebastian L
Aug 10 at 12:53






I think I was confused about how fonts work here. I used Convert.ToChar(CharacterIndex), then applied the font family and it seems to be working now :)
– pfinferno
Aug 10 at 12:59






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard