Skip to content
Advertisement

Can’t convert SQL VARBINARY to byte[] correctly and convert to Image in ASP.NET c#

Here is what I’m doing:

When I convert to byte[] I get something like: {byte[4354567]}

I’m then trying to convert array to Image like so:

And in View I use:

What am I missing?

Advertisement

Answer

<img src=… has to point to an image file by its path, eg <img src="/myImage.jpg">. You can’t stick a binary representation of the image in the src and have it work.

So you could either write those binary images out to disk somewhere (you probably don’t want to do that, as then you’re duplicating the data, and would have to manage synchronizing).

Or you could create some kind of image handler, so the <img src= would be something like: <img src="/myHandler/imageId", and then have the handler read the binary data from the database and respond with the image.

This is an MVC controller action that I’ve used in the past to read a binary PDF out of the DB, and return it as a file. This is in my Competition controller. If this was returning an image, you could call it something like:

<img src="Competition/ViewJobDescription?competitionId=1234" />

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement