views:

215

answers:

5

I have a test that uploads a bytearray (image - jpg) to our database (Sql Server FileStream) and then retrieves it through a HttpHandler. Next I compare the two.

Now, they are almost the same except for the first four bytes...

What's in those first four bytes?

First byteArray : 255, 216, 255, 224, from here on they are the same

Second byteArray: 63, 63, 63, 63 ...

When retrieved from the db by the handler, the string is converted to a bytearray like this

Update

StringWriter writer;
SimpleWorkerRequest worker;

writer = new StringWriter();
worker = new SimpleWorkerRequest(page, query, writer);
HttpRuntime.ProcessRequest(worker);
writer.Flush();

var  encoding=new ASCIIEncoding();
var blob = encoding.GetBytes(writer.GetStringBuilder().ToString());

return blob;
+1  A: 

Writer class is a text encoding stream, by the sound of it, you are using a StreamWriter which is an implementation of System.IO.TextWriter. This is incorrect. You should be using a binary stream to write the data in its native format.

Hope this helps gives you the hint in the direction you are looking for, Best regards, Tom.

tommieb75
A: 

If your goal is simply to get it working and are able to change the database scheme, you could go with this post:http://blogs.msdn.com/domgreen/archive/2009/09/06/comparing-two-images-in-c.aspx

The author uses base64 encoding to compare existing images, but you could use the encoding to store the image in the db much the same way.

br, Marcel

Marcel
+11  A: 

If you want to know why this happens, this is because ASCII encoding can't handle characters above 128. The first four characters are converted to '?'.

Since you have an image in the byte array, you shouldn't try to convert it to text in order to compare the two arrays. For comparison's sake, you should iterate through all bytes and print their values. It would be better to use hex notation for this.

kgiannakakis
+1 recognizing 63 as the question mark. Makes sense.
Dave
+4  A: 

According to the Wikipedia article, those first four bytes are:

  • 0xFFD8 Start Of Image
  • 0xFFEn Application-specific marker

Yours is 0xFFE0, which is the APP0 marker, required after the Start-Of-Image. See www.w3.org/Graphics/JPEG/jfif3.pdf, section entitled "APP0 marker used to identify JPEG FIF."

Dave
+1  A: 

An unmodified SimpleWorkerRequest is unsuitable for receiving binary data. Let me quote from MSDN (highlighting by me):

To achieve richer functionality, such as providing posted content and headers and capturing the response headers or response body as binary data, you should extend SimpleWorkerRequest and override the appropriate HttpWorkerRequest methods.

Heinzi
Well, I'll look if someone has done this before me. thx.
Lieven Cardoen