How to write embedded objects to disk?

aewarnick

Well-known member
Joined
Jan 29, 2003
Messages
1,031
Very frustrated. . . this should not be hard.
I have a font as an embedded resource in my program. I think the problem is that I cannot access it to get the size in bytes (if that is possible). I think that I need to put the font into the byte array to write it to disk but I cannot get its attributes or access it.
Here is what I have so far:

C#:
FileStream FS=new FileStream("C:\\Windows\\verdana.ttf", FileMode.Create, FileAccess.Write);
			ResourceWriter RW=new ResourceWriter(FS);
			RW.AddResource("verdana.ttf", "1");
			RW.Close();
			byte[]b= new Byte[200];
			FS.Write(b, 0, 200);
			FS.Close();
 
Last edited by a moderator:
im not totally sure what you are trying to do but this may help

System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream("filename including namespace if it is different than default")
use GetNames method to find out the name of your file

this is probably not what you need but it might help
 
if you are trying to save a custom object to disk you should read the help files on "Serialization"
 
This is actually closer to what I am trying to do:

ResourceManager RM=new ResourceManager(typeof(Form1));
object font= RM.GetObject("verdana.ttf");
MessageBox.Show(""+font.ToString());
BinaryFormatter B=new BinaryFormatter();
B.Serialize(FS, font);
FS.Close();

My only trouble is referencing the embedded resource named, "verdana.ttf". I noticed that VS uses the Resource manager to load pictures into picture boxes. I cannot find where the resource is though. For example, my embedded resource is in the solution explorer with all the classes but theirs I cannot find anywhere. This is what is written in the code to load it:

this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image")));

How can I make a reference to my embedded resource that is shown in the Solution explorer?
 
In C#, the string used to access embedded resources (I think) is DefaultNamespace.FileName, where DefaultNamespace is specified in your project properties.
 
I cannot get anything to work. Do I need to create a new resource file? I dont think so becuase the font is embedded in the program. I know by the size of it.
 
I was able to accomplish this (with verdana.ttf embedded) like this:

C#:
FileStream f = new FileStream(@"c:\verdana.ttf", FileMode.Create);
Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication7.verdana.ttf");
byte b;
int i;

while (true)
{
	i = s.ReadByte();
    if (i == -1) break;
	b = (byte)i;

	f.WriteByte(b);
}
 
Youre right divil. That did work. Forget that recource manager. Thank you so much. Why does it have to be so complicated like that? I think MS could have made that alot easier. After all, the object is right there!!
 
Yes, you do it in chunks at a time instead of byte by byte:

C#:
FileStream f = new FileStream(@"c:\verdana.ttf", FileMode.Create);
Stream s = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsApplication7.verdana.ttf");
byte[] b = new byte[4096];
int i;

while (true)
{
	i = s.Read(b, 0, 4096);
	if (i == 0) break;

	f.Write(b, 0, i);
}

f.Close();
 
Actually I think I was wrong. . .there SEEMS to be no difference in speed. But, would you say that using a byte[] buffer is the best way to go, Rather than byte by byte?
 
Back
Top