Writing to a blob: heads up

Recently I was creating a function to resize an image. The function itself was resizing the image perfectly, however, my file size was always at least the same size…

I think I tried about 8 different pieces of DotNet code, always resulting in the same thing. The image was resized, but the file size was at least the same as the original.

It wasn’t until I started trying my code in Visual Studio, using pure C# that I noticed that the code to resize wasn’t the issue.

That’s why I’ll post this blog, just as a friendly heads up when you use blobs.

Apparently, if there is data in a blob, and you write to an outstream, you overwrite the old blob data.

Sound right, right?
Let’s take this example to see what happens:

At first the blob contains ABCDEFGHIJKLMNOQRSTUVWXYZ

I create an instream to the blob, and do some stuff with it.
Next, I create an outstream to write my result: 0123456789

Finally, I export my blob to a file, and this is the content of my file:

See what I mean?

When there is data in your blob, the outstream is a reference to your blob. So you start writing, but only overwrite the first part of you blob if the result is smaller. If it is larger, there will be no problem though.

So better be sure and clear your blob!

Sadly it took me a while to figure out, since my image was just display OK.
I think that in an image format, there is some sort of delimiter which says it is the end of the image. Basically, you could just hide other stuff in your file, while still being able to open the image without a problem.

Anyway, whenever you run into a problem like this, remember this post and clear your blob!

2 thoughts on “Writing to a blob: heads up

  1. Pingback: Writing to a blob: heads up | Pardaan.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.