You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WebView读取含HTML的数据库数据时,如何放大字体或移除HTML元素?

WebView显示数据库中的HTML/图片并放大字体的解决方案

Hey there! Let's tackle your problem step by step—you want to display HTML content (including images) from your database in a WebView, and also make the font larger. Here's how to do it properly:

1. Why a wrapped HTML document is needed

Directly loading raw HTML from your database won't let you control the font size consistently. The fix is to wrap that content in a complete HTML document with custom CSS—this lets you set global styles like font size, and also ensure images display correctly.

2. Step-by-step code implementation

First: Fetch data from your database

Assuming your DBQUESTION table has a field for the HTML content (let's call it QUESTION_HTML) and either a Base64-encoded image (IMAGE_BASE64) or a local image path (IMAGE_PATH), start by retrieving that data:

// Your existing query, expanded to get the needed fields
string questionPhrase = @"SELECT QUESTION_HTML, IMAGE_BASE64 FROM DBQUESTION WHERE QUIZT_ID='100'";
var question = objConn.Prepare(questionPhrase);
question.Step(); // Execute query and move to the result row

// Extract the content from the query result
string rawHtml = question.GetString(0);
string imageBase64 = question.GetString(1);

Second: Build the full styled HTML

Now wrap the raw HTML in a document with CSS to adjust font size and handle images. We'll cover both common image storage scenarios:

Scenario 1: Image is stored as Base64

If your database holds the image as a Base64 string, we can embed it directly in the HTML:

// Optional: If your raw HTML doesn't already include the image tag, add it
if (!string.IsNullOrEmpty(imageBase64))
{
    rawHtml += $"<img src='data:image/png;base64,{imageBase64}' alt='Question Image' />";
}

// Build the complete HTML with custom styles
string fullHtml = $@"<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'/>
    <style>
        body {{
            font-size: 20px; /* Adjust this value to make the font larger/smaller */
            padding: 10px;
            line-height: 1.5; /* Optional: Improves readability */
        }}
        img {{
            max-width: 100%; /* Ensures images fit within the WebView */
            height: auto;
            margin: 10px 0;
        }}
    </style>
</head>
<body>
    {rawHtml}
</body>
</html>";

// Load the HTML into the WebView
QuestionTextWeb.NavigateString(fullHtml);
Scenario 2: Image is stored as a local path

If your database stores a path to a local image (like in your app's Assets folder), convert it to a WebView-compatible URI first:

// Get the image path from the database (adjust the index to match your table)
string imagePath = question.GetString(1);
// Convert to a URI the WebView can recognize (ms-appx:/// points to your app's install directory)
string imageUri = new Uri($"ms-appx:///Assets/{imagePath}").ToString();

// Add the image tag to your raw HTML
rawHtml += $"<img src='{imageUri}' alt='Question Image' />";

// Build the styled HTML (same CSS as above)
string fullHtml = $@"<!DOCTYPE html>
<html>
<head>
    <meta charset='utf-8'/>
    <style>
        body {{
            font-size: 20px; /* Tweak this for your desired font size */
            padding: 10px;
        }}
        img {{
            max-width: 100%;
            height: auto;
        }}
    </style>
</head>
<body>
    {rawHtml}
</body>
</html>";

QuestionTextWeb.NavigateString(fullHtml);

3. Key notes

  • Font size control: The font-size property in the CSS is where you adjust the text size—use px, em, or % (e.g., 1.2em for 20% larger than default).
  • Image responsiveness: The max-width: 100% rule ensures images don't overflow the WebView's width, which is crucial for different screen sizes.
  • Your existing XAML is fine: You don't need to modify your WebView XAML code—it works perfectly with this approach:
    <WebView x:Name="QuestionTextWeb" Grid.Row="3" Margin="10,10,0,10" MinHeight="100" MaxHeight="200" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
    

内容的提问来源于stack exchange,提问作者Rose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 07:24:34