0

I use windows 7 and VS2010 to build a C++ MFC project to perform some test.

In the end of the test, I wrote some code to save the test results into a HTML file. Now I want to add some test result photos in D:// into the HTML file.

Is it possible to do that in C++?

In the end of the test:

WriteReport(m_report_info);   //writereport

The definition:

BOOL CControllerDlg::WriteReport(REPORT_INFO reportInfo)
{
if(reportInfo.m_bt_addr.IsEmpty()) return FALSE;
CString strTime = CTime::GetCurrentTime().Format("%Y%m%d%H%M%S");

CStdioFile htmlFile;
CString res = (reportInfo.m_bt_result?"Pass":"Fail");
CString html_name;



......



if(htmlFile.Open(html_name,CFile::modeCreate | CFile::modeReadWrite))
{
    CString strHTML =  "<html>";
            strHTML += "<title>Test Report</title>";
            strHTML += "<body>";
            strHTML += "<h1 align=center>Test Report</h1>";




            strHTML += "</body>";
            strHTML += "</html>";
    htmlFile.WriteString(strHTML);
    htmlFile.Close();
}
else
{

    return FALSE;
}
return TRUE;
}

The photo is in D://test//photo1. I want to insert the photo in the body part of the report HTML file. I am using C++, so it's different from front-end method.

Bill
  • 33
  • 7
  • 2
    Er... Just use an `` tag? – Shawn Jan 21 '19 at 02:58
  • Do you want to _embed_ the image into your html file, not just put a link to the image file? Then maybe this could be interesting: https://stackoverflow.com/questions/2807251/can-i-embed-a-png-image-into-an-html-page – Jabberwocky Jan 21 '19 at 10:39
  • Possible duplicate of [Can I embed a .png image into an html page?](https://stackoverflow.com/questions/2807251/can-i-embed-a-png-image-into-an-html-page) – Jabberwocky Jan 21 '19 at 10:40
  • It's web design. I am using C++ to write a MFC program. They're different. – Bill Jan 22 '19 at 02:01
  • @Bill My comment still stands, the link I provided does not contain C++ code, it just explains the concept how to _embed_ an image into html code, if it's really that you want, your question is not very clear. – Jabberwocky Jan 22 '19 at 10:46

1 Answers1

1

Use HTML <img> tag to insert images.

Note that HTML files expect UTF8 content, so you may want to compile your program in Unicode (UTF16) then convert the text to UTF8 as follows:

if(htmlFile.Open(html_name, CFile::modeCreate | CFile::modeWrite))
{
    CStringW strHTML = L"<!DOCTYPE HTML><html>";
    strHTML += L"<title>Test Report</title>";
    strHTML += L"<body>";
    strHTML += L"<h1 align=center>Test Report</h1>";
    strHTML += L"<img src=\"file:///D://test//photo1.jpg\" />";
    strHTML += L"</body>";
    strHTML += L"</html>";

    CStringA utf8 = CW2A(strHTML, CP_UTF8);
    htmlFile.Write(utf8.GetString(), utf8.GetLength());
    htmlFile.Close();
}
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77
  • 1
    I've upvoted your answer, but on a second thought he probably want this: https://stackoverflow.com/questions/2807251/can-i-embed-a-png-image-into-an-html-page – Jabberwocky Jan 21 '19 at 10:40
  • Sorry,not that, I want the method in C++. – Bill Jan 22 '19 at 01:59