Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. gif Image

gif Image

Scheduled Pinned Locked Moved C#
htmlhardwarehelpquestionlearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    Karmendra Suthar
    wrote on last edited by
    #1

    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
    
    L 1 Reply Last reply
    0
    • K Karmendra Suthar

      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
      
      L Offline
      L Offline
      Luc 648011
      wrote on last edited by
      #2

      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(). :)

      K 1 Reply Last reply
      0
      • L Luc 648011

        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(). :)

        K Offline
        K Offline
        Karmendra Suthar
        wrote on last edited by
        #3

        That's so much right thanks so much Luc. Observations: You always reply my questions, thanks again for that. :-D

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups