[02/03] String to Image Help
I have a base-64 image string:
Quote:
R0lGODlhawAsALMAALu3ns/Kuv//9+zexPv33eovKfF8bfWzngAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAawAsAAAE/hCESau9OOvNu/+AJIxkaZ5oqq5s67ZBGAxvbd94Pg6xNBC6oHDoIvBkP4JyyWw6n9CodEqtRo8+q3bL7XqV2Nl3LC0wC0myN5xWuw0Fg9Ls/rLr+IJZj1/3xH1qfHyBW3eFW3p6B2YDhIhVh5BUfIx8jnSTU5KaUYRwBQcEj51QnKVnomaMo3uZqE6nsEZyBKy2rrNPsrAHBr+NP3ujbbq8qL6 KyqOjogRwzrDHpb6tiratRoq10n/FunFNSYOhocbeuku/lsrtrtHdSOlLq3qgoHxy36XTnXSizmoNgjernyZXoJplCzfP4KR35LIxa9gDwD5UdPQ5yzUxHRaL8fPmmGuy5weckB8vYnS3ByBD jxVVrlxmwKQemZNShjTljhvMECD7DBg61MoveopwatLZpyelV1J8Lo0ZaBG9os/UgUly4EcSrHNqKqk2VioZpnlIsbOXdKEydgbWtoIzAA60OmjrtPNaKV/EhEezMRJ1shkhgnaoNj2sjQ6hvyMbm7Hr1prZxECVJiKFqbFnvwLRgLaGRuTLs4rTvurMGrKcu6PtNbGEN7XeR3Ev6c51t7Bfwgx vkRqT9427VsIakcvtWNkA2qTl1s68s7oVnUSyaydixNv27+BvhAlPvvyKikDTq1/Pvr379/Djy58PNAIAOw==
This can be converted to an image by using Convert.FromBase64String() to get a byte array, putting the byte array into a MemoryStream, and creating a new Bitmap() by passing the MemoryStream object into its constructor.
What if, for whatever paranormal reasons, the last 4 characters are chopped off the string and I end up with this:
Quote:
R0lGODlhawAsALMAALu3ns/Kuv//9+zexPv33eovKfF8bfWzngAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAawAsAAAE/hCESau9OOvNu/+AJIxkaZ5oqq5s67ZBGAxvbd94Pg6xNBC6oHDoIvBkP4JyyWw6n9CodEqtRo8+q3bL7XqV2Nl3LC0wC0myN5xWuw0Fg9Ls/rLr+IJZj1/3xH1qfHyBW3eFW3p6B2YDhIhVh5BUfIx8jnSTU5KaUYRwBQcEj51QnKVnomaMo3uZqE6nsEZyBKy2rrNPsrAHBr+NP3ujbbq8qL6 KyqOjogRwzrDHpb6tiratRoq10n/FunFNSYOhocbeuku/lsrtrtHdSOlLq3qgoHxy36XTnXSizmoNgjernyZXoJplCzfP4KR35LIxa9gDwD5UdPQ5yzUxHRaL8fPmmGuy5weckB8vYnS3ByBD jxVVrlxmwKQemZNShjTljhvMECD7DBg61MoveopwatLZpyelV1J8Lo0ZaBG9os/UgUly4EcSrHNqKqk2VioZpnlIsbOXdKEydgbWtoIzAA60OmjrtPNaKV/EhEezMRJ1shkhgnaoNj2sjQ6hvyMbm7Hr1prZxECVJiKFqbFnvwLRgLaGRuTLs4rTvurMGrKcu6PtNbGEN7XeR3Ev6c51t7Bfwgx vkRqT9427VsIakcvtWNkA2qTl1s68s7oVnUSyaydixNv27+BvhAlPvvyKikDTq1/Pvr379/Djy58PNAIA
When I try to use this, I cannot create a bitmap object. Instead, I keep getting "Invalid parameter used" when creating the bitmap object.
I can show you code if you'd like, but I have a feeling it isn't really a coding issue.
Re: [02/03] String to Image Help
Sorry, the question is, how can I write the image despite the missing end-characters? Is it even possible? Does anyone know why it's erroring out?
Re: [02/03] String to Image Help
Code:
byte[] TheBlob;
TheBlob = Convert.FromBase64String("R0lGODlhawAsALMAALu3ns/Kuv//9+zexPv33eovKfF8bfWzngAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACwAAAAAawAsAAAE/hCESau9OOvNu/+AJIxkaZ5oqq5s67ZBGAxvbd94Pg6xNBC6oHDoIvBkP4JyyWw6n9CodEqtRo8+q3bL7XqV2Nl3LC0wC0myN5xWuw0Fg9Ls/rLr+IJZj1/3xH1qfHyBW3eFW3p6B2YDhIhVh5BUfIx8jnSTU5KaUYRwBQcEj51QnKVnomaMo3uZqE6nsEZyBKy2rrNPsrAHBr+NP3ujbbq8qL6KyqOjogRwzrDHpb6tiratRoq10n/FunFNSYOhocbeuku/lsrtrtHdSOlLq3qgoHxy36XTnXSizmoNgjernyZXoJplCzfP4KR35LIxa9gDwD5UdPQ5yzUxHRaL8fPmmGuy5weckB8vYnS3ByBDjxVVrlxmwKQemZNShjTljhvMECD7DBg61MoveopwatLZpyelV1J8Lo0ZaBG9os/UgUly4EcSrHNqKqk2VioZpnlIsbOXdKEydgbWtoIzAA60OmjrtPNaKV/EhEezMRJ1shkhgnaoNj2sjQ6hvyMbm7Hr1prZxECVJiKFqbFnvwLRgLaGRuTLs4rTvurMGrKcu6PtNbGEN7XeR3Ev6c51t7BfwgxvkRqT9427VsIakcvtWNkA2qTl1s68s7oVnUSyaydixNv27+BvhAlPvvyKikDTq1/Pvr379/Djy58PNAIA")
System.Drawing.Image myImage = null;
System.IO.MemoryStream myStream = new System.IO.MemoryStream();
System.IO.MemoryStream processedMemStream = new System.IO.MemoryStream();
myStream.Write(TheBlob, 0, TheBlob.Length);
myImage = System.Drawing.Image.FromStream(myStream);
myStream.Close();
Yeah, it's in C#, but you have Visual studio.
Go on, try this!
Re: [02/03] String to Image Help
One more thing to add, if I were to do a Response.Binarywrite (in asp.net) of the byte array, IE would handle this image perfectly. It's as if IE doesn't notice the missing bits of the image. What's that all about?
Re: [02/03] String to Image Help
Re: [02/03] String to Image Help
I agree with your observations.
I hypothesize the following:
1. I think the length of the string is being automatically padded by IE (it must be used to receiving broken images). The strings are 764 and 760 bytes long which are converted to 571 and 570 bytes once converted. I can only surmise that the image header encoded in there is reporting a known size (width x height (in pixels)) that is different to the number of actual bytes of data it finds in the byte array. Have you tried saving this byte array to a file and loading it into MSpaint?
2. Your data source bites my ass.
Re: [02/03] String to Image Help
Actually I just tried saving it to a file and GIMP complains that therrrr was an IO error (with the shorter string) but shows it anyway. I presume its just a matter of working out why your data is being corrupted.
Come onto MSN and we can discuss it.
Additional info: the Width and Heights can be found at offsets 0x6 and 0x8 respecively (big endian 16bit unsigned integers by the look of it). Its been a while since I chopped up a GIF.
Anyway, take the total length of th byte array and subtract the header from that and then work out how many pixel bytes there shoul be, then you can pad it out to the desired length, althrough I'm not too familiar with base64 images so I'm guessing its not as easy as just appending "0000" to the end of the string.
VisualAd has some experience with Base 64 i believe.