I have a web api service that accepts an image (byte[]) as part of the data for the post/create method. I save the image data in a sql server blob column. When I use the corresponding get method I see the data as
"ffd8ffe000104a46494600010100000100010000ffdb004300100b0c0e0c0a100e0d0e..." (shortened)
When I look at the bytes of the original image they look just like that (snip from my binary editor):
![Manual Method[(http://sdrv.ms/1bjgsnX)
I need a way to convert my data into the correct jpg file. I’ve tried several things but finally had to manually do it. I didn't include here my attempts at using Image but they were also many and unsuccessful. There must be a more standard way of doing what I need to do. For all of these methods I use the same File writing code:
string base64String = Convert.ToBase64String(photoFromDB, Base64FormattingOptions.None);
//various methods to convert that to a byte[] tempBytes
string DestFilePath = "testManual.jpg";
System.IO.FileStream fs = new System.IO.FileStream(DestFilePath,
System.IO.FileMode.Create, System.IO.FileAccess.Write);
fs.Write(tempBytes, 0, tempBytes.Length);
fs.Close();
Here are my attempts and the outcomes:
![All Methods[(http://sdrv.ms/1bjgegH)
//Manual method that works but I don’t like.
byte[] tempBytes = new byte[base64String.Length/2];
string tempString;
byte tempByte;
int count = 0;
for (int i = 0; i < base64String.Length; i = i+2)
{
tempString = base64String.Substring(i, 2);
tempByte = Convert.ToByte(tempString, 16);
tempBytes[count++] = tempByte;
}
//Unicode convert
//I can see my data here but it has extra nulls included.
tempBytes = System.Text.Encoding.Unicode.GetBytes(base64String);
//UTF32 convert
//again can see my data but even more nulls included
tempBytes = System.Text.Encoding.UTF32.GetBytes(base64String);
//UTF7, UTF8, and Default, ASCII, BigEndianUnicodegive me the same output
//I can see my data but it isn’t correct yet
tempBytes = System.Text.Encoding.UTF7.GetBytes(base64String);
tempBytes = System.Text.Encoding.UTF8.GetBytes(base64String);
tempBytes = System.Text.Encoding.Default.GetBytes(base64String);
tempBytes = System.Text.Encoding.ASCII.GetBytes(base64String);
tempBytes = System.Text.Encoding.BigEndianUnicodegive.GetBytes(base64String);
What’s the secret sauce I’m missing?