Special characters with DrawString?!

J

Jonah Olsson

Hi guys!

I'm trying to use Swedish characters with DrawString but no luck. Do I need
to set the Charset somewhere?? I've been searching Google Groups for hours
now, but I can't find any help or tutorial. Below is my code.

Thanks for any kind of help!

Regards,
Jonah Olsson


Dim b As Bitmap = New Bitmap(1, 1)
Dim g As Graphics = Graphics.FromImage(b)
Dim solidBrush As SolidBrush = New SolidBrush(TextColor)

Dim privateFontCollection As PrivateFontCollection = New
PrivateFontCollection


privateFontCollection.AddFontFile(Server.MapPath("Static/Fonts/TELEBC__.TTF"
))

Dim thisFont As FontFamily = privateFontCollection.Families(0)
Dim regFont As Font = New Font(thisFont, FontSize, FontStyle.Bold,
GraphicsUnit.Pixel)

Dim Width As Integer
Width = Convert.ToInt32(g.MeasureString(Text, regFont).Width)
Dim Height As Integer
Height = Convert.ToInt32(g.MeasureString(Text, regFont).Height)

b = New Bitmap(Width, Height, PixelFormat.Format24bppRgb)

g = Graphics.FromImage(b)

g.TextRenderingHint = TextRenderingHint.AntiAlias
g.SmoothingMode = SmoothingMode.AntiAlias
g.Clear(BackColor)
g.DrawString(Text, regFont, solidBrush, 0, 0,
StringFormat.GenericTypographic)

Response.ContentType = "image/gif"
b.Save(Response.OutputStream, ImageFormat.Gif)
b.Dispose()
 
D

Dmitriy Lapshin [C# / .NET MVP]

Jonah,

It probably depends on whether the font you use is a Unicode one. If it is
not unicode, character translation should be done to match Swedich
characters stored as Unicode ones in a String with the code page the Font
uses. To set up this code page, use the GdiCharSet property of the Font
class.
 
J

Jonah Olsson

Hi Dmitriy,

I solved the problem by using UrlEncode and UrlDecode, but that feels like a
bad work around. I have to call the web page (in <img src="...">) with
Web.HttpUtility.UrlEncode(...) and then decode the string in my code.

I'll try to set the GdiCharSet to see if there's a way I can skip the encode
and decode part.

Thanks.
Jonah
 
D

Dmitriy Lapshin [C# / .NET MVP]

Jonah,

Oops, I assumed you used Windows Forms. With the Web, you should set a
correct encoding for the web page in the appropriate meta tag (as well as in
the "Content-Type" header). I am not completely sure, but the HttpResponse
class should have the Encoding property. Set properly, it should
automatically take care over encoding the Unicode characters you send to the
response stream to the right code page.

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

Jonah Olsson said:
Hi Dmitriy,

I solved the problem by using UrlEncode and UrlDecode, but that feels like a
bad work around. I have to call the web page (in <img src="...">) with
Web.HttpUtility.UrlEncode(...) and then decode the string in my code.

I'll try to set the GdiCharSet to see if there's a way I can skip the encode
and decode part.

Thanks.
Jonah


"Dmitriy Lapshin [C# / .NET MVP]" <[email protected]> skrev i
meddelandet news:[email protected]...
Jonah,

It probably depends on whether the font you use is a Unicode one. If it is
not unicode, character translation should be done to match Swedich
characters stored as Unicode ones in a String with the code page the Font
uses. To set up this code page, use the GdiCharSet property of the Font
class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,016
Latest member
TatianaCha

Latest Threads

Top