Data URI scheme(RFC 2397)讓小型資料直接嵌入網頁,避免外部檔案請求。格式:data:[媒體類型][;base64],資料。例:
<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAA..." width="16" height="16" />
支援類型:文字、HTML、CSS、JavaScript、圖片、圖示等。
Label、兩個 TextBox(路徑、結果)、一個 Button(瀏覽)。Name 與 Text 屬性。private void button_browse_Click(object sender, EventArgs e) {
OpenFileDialog fbd = new OpenFileDialog();
fbd.Title = "請選擇一張圖片:";
fbd.CheckFileExists = true;
fbd.FileName = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
if (fbd.ShowDialog() == DialogResult.OK) {
textBox_saveDir.Text = fbd.FileName;
try {
Image img = Image.FromFile(textBox_saveDir.Text, true);
MemoryStream ms = new MemoryStream();
ImageFormat fmt = ImageFormat.Png;
string ext = Path.GetExtension(textBox_saveDir.Text).ToLower();
switch (ext) {
case ".gif": fmt = ImageFormat.Gif; break;
default: ext = ".png"; break;
}
img.Save(ms, fmt);
textBox1.Text = "<img src=\"data:image/" + ext.TrimStart('.') + ";base64," +
Convert.ToBase64String(ms.GetBuffer()) + "\" width=\"" + img.Width +
"\" height=\"" + img.Height + "\" />";
}
catch (Exception ex) {
textBox1.Text = "錯誤:" + ex.Message + "\r\n堆疊:" + ex.StackTrace;
}
}
}
<img ... /> 複製到文字檔,存為 .html。