Use a System.Drawing.Image in an HTML tag

前端 未结 4 2038
忘了有多久
忘了有多久 2021-01-14 11:21

I have a potentially easy question. I have an image stored in a database, I then used a C# method in my application that gets that image and stores it in a custom class tha

4条回答
  •  [愿得一人]
    2021-01-14 11:55

    I ended up going with Brads Method of solving this (See the comment under the question) I ended up creating a method that takes in a System.Drawing.Image and converts it to a byte array then encode that byte array to get the image. The code looks like the following:

    byte[] imgBytes = turnImageToByteArray(ind.ind.myImageFromDB);
    string imgString = Convert.ToBase64String(imgBytes);
    table += String.Format("img src=\"data:image/Bmp;base64,{0}\">", imgString);
    
    private byte[] turnImageToByteArray(System.Drawing.Image img)
    {
      MemoryStream ms = new MemoryStream();
      img.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
      return ms.ToArray();
    }
    

    Currently this works for my purposes so thank you to everyone and their suggestions, as always everyone comes though and helps :-)

提交回复
热议问题