简体   繁体   中英

What is the file path for image resources when you add them during package and deploy in Visual Studio?

I have made a html file that outputs a receipt for my C# application. In the html report I have a logo at the top of the receipt. The image is in the debug folder and it works fine when I run it debugging.

However when I package and deploy my application, the receipt loses the image. I add the image as a resource when I package and deploy so I know its there. I am assuming that I need to put the full path to access the image.

However, I do not know what the path will be when we package and deploy. Where are the image resources we add when we package and deploy stored when we install the application?

Here is my function that creates my html file. Where I put the image source what do I need to put to get the logo to show up when I debug and when I package and deploy?

private StringBuilder GenerateReport()
{
    StringBuilder html = new StringBuilder();
    StringBuilder css = new StringBuilder();
    try
    {
        // CSS is a way to style the HTML page. Each HTML tag can be customized.
        // In this example, the H1 and TD tags are customized.
        // Refer to this website for examples: https://www.w3schools.com/Css/css_syntax.asp

        css.AppendLine("<style>");
        css.AppendLine("td {padding: 5px; text-align:center; font-weight: bold; text-align: center; font-size: 12px;}");
        css.AppendLine("h1 {color: orange;}");
        css.AppendLine("</style>");

        // HTML is used to format the layout of a webpage. This will be the frame
        // we use to place our data in. CSS is used to style the page to look a
        // certain way.

        // The <HTML> and </HTML> tags are the start and end of a webpage.
        // The <HEAD> and </HEAD> tags gives information about the webpage
        // such as the title and if there is any CSS styles being used.
        // The text between the <TITLE> and </TITLE> tags are used by the
        // browser to display the name of the page.
        // <BODY> and </BODY> is where the data of the page is stored
        // <H1> and </H1> is the largest font size for headings. These
        // can be from H1 to H6. H6 is the smallest font. https://www.w3schools.com/tags/tag_hn.asp

        html.AppendLine("<html>");
        css.AppendLine("<center {display: block;margin - left: auto;margin - right: auto;width: 50 %;}</center>");
        html.AppendLine($"<head>{css}<title>{"Receipt"}</title></head>");
        //css.AppendLine("<left {display: block;margin - left: auto;margin - right: auto;width: 50 %;}</left>");
        html.Append("<img src= Debug\\hunting.jpg style=' align: center; width: 75px; height: 50px;'>");
        html.AppendLine("<body>");

        html.AppendLine($"<h1>{" Order Receipt"}</h1>");
        html.Append($"<br></br>");
        html.Append($"<p style = 'text-indent: -550px; font-size: 15px'><b>{"Customer: " + strFirstName + " " + strLastName}</b></p>");
        html.Append($"<p style = 'text-indent: -550px; font-size: 15px'><b>{"Order Number: " + strMaxOrderID}</b></p>");
        html.Append($"<p style = 'text-indent: -550px; font-size: 10px'><b>{"Phone Number: " + strPhoneNumber}</b></p>");
        // Create table of data
        // <TABLE> and </TABLE> is the start and end of a table of rows and data.
        // <TR> and </TR> is one row of data. They contain <TD> and </TD> tags.
        // <TD> and </TD> represents the data inside of the table in a particular row.
        // https://www.w3schools.com/tags/tag_table.asp

        // I used an <HR /> tag which is a "horizontal rule" as table data.
        // You can "span" it across multiple columns of data.

        html.AppendLine("<table>");
        html.AppendLine("<tr><td>Item Name</td><td>Quantity</td><td>Price</td></tr>");
        html.AppendLine("<tr><td colspan=3><hr /></td></tr>");
        for (int i = 0; i < lstShoppingCartName.Count; i++)
        {
            html.Append("<tr>");
            html.Append($"<td>{lstShoppingCartName[i]}</td>");
            html.Append($"<td>{lstShoppingCartQuantity[i]}</td>");
            html.Append($"<td>{lstShoppingCartCost[i]}</td>");
            html.Append("</tr>");
            html.AppendLine("<tr><td colspan=4><hr /></td></tr>");
        }
        html.AppendLine("</table>");
        html.Append($"<br></br><br></br>");
        html.Append($"<p style = 'align:center; text-indent: 390px; font-size: 15px '><b>{"SubTotal: " + decSubTotal.ToString("C2")}</b></p>");
        html.Append($"<p style = 'align:center; text-indent: 371px; font-size: 15px '><b>{"Discount Percent: " + decDiscountPercent.ToString("N3")}</b></p>");
        html.Append($"<p style = 'align:center; text-indent: 422px; font-size: 15px '><b>{"Discount: " + decDiscount.ToString("C2")}</b></p>");
        html.Append($"<p style = 'align:center; text-indent: 297px; font-size: 15px '><b>{"SubTotal After Discount: " + decSubTotalDiscount.ToString("C2")}</b></p>");
        html.Append($"<p style = 'align:center; text-indent: 430px; font-size: 15px '><b>{"Taxes: " + decTaxes.ToString("C2")}</b></p>");
        html.Append($"<p style = 'align:center; text-indent: 450px; font-size: 20px ' ><b>{"Total: " + decTotal.ToString("C2")}</b></p>");
        html.Append($"<div><button onClick='window.print()'> {"Print this page"}</ button ></ div >");
        html.AppendLine("</body></html>");
    }
    catch(Exception ex)
    {
        MessageBox.Show(message + ex.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    return html; // The returned value has all the HTML and CSS code to represent a webpage
}

if your logo isn't an huge image file, you could set it directly into the markup, using Base64 encoding, that transform an image into a string, to use instead of path.

You can convert your logo into Base64 string using an online service like this

and then add it to your HTML output this way

<img src="data:image/png;base64,#past here your image encoded text#">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM