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. Convert.ToBase64 question

Convert.ToBase64 question

Scheduled Pinned Locked Moved C#
csharpasp-netgraphicsxmlperformance
7 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.
  • M Offline
    M Offline
    matt cole
    wrote on last edited by
    #1

    Hi, I'm trying to get a base64 string from an image to place inside a WordProcessingML xml document. I need to save the image to a stream after it's generated through asp.net, that is, I'm not able to use the file system. For some reason, I seem to get a different string loading something from the file system and using file streams and binary readers, as opposed to when I use Image.Save and a memory stream. I've pasted some sample code below, it's not my actual code that I'm working with but simulates the situation. The two methods generate different strings from the same image file. If I reverse the process, and take the two generated strings and load them into an image, both work fine. No doubt there is some obvious encoding issue or something that I'm sadly unaware of so if anyone had any ideas that'd be great. FileStream fs = File.OpenRead(@"C:\temp\chart.png"); BinaryReader br = new BinaryReader(fs); string pngString = Convert.ToBase64String(br.ReadBytes((int)fs.Length)); MemoryStream memStream = new MemoryStream(); Image image = Image.FromFile(@"C:\temp\chart.png"); image.Save(memStream,System.Drawing.Imaging.ImageFormat.Png); string memString = Convert.ToBase64String(memStream.GetBuffer()); Thanks, Matt

    A 1 Reply Last reply
    0
    • M matt cole

      Hi, I'm trying to get a base64 string from an image to place inside a WordProcessingML xml document. I need to save the image to a stream after it's generated through asp.net, that is, I'm not able to use the file system. For some reason, I seem to get a different string loading something from the file system and using file streams and binary readers, as opposed to when I use Image.Save and a memory stream. I've pasted some sample code below, it's not my actual code that I'm working with but simulates the situation. The two methods generate different strings from the same image file. If I reverse the process, and take the two generated strings and load them into an image, both work fine. No doubt there is some obvious encoding issue or something that I'm sadly unaware of so if anyone had any ideas that'd be great. FileStream fs = File.OpenRead(@"C:\temp\chart.png"); BinaryReader br = new BinaryReader(fs); string pngString = Convert.ToBase64String(br.ReadBytes((int)fs.Length)); MemoryStream memStream = new MemoryStream(); Image image = Image.FromFile(@"C:\temp\chart.png"); image.Save(memStream,System.Drawing.Imaging.ImageFormat.Png); string memString = Convert.ToBase64String(memStream.GetBuffer()); Thanks, Matt

      A Offline
      A Offline
      Andy Brummer
      wrote on last edited by
      #2

      if you do an image.Save to a binaryWriter do you get the same files? There is a form of image.Save which takes encoding parameters. You probably have to set these to match the original file.

      M 2 Replies Last reply
      0
      • A Andy Brummer

        if you do an image.Save to a binaryWriter do you get the same files? There is a form of image.Save which takes encoding parameters. You probably have to set these to match the original file.

        M Offline
        M Offline
        matt cole
        wrote on last edited by
        #3

        [Message Deleted]

        1 Reply Last reply
        0
        • A Andy Brummer

          if you do an image.Save to a binaryWriter do you get the same files? There is a form of image.Save which takes encoding parameters. You probably have to set these to match the original file.

          M Offline
          M Offline
          matt cole
          wrote on last edited by
          #4

          Hi Andy, I didn't actually even save the image programatically, as I mentioned the snippet I've pasted in isn't the actual code I'm working with it's just a test that I wrote up to see what the problem was in my actual code. The image on disk has just been saved straight from a browser so I could use it in the test. Don't know if this makes any difference to you? Thanks, Matt

          A 1 Reply Last reply
          0
          • M matt cole

            Hi Andy, I didn't actually even save the image programatically, as I mentioned the snippet I've pasted in isn't the actual code I'm working with it's just a test that I wrote up to see what the problem was in my actual code. The image on disk has just been saved straight from a browser so I could use it in the test. Don't know if this makes any difference to you? Thanks, Matt

            A Offline
            A Offline
            Andy Brummer
            wrote on last edited by
            #5

            I don't think the image object makes any promises about Load() and Save() generating the exact same bits. I just suggested a test to verify that. Unless you have to use the image object to manipulate the image in some way I would just deal with it as a stream. That's how I've always dealt with them in WordprocessingML documents.


            I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

            M 2 Replies Last reply
            0
            • A Andy Brummer

              I don't think the image object makes any promises about Load() and Save() generating the exact same bits. I just suggested a test to verify that. Unless you have to use the image object to manipulate the image in some way I would just deal with it as a stream. That's how I've always dealt with them in WordprocessingML documents.


              I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

              M Offline
              M Offline
              matt cole
              wrote on last edited by
              #6

              [Message Deleted]

              1 Reply Last reply
              0
              • A Andy Brummer

                I don't think the image object makes any promises about Load() and Save() generating the exact same bits. I just suggested a test to verify that. Unless you have to use the image object to manipulate the image in some way I would just deal with it as a stream. That's how I've always dealt with them in WordprocessingML documents.


                I can imagine the sinking feeling one would have after ordering my book, only to find a laughably ridiculous theory with demented logic once the book arrives - Mark McCutcheon

                M Offline
                M Offline
                matt cole
                wrote on last edited by
                #7

                Yeah my actual code does deal with it as a stream but it generates the same string as the Image.Save(memoryStream) method. Basically I'm using a third party charting component which has a save method which is able to save to a stream, I assume it uses the same method as the Image.Save does as it generates the same base64 string. I was hoping if i could get to the bottom of the reason I am getting two different strings I could take that solution to my actual code. I can see now that the string is exactly the same, except the file IO method has more bytes on the end that are missing from the other method.

                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