gif Image
-
Hi, I embedded some files in the assembly, html(text) images(gif, jpg, png). What i want to do is extract all the files to a tmp directory. I had SUCCESS in getting all the files to tmp directory. I open the text files..PERFECT. but images are not right, it says corrupt. How do I get the images extracted correctly from embedded resource and save it to disk. Following is my piece of code.
private void button1_Click(object sender, EventArgs e)
{
Assembly _assembly;
Stream _imageStream;
StreamReader _StreamReader;StreamWriter \_StreamWriter; string dirpath = Application.StartupPath + "\\\\help"; string\[\] resourceNames = this.GetType().Assembly.GetManifestResourceNames(); foreach (string resourceName in resourceNames) { //get resource if it is help if (resourceName.Contains(".help")) { MessageBox.Show(resourceName); try { if (!Directory.Exists(dirpath)) { DirectoryInfo di = Directory.CreateDirectory(dirpath); di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; di.CreateSubdirectory("images"); } string filename = Application.StartupPath + "\\\\help\\\\"; //if image, it should save in image folder if (resourceName.Contains(".images")) filename = filename + "images\\\\"; //get the filename and append it to the filename string\[\] filenamearray = resourceName.Split('.'); int length = filenamearray.Length; filename = filename + filenamearray\[length - 2\] + "." + filenamearray\[length-1\]; string read = null; \_assembly = Assembly.GetExecutingAssembly(); \_StreamReader = new StreamReader(\_assembly.GetManifestResourceStream(resourceName)); \_StreamWriter = new StreamWriter(filename); while ((read = \_StreamReader.ReadLine()) != null) { \_StreamWriter.WriteLine(read); } \_StreamWriter.Close(); \_Strea
-
Hi, I embedded some files in the assembly, html(text) images(gif, jpg, png). What i want to do is extract all the files to a tmp directory. I had SUCCESS in getting all the files to tmp directory. I open the text files..PERFECT. but images are not right, it says corrupt. How do I get the images extracted correctly from embedded resource and save it to disk. Following is my piece of code.
private void button1_Click(object sender, EventArgs e)
{
Assembly _assembly;
Stream _imageStream;
StreamReader _StreamReader;StreamWriter \_StreamWriter; string dirpath = Application.StartupPath + "\\\\help"; string\[\] resourceNames = this.GetType().Assembly.GetManifestResourceNames(); foreach (string resourceName in resourceNames) { //get resource if it is help if (resourceName.Contains(".help")) { MessageBox.Show(resourceName); try { if (!Directory.Exists(dirpath)) { DirectoryInfo di = Directory.CreateDirectory(dirpath); di.Attributes = FileAttributes.Directory | FileAttributes.Hidden; di.CreateSubdirectory("images"); } string filename = Application.StartupPath + "\\\\help\\\\"; //if image, it should save in image folder if (resourceName.Contains(".images")) filename = filename + "images\\\\"; //get the filename and append it to the filename string\[\] filenamearray = resourceName.Split('.'); int length = filenamearray.Length; filename = filename + filenamearray\[length - 2\] + "." + filenamearray\[length-1\]; string read = null; \_assembly = Assembly.GetExecutingAssembly(); \_StreamReader = new StreamReader(\_assembly.GetManifestResourceStream(resourceName)); \_StreamWriter = new StreamWriter(filename); while ((read = \_StreamReader.ReadLine()) != null) { \_StreamWriter.WriteLine(read); } \_StreamWriter.Close(); \_Strea
images contain binary data, not text, so StreamReader/Writer aren't the right classes, and ReadLine()/WriteLine() aren't the right methods to use here. Use BinaryReader/Writer or alternatively recreate then save the image using Image.FromStream() and Image.Save(). :)
-
images contain binary data, not text, so StreamReader/Writer aren't the right classes, and ReadLine()/WriteLine() aren't the right methods to use here. Use BinaryReader/Writer or alternatively recreate then save the image using Image.FromStream() and Image.Save(). :)
That's so much right thanks so much Luc. Observations: You always reply my questions, thanks again for that. :-D