C# - Base64 byte array to Image FAILS no matter what I try
===

I'm having trouble creating an Image/Bitmap object in C# from a base64 encoded byte array.

**Here's what I'm dealing with:**

---

I have a frontend where a user can crop an image. When the user selects an image via an `input[type=file]`, my javascript code uses HTML5's FileReader to save the `DataUrl` (base64 string) to a `hidden field`, that is posted along with the crop coordinates and dimensions, and everything else in that `form`.

**The essence:**

base64 data, if you want to test yourself:

http://kristianbak.com/test_image.txt

1. base64 string is posted to the action, and received as the parameter `imageData`
2. the action converts the string to a base64 byte array, like this:
3. `byte[] imageBytes = Convert.FromBase64String(imageData.EncodeTo64());`

**EncodeTo64 extension method:**

    public static string EncodeTo64(this String toEncode)
    {
        var toEncodeAsBytes = Encoding.ASCII.GetBytes(toEncode);
        var returnValue = Convert.ToBase64String(toEncodeAsBytes);
        return returnValue;
    }

After the base64 string is converted to a byte array, I read the bytes into the memory using a `MemoryStream`:

    using (var imageStream = new MemoryStream(imageBytes, false))
    {
        Image image = Image.FromStream(imageStream); //ArgumentException: Parameter is not valid.
    }

---

**I have also tried the following variations:**

**a)**

    using (var imageStream = new MemoryStream(imageBytes))
    {
        Bitmap image = new Bitmap(imageStream); //ArgumentException: Parameter is not valid.
    }

**b)**

    using (var imageStream = new MemoryStream(imageBytes))
    {
        imageStream.Position = 0;
        Image image = Image.FromStream(imageStream); //ArgumentException: Parameter is not valid.
    }

**c)**

    TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(Bitmap));
    Bitmap image = (Bitmap)typeConverter.ConvertFrom(imageBytes);

**d)**

Using this method:

    private Bitmap GetBitmap(byte[] buf)
    {
        Int16 width = BitConverter.ToInt16(buf, 18);
        Int16 height = BitConverter.ToInt16(buf, 22);
    
        Bitmap bitmap = new Bitmap(width, height); //ArgumentException: Parameter is not valid.
    
        int imageSize = width * height * 4;
        int headerSize = BitConverter.ToInt16(buf, 10);
    
        System.Diagnostics.Debug.Assert(imageSize == buf.Length - headerSize);
    
        int offset = headerSize;
        for (int y = 0; y < height; y++)
        {
            for (int x = 0; x < width; x++)
            {
                bitmap.SetPixel(x, height - y - 1, Color.FromArgb(buf[offset + 3], buf[offset], buf[offset + 1], buf[offset + 2]));
                offset += 4;
            }
        }
        return bitmap;
    }

**Conclusion:**

I feel that there's something else wrong, and I hope you can answer to this question.