簡體   English   中英

package中添加圖片資源並部署到Visual Studio中時,圖片資源的文件路徑是什么?

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

我制作了一個 html 文件,輸出了我的 C# 申請的收據。 在 html 報告中,我在收據頂部有一個徽標。 該圖像位於調試文件夾中,當我運行它進行調試時它工作正常。

但是,當我 package 並部署我的應用程序時,收據丟失了圖像。 我在 package 和部署時將圖像添加為資源,所以我知道它在那里。 我假設我需要輸入完整路徑才能訪問圖像。

但是,我不知道我們 package 和部署時的路徑是什么。 我們package時添加的圖片資源和安裝應用時deploy存儲的圖片資源在哪里?

這是我的 function,它創建了我的 html 文件。 我把圖像源放在哪里,我需要放什么才能在我調試和部署 package 時顯示徽標?

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
}

如果您的徽標不是一個巨大的圖像文件,您可以使用 Base64 編碼將其直接設置到標記中,將圖像轉換為字符串,以代替路徑使用。

您可以使用這樣的在線服務將徽標轉換為 Base64 字符串

然后通過這種方式將其添加到您的 HTML output

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM