Tutorial :Convert BLOB to Jpg using C#



Question:

I am currently developing a web application that receives data from an on-site database. The database developers have developed some web-services that I am able to call and send/receive data to and from the the database.

When I want to display an image the method returns the image in BLOB format. My question is: what is the best way to convert BLOB to .jpg or .bmp so I can display the image correctly? If someone could point me in the right direction that would be great!

Cheers, Tristan


Solution:1

You can create a generic handler (ashx file) and have it return the byte array.

For example, the url http://www.example.com/GetImage.ashx?imageId=1 could contain code to write the bytes received from the image with id of 1.

You will need to implement the handler code yourself to do the database query, etc. and write the bytes out to the response. You will also need to set the response mime type to jpg, png or whatever format your images are in.

Check out this stackoverflow post for more details: How to bind a MemoryStream to asp:image control?


Solution:2

byte[] array and mimetype are the key words :)


Solution:3

There is no one image format associated with BLOB. It's simply a way of putting raw binary in a database. You need to figure out which format it actually is (libmagic may help), then you can convert if needed.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »