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. Read / Write an Image file

Read / Write an Image file

Scheduled Pinned Locked Moved C#
graphicshelp
10 Posts 4 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.
  • R Offline
    R Offline
    Rob Tomson
    wrote on last edited by
    #1

    I'm having problems trying to accomplish a certain task. I want to save some custom information about an image and then save the image itself in the same file. I can write and read my infomation by itself using a binary writer/reader but as soon as I try to throw a System.Drawing.Image in the mix things get messed up. Am I going about using the image correctly like this: ... System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Image.Save(iStream, System.Drawing.Imaging.ImageFormat.Png); ... It seems to me that as soon as I do the above code the filestream gets all weird and I it's no longer in a readable format. Please help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

    Y 1 Reply Last reply
    0
    • R Rob Tomson

      I'm having problems trying to accomplish a certain task. I want to save some custom information about an image and then save the image itself in the same file. I can write and read my infomation by itself using a binary writer/reader but as soon as I try to throw a System.Drawing.Image in the mix things get messed up. Am I going about using the image correctly like this: ... System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Image.Save(iStream, System.Drawing.Imaging.ImageFormat.Png); ... It seems to me that as soon as I do the above code the filestream gets all weird and I it's no longer in a readable format. Please help, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

      Y Offline
      Y Offline
      Yulianto
      wrote on last edited by
      #2

      Rob Tomson wrote: System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Why do you Append? Try Create or Truncate.


      Work hard and a bit of luck is the key to success.

      You don`t need to be genius, to be rich.

      R 1 Reply Last reply
      0
      • Y Yulianto

        Rob Tomson wrote: System.IO.FileStream iStream = new System.IO.FileStream(FileName, System.IO.FileMode.Append, System.IO.FileAccess.Write); Why do you Append? Try Create or Truncate.


        Work hard and a bit of luck is the key to success.

        You don`t need to be genius, to be rich.

        R Offline
        R Offline
        Rob Tomson
        wrote on last edited by
        #3

        I was doing Append because the code before this writes my custom information that I want before the actual image. If I just do Create then it would overwrite all the info I just wrote. I've also tried just writing my stuff then writing the image through one FileStream but I get the same effect of not being able to read the file afterwards. Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

        L 1 Reply Last reply
        0
        • R Rob Tomson

          I was doing Append because the code before this writes my custom information that I want before the actual image. If I just do Create then it would overwrite all the info I just wrote. I've also tried just writing my stuff then writing the image through one FileStream but I get the same effect of not being able to read the file afterwards. Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

          L Offline
          L Offline
          leppie
          wrote on last edited by
          #4

          You cant do that, it break the file format. Alternatively, it you wanna encapsulate the image files, you can embed them in the Stream, but you will have to setup (preferably another) Stream to supply only the correct offsets for Image.FromStream() to work. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

          R 1 Reply Last reply
          0
          • L leppie

            You cant do that, it break the file format. Alternatively, it you wanna encapsulate the image files, you can embed them in the Stream, but you will have to setup (preferably another) Stream to supply only the correct offsets for Image.FromStream() to work. xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots

            R Offline
            R Offline
            Rob Tomson
            wrote on last edited by
            #5

            How would I go about doing that? Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

            S 1 Reply Last reply
            0
            • R Rob Tomson

              How would I go about doing that? Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

              S Offline
              S Offline
              Sebastian Schneider
              wrote on last edited by
              #6

              Well, if I wanted to save an Image along with additional info, I would (as I am using NTFS) use alternate data streams to store my info. This means, any Image-Processor can still load that file (if the image is in a common format) and my program could utilize the additional data streams attached to the image file. If you really need to save everything into one stream, try writing an offset first. This means: Save into the very first bytes of your file (lets say: the first 4 bytes) an integer which contains the exact location at which the image data begins. So: you format your information (be sure to have some kind of delimiter or such) and store it into a string. Get the length of that string, add 4 and save that integer into your file (BINARY format, not as a string). Then, append your info-string and the image-data. That way you will be able to jump to the image data directly, without having to parse the info first. Thats all. Sid

              R 1 Reply Last reply
              0
              • S Sebastian Schneider

                Well, if I wanted to save an Image along with additional info, I would (as I am using NTFS) use alternate data streams to store my info. This means, any Image-Processor can still load that file (if the image is in a common format) and my program could utilize the additional data streams attached to the image file. If you really need to save everything into one stream, try writing an offset first. This means: Save into the very first bytes of your file (lets say: the first 4 bytes) an integer which contains the exact location at which the image data begins. So: you format your information (be sure to have some kind of delimiter or such) and store it into a string. Get the length of that string, add 4 and save that integer into your file (BINARY format, not as a string). Then, append your info-string and the image-data. That way you will be able to jump to the image data directly, without having to parse the info first. Thats all. Sid

                R Offline
                R Offline
                Rob Tomson
                wrote on last edited by
                #7

                That sounds very interesting, thank you. What do you mean by 'use alternate data streams'? Does this mean I can embedd info in the file and still have other programs read the file? That would really helpful if I could do that instead. Any suggestions? Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

                S 1 Reply Last reply
                0
                • R Rob Tomson

                  That sounds very interesting, thank you. What do you mean by 'use alternate data streams'? Does this mean I can embedd info in the file and still have other programs read the file? That would really helpful if I could do that instead. Any suggestions? Thanks, Rob Tomson -- There are 10 kinds of people. Those who understand binary and those who don't.

                  S Offline
                  S Offline
                  Sebastian Schneider
                  wrote on last edited by
                  #8

                  Well, try googling for "alternate data stream NTFS" and you should find plenty of information. Cheers Sid

                  R 1 Reply Last reply
                  0
                  • S Sebastian Schneider

                    Well, try googling for "alternate data stream NTFS" and you should find plenty of information. Cheers Sid

                    R Offline
                    R Offline
                    Rob Tomson
                    wrote on last edited by
                    #9

                    wow, thank you very much. I didn't know something like this existed and it's very interesting. Looks like this is what I'm going to use. Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

                    S 1 Reply Last reply
                    0
                    • R Rob Tomson

                      wow, thank you very much. I didn't know something like this existed and it's very interesting. Looks like this is what I'm going to use. Thanks, Rob -- There are 10 kinds of people. Those who understand binary and those who don't.

                      S Offline
                      S Offline
                      Sebastian Schneider
                      wrote on last edited by
                      #10

                      Just do not forget that this only works on recent NTFS versions. And that it is quite easy to break the alternate streams. I suggest you do some "experiments" by hand first. Cheers Sid

                      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