0

I am trying to save an image to my database in byte array format, but when I look at the database I only see the bytes but not the image! How can I make it so I do not see the byte code but the image? Do I have to reverse the process on client side? Please help! Thank you!

Byte conversion function:

private byte[] String_To_Bytes2(string strInput)
        {
            int numBytes = (strInput.Length) / 2;
            byte[] bytes = new byte[numBytes];
            for (int x = 0; x < numBytes; ++x)
            {
                bytes[x] = Convert.ToByte(strInput.Substring(x * 2, 2), 16);
            }
            return bytes;
        }
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
xDevil
  • 147
  • 1
  • 2
  • 12
  • 1
    The best solution will be, store image in root folder and save image name in database, that will help you in coding. – Kushal Vora Dec 31 '15 at 10:07
  • thing is that i need to store it on the database because this will not be a local software – xDevil Dec 31 '15 at 10:19
  • There's no such notion as image in a database. An image is an array of bytes and databases have column types allowing to store those arrays. I don't know what image do you expect to see. – Darin Dimitrov Dec 31 '15 at 10:44
  • interesting that you say that since SQL has image type.... – xDevil Dec 31 '15 at 10:45
  • And where do you expect to see this image? – Darin Dimitrov Dec 31 '15 at 10:46
  • Are you displaying it somewhere in your application after retrieving it from the database? – Darin Dimitrov Dec 31 '15 at 10:47
  • yes i want to see it in a table in view! As you can see from the code above the image is stored as a byte array! The problem is that after i store it i still see it as a byte array and not as a picture! – xDevil Dec 31 '15 at 10:49

1 Answers1

0

I managed to solve the problem by using the first solution found on the following post: Displaying database image (bytes[]) in Razor/MVC3?

Model.Content is a byte[] image.

@{
    string imageBase64 = Convert.ToBase64String(Model.Content);
    string imageSrc = string.Format("data:image/gif;base64,{0}", imageBase64);}  <img src="@imageSrc" alt="@Model.Name" width="100" height="100" />
Community
  • 1
  • 1
xDevil
  • 147
  • 1
  • 2
  • 12