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