Get a file from my resource file and write it to disk

J

JT

Hi,

Here's my problem. I am using an obscure font in my Windows Form
application, so I want to ensure that people using the application
will have the text displayed as intended. I've included the ttf font
file in my resources file and I want to write it to disk so that I can
then use PrivateFontCollection.AddFontFile(string filename) to make it
available. This seems like a horrible way to do it but I haven't
figured out another way. I would love to use the .AddMemoryFont()
method, but I'm not sure how to do that either.

So here are my questions,

1) How can I read the font file from the resources file and write it
to disk? I've tried reading it a byte at a time and writing it with a
stream writer, but I can't figure out the encoding and so the file
winds up being twice or more times the size that it should be and is
not considered a valid font file format.

2) How can I read the font file from the resources file and then load
it from memory with the AddMemoryFont(IntPtr memory, int Length).
When I try to obtain the address of either the resource or the byte
array it returns, I'm told that not only is it only possible using the
unsafe keyword, but it can't be done anyway (Compiler Error CS0208).

I don't want to have to use a setup application to place the font file
on the disk because a setup app is otherwise not necessary for the
application and I'd still like to avoid needing to write the file to
the disk.

Thanks,

JT
 
P

Peter Duniho

[...]
So here are my questions,

1) How can I read the font file from the resources file and write it
to disk? I've tried reading it a byte at a time and writing it with a
stream writer, but I can't figure out the encoding and so the file
winds up being twice or more times the size that it should be and is
not considered a valid font file format.

You shouldn't be using an Encoder at all. Just store the font as a
binary resource, read it from the resource as bytes, and write the
bytes directly to a file. Use a FileStream directly instead of a
StreamWriter.
2) How can I read the font file from the resources file and then load
it from memory with the AddMemoryFont(IntPtr memory, int Length).
When I try to obtain the address of either the resource or the byte
array it returns, I'm told that not only is it only possible using the
unsafe keyword, but it can't be done anyway (Compiler Error CS0208).

AFAIK, you have to use unsafe code, as you've been told. Why you can't
get it to work I don't know, since you didn't post the code you tried.
I don't want to have to use a setup application to place the font file
on the disk because a setup app is otherwise not necessary for the
application and I'd still like to avoid needing to write the file to
the disk.

Unless the license for the font restricts you from installing the font,
IMHO it would be better to just install the font in a setup
application. I appreciate the temptation to keep the font isolated to
your own application, and if the font truly is unusable outside of your
application maybe that makes sense. But otherwise, why not allow the
font to be installed generally? Especially since doing so avoids all
of the hoops you have to jump through to use it otherwise.

Pete
 
J

JT

This is the really link to the article:http://blogs.msdn.com/michkap/archive/2007/04/14/2137215.aspx
Just checked it in IE and FireFox, it is ok.

Option two: Google "Rhymes with Amharic"
The article is the first hit.

Okay. I guess the point is that he's using a font that can't be
displayed on my PC because I get a page with a title area containing
Sorting It All Out, Michael Kaplan's random stuff of dubious value, a
content area below that taking up about 90% of the width of my screen
containing literally nothing, and the sidebar information (archives,
etc.). Do I need to Sign In or Join in order to see this content?...

.... That link may work for you but it doesn't work for me. I had to
go to the archives section, click on April 2007, and then look for
something that looked right. I found 5 posts on the subject. The one
that matches the URL you gave me comes up blank (post # 4). Here are
the 4 that actually contain some content for anyone else who is
interested in this topic. I don't know yet if there is another way to
see what is in post # 4 or if it is even relevant.

http://blogs.msdn.com/michkap/archive/2007/04/14/2128198.aspx
http://blogs.msdn.com/michkap/archive/2007/04/14/2133650.aspx
http://blogs.msdn.com/michkap/archive/2007/04/14/2134278.aspx
http://blogs.msdn.com/michkap/archive/2007/04/15/2145619.aspx

http://blogs.msdn.com/michkap/archive/2007/04/14/2137215.aspx contains
nothing readable.

I will go look at these other four. Thank you for responding and at
least getting me close to something to look at.

JT
 
M

Mihai N.

I guess the point is that he's using a font that can't be
displayed on my PC
At least the regular paragraphs use "Verdana,Arial,Helvetica,sans-serif"
(inherited from body)
There is no need to sign-in or anything.

All I can say is that the page looks ok to me in IE 6, IE 7 and FireFox,
on 4 different machines (2 XP, 1 Vista, 1 Mac OS X)
I don't know what tha problem is, why can't you see it.
I found 5 posts on the subject. The one
that matches the URL you gave me comes up blank (post # 4).
Right, those are the posts.
I will go look at these other four. Thank you for responding and at
least getting me close to something to look at.
That article does not seem to contain any crytical info.
But I have tried to create a PDF and attach it to this post.
 
M

Mihai N.

I guess the point is that he's using a font that can't be
displayed on my PC
At least the regular paragraphs use "Verdana,Arial,Helvetica,sans-serif"
(inherited from body)
There is no need to sign-in or anything.

All I can say is that the page looks ok to me in IE 6, IE 7 and FireFox,
on 4 different machines (2 XP, 1 Vista, 1 Mac OS X)
I don't know what tha problem is, why can't you see it.
I found 5 posts on the subject. The one
that matches the URL you gave me comes up blank (post # 4).
Right, those are the posts.
I will go look at these other four. Thank you for responding and at
least getting me close to something to look at.
That article does not seem to contain any crytical info.
But I have tried to create a PDF, you can find it here:
http://www.mihai-nita.net/Article4.rar
(I intend to delete it from there in about a week)
 
J

JT

At least the regular paragraphs use "Verdana,Arial,Helvetica,sans-serif"
(inherited from body)
There is no need to sign-in or anything.

All I can say is that the page looks ok to me in IE 6, IE 7 and FireFox,
on 4 different machines (2 XP, 1 Vista, 1 Mac OS X)
I don't know what tha problem is, why can't you see it.


Right, those are the posts.


That article does not seem to contain any crytical info.
But I have tried to create a PDF, you can find it here:
http://www.mihai-nita.net/Article4.rar
(I intend to delete it from there in about a week)

Hey Mihai,

I don't understand it either. Posts 1, 2, 3, and 5 all displayed
information immediately. After about 30 minutes of bouncing around
back and forth between the pages, post 4 finally displayed
information. Unfortunately, I don't think this example is going to do
what I would like it to do. I think I'll have to look at some of the
other articles mentioned in these posts in order to use a memory
stream. I don't want to install the font and I don't want to have to
spend time modifying this example if it's not going to handle things
the way I want them handled. I'll bookmark this, but I only skimmed
through it once. If this was a high priority item for me, I would
spend more time on it. I have other higher priority things to do. I
thought there would be a cleaner way of handling this. Someone
(besides me) really should create a DLL to do this if Michael can't
convince MS to include this in a future release. I hope you don't
feel like I wasted your time. Your efforts and awareness of these
posts was very much appreciated. I may get back to it in the future.

JT
 
M

Mihai N.

in order to use a memory stream.
I don't want to install the font
Just remember that the title of your request is
"Get a file from my resource file and write it to disk"

Someone
(besides me) really should create a DLL to do this if Michael can't
convince MS to include this in a future release.
I kind of doubt someone will put together a dll and then share it.
Especially that the technique is not that complicated. But well...

I hope you don't feel like I wasted your time.
No problem. The newsgroups have the advantage that many people get to
see the posts. So if the link was not useful for you, it might be
for somebody else.
And it might also be useful for you at some point.
So no, I don't think it was a waste of time.
 

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

Top