你保存到流中,把“光标”放在末尾。。。然后把它交给浏览器,我怀疑它是从当前位置读取的。尝试添加:
stream.Position = 0;
就在最后一行之前。
编辑:好吧,你说没用。。。这是一个简短但完整的程序,对我有用。试试这个,看看它是否适合你——如果适合,看看你能不能找出你的代码和这个的区别:
using System;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
class Test
{
[STAThread]
static void Main()
{
Form form = new Form();
WebBrowser browser = new WebBrowser();
browser.Dock = DockStyle.Fill;
form.Controls.Add(browser);
form.Load += delegate { SetDocumentStream(browser); };
Application.Run(form);
}
static void SetDocumentStream(WebBrowser browser)
{
string text = "<html><head><title>Stuff</title></head>" +
"<body><h1>Hello</h1></body></html>";
byte[] bytes = Encoding.UTF8.GetBytes(text);
MemoryStream ms = new MemoryStream();
ms.Write(bytes, 0, bytes.Length);
ms.Position = 0;
browser.DocumentStream = ms;
}
}