1

I have a table with all employee pictures in SQL Server table. Column is of type Image

I have a requirement to download emplyee profile in word format with their picture in it. I was able to export all their data in HTML format and then I used the following C# code to convert that html to word and give it as download.

public static void HtmlToWordDownload(string HTML, string FileName)
{
    lock (LockMulti)
    {
        string strBody = string.Empty;
        strBody = @"<html xmlns:o='urn:schemas-microsoft-com:office:office' " +
        "xmlns:w='urn:schemas-microsoft-com:office:word'" +
        "xmlns='http://www.w3.org/TR/REC-html40'>" +
        "<head><title>Document Title</title>" +
         "<!--[if gte mso 9]><xml><w:WordDocument><w:View>Print</w:View><w:Zoom>100</w:Zoom>" +
         "<w:DoNotOptimizeForBrowser/></w:WordDocument></xml><![endif]-->" +
         "<style> @page Section1 {size:8.27in 11.69in; mso-first-footer:ff1; mso-footer: f1; mso-header: h1; " +
            //"border:solid navy 2.25pt; padding:24.0pt 24.0pt 24.0pt 24.0pt; " +
         "margin:0.6in 0.6in 0.6in 0.6in ; mso-header-margin:.1in; " +
         "mso-footer-margin:.1in; mso-paper-source:0;} " +
         "div.Section1 {page:Section1;} p.MsoFooter, li.MsoFooter, " +
         "div.MsoFooter{margin:0in; margin-bottom:.0001pt; " +
         "mso-pagination:widow-orphan; tab-stops:center 3.0in right 6.0in; " +
         "font-size:12.0pt; font-family:'Arial';} " +
         "p.MsoHeader, li.MsoHeader, div.MsoHeader {margin:0in; " +
         "margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:center " +
         "3.0in right 6.0in; font-size:12.0pt; font-family:'Arial';}--></style></head> ";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
        HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + FileName + ".doc");
        StringBuilder htmlCode = new StringBuilder();
        htmlCode.Append(strBody);
        htmlCode.Append("<body><div class=Section1>");
        htmlCode.Append(HTML);
        htmlCode.Append("</div></body></html>");
        HttpContext.Current.Response.Write(htmlCode.ToString());
        HttpContext.Current.ApplicationInstance.CompleteRequest();
        HttpContext.Current.Response.Flush();
    }
}

It is working absolutely fine. Now how can I embed employe's picture in it. What format should I convert that Image to embed in the HTML. I tried the following query, but it is giving same output for all the pictures. Please help.

select 
    top 30 convert(varchar, convert(binary, i.Photo)) 
from hrm.hrm_rp_Employee_Image i

Output is as follows

(No column name)
ÿØÿà
ÿØÿà
ÿØÿà
ÿØÿà
ÿØÿà
ÿØÿà

Is my approach correct or something else needs to be done?

Update

I found out the following query to convert it to base64.

declare @pic varchar(max)
SELECT  @pic = '<img src="data:image/jpg;base64,' + CAST(N'' AS XML).value(
          'xs:base64Binary(xs:hexBinary(sql:column("Pic")))'
        , 'VARCHAR(MAX)'
    ) 
FROM (
   SELECT CAST(Photo AS VARBINARY(MAX)) AS Pic from hrm.hrm_rp_Employee_Image
) AS h;

set @pic = @pic + '" style="height: 100px; width: 100px;" />' 
print @pic

It successfully converts and gives me a string and it works perfectly when I test it on any online html editor.

But it is not showing in my word file after download, an image with red color 'X' symbol appears, can somebody tell me what could be the problem.

  • is it necessary to store the image in the `database`? if not you can store the image in some other location and then store the path in the database and use that and if you want to work with `image` column then give this link a shot [link](http://aspdotnetcodes.com/Insert_Images_Database.aspx). – vikscool May 22 '18 at 05:10
  • Yes, I cannot change that because it was decided by IT manager and it was so for long time, my only way is to convert that image to a string and assign it to – Prasanth Kumar Vinakota May 22 '18 at 06:46

1 Answers1

0

you need to convert binary data into image in c#, then append it into html.

below link will help you to convert binary data to image

link

pankaj singh
  • 95
  • 10