For a side project I am trying to make a simple executable that takes 2 arguments:
The way I am thinking it should all go is this:
Now of course there are some intermediate steps that need to happen but I don't think I need to cover that at the moment.
So in the end I am left with the following C# file which should do what I want.
using System;
using System.Drawing;
using System.Text;
using System.IO;
namespace Base642Img
{
class MainClass
{
public static void Main (string[] args)
{
if (args.Length == 2) {
byte[] buffer;
String imageData;
FileStream fileStream = new FileStream (args [0], FileMode.Open, FileAccess.Read);
try {
int length = (int)fileStream.Length;
buffer = new byte[length];
int count;
int sum = 0;
while ((count = fileStream.Read (buffer, sum, length - sum)) > 0) {
sum += count;
}
} finally {
fileStream.Close();
}
try{
imageData = Encoding.UTF8.GetString (buffer, 0, buffer.Length);
imageData = imageData.Replace (System.Environment.NewLine, "");
byte[] imageBytes = System.Convert.FromBase64String (imageData);
MemoryStream ms = new MemoryStream (imageBytes, 0, imageBytes.Length);
ms.Write (imageBytes, 0, imageBytes.Length);
Image image = Image.FromStream (ms, true);
image.Save (args [1], System.Drawing.Imaging.ImageFormat.Gif);
} catch (Exception e) {
Console.WriteLine (e.ToString ());
}
} else {
Console.WriteLine ("Incorrect number of arguments");
}
}
}
}
This builds perfectly fine in MonoDevelop. But when I go to run the file (given the two arguments) it spits out the following exception:
System.FormatException: Invalid length.
at (wrapper managed-to-native) System.Convert:InternalFromBase64String (string,bool)
at System.Convert.FromBase64String (System.String s) [0x00000] in <filename unknown>:0
at Base642Img.MainClass.Main (System.String[] args) [0x00000] in <filename unknown>:0
I have absolutely no idea what this is saying and all of my searches on Google have turned up blank so I now turn to Stack Overflow hoping that someone can help me with this.
Convert.FromBase64String
requires that the input string be padded at the end with up to two =
characters so that its total length (ignoring white-space characters) is a multiple of four. Apparently, the program that created your base 64-encoded file omitted the trailing padding.
You need to append the missing padding characters yourself as necessary:
// using System.Collections.Generic;
// using System.Linq;
string imageData = File.ReadAllText(args[0]);
HashSet<char> whiteSpace = new HashSet<char> { '\t', '\n', '\r', ' ' };
int length = imageData.Count(c => !whiteSpace.Contains(c));
if (length % 4 != 0)
imageData += new string('=', 4 - length % 4); // Pad length to multiple of 4.
byte[] imageBytes = Convert.FromBase64String(imageData);
MemoryStream ms = new MemoryStream(imageBytes);
Image image = Image.FromStream(ms, true);
image.Save(args[1], System.Drawing.Imaging.ImageFormat.Gif);
Note: Since Convert.FromBase64String
ignores white-space characters, it's unnecessary to remove new-line characters. (My thanks to EZI for pointing this out.)