aewarnick Posted March 12, 2003 Posted March 12, 2003 (edited) 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 it's attributes or access it. Here is what I have so far: 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(); Edited March 12, 2003 by divil Quote C#
bpayne111 Posted March 12, 2003 Posted March 12, 2003 i'm 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 Quote i'm not lazy i'm just resting before i get tired.
bpayne111 Posted March 12, 2003 Posted March 12, 2003 if you are trying to save a custom object to disk you should read the help files on "Serialization" Quote i'm not lazy i'm just resting before i get tired.
aewarnick Posted March 12, 2003 Author Posted March 12, 2003 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? Quote C#
*Gurus* divil Posted March 12, 2003 *Gurus* Posted March 12, 2003 In C#, the string used to access embedded resources (I think) is DefaultNamespace.FileName, where DefaultNamespace is specified in your project properties. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted March 12, 2003 Author Posted March 12, 2003 I cannot get anything to work. Do I need to create a new resource file? I don't think so becuase the font is embedded in the program. I know by the size of it. Quote C#
*Gurus* divil Posted March 12, 2003 *Gurus* Posted March 12, 2003 I was able to accomplish this (with verdana.ttf embedded) like this: 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); } Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted March 12, 2003 Author Posted March 12, 2003 You're 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!! Quote C#
aewarnick Posted March 12, 2003 Author Posted March 12, 2003 A question, that was quite slow. Is there any faster way to write it than WriteByte? Quote C#
*Gurus* divil Posted March 12, 2003 *Gurus* Posted March 12, 2003 Yes, you do it in chunks at a time instead of byte by byte: 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(); Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
aewarnick Posted March 12, 2003 Author Posted March 12, 2003 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? Quote C#
*Gurus* divil Posted March 12, 2003 *Gurus* Posted March 12, 2003 Yes. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.