如何将网页文件保存为Mht. http://www.cnblogs.com/onlytiancai/archive/2007/03/24/makehtmlsamle.html 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 楼上的朋友,我看了你给的网址了,里面的代码和我写的一样啊。我的意思是,我执行到CreateMHTMLBody时会出错,既然你写的也能成功运行,我很郁闷,我这里怎么就是执行不了呢。我的操作系统是XP,你的呢?会不会与操作系统有关系啊?望知情人不吝告之。 怎么没有人看,csdn的高手都那去了,怎么最近问的问题都没人解决阿....!!!!!!! Browser 的内容保存为单一 mht 文件: public static void SaveToMht( string mhtPath, WebBrowser wb ) { StringBuilder sb = new StringBuilder(); sb.AppendLine( "MIME-Version:1.0" ); sb.AppendLine( "Content-Type:multipart/related;" ); sb.AppendLine( "\ttype=\"text/html\";" ); sb.AppendLine( "\tboundary=\"----=传说中的分割线=------\"" ); sb.AppendLine(); sb.AppendLine( "------=传说中的分割线=------" ); sb.AppendLine( "Content-Type:text/html;" ); sb.AppendLine( "Content-Transfer-Encoding:8bit" ); sb.AppendLine();//-----------------------------------------------------------------------------------------// 特别注意:这部分代码是需要改进的地方。// 因为所有的图片都需要保存到 mht 里面,所以扫描图片的方式有待改进。//// 我的代码里只用了 document.images 来扫描所有 img 标签里的图片,// 需要改进的是增加对具有 background-image 的 style 的元素的扫描,// 以及,想办法处理 filter 里出现的图片。// // 处理 filter 里的图片是最麻烦的部分,你可能要附加一部分脚本来专门处理,// 因为 mht 里的图片数据是不能被 filter 直接识别的。一个可行的方案是,// 找出这些 filter 图片放置的位置,用 img 代替之,然后在 onload 中附加一段脚本,// 将这些 img 重新处理为 filter,以还原其保存时的状态。//// 另外需要注意的一点是,// 因为可能会增加 img 来暂代 filter 里的图片,这样就会对源文件的内容造成一些改动,// 改动后的源文件最好是用一个 out(或者 ref) string 传回,让函数直接返回被处理出来的 image。//-----------------------------------------------------------------------------------------// List<string> images = new List<HtmlElement>();// images.AddRange( ScanAllImgTags( wb ) );// images.AddRange( ScanAllBackgroundImages( wb ) );// string modifiedSourceHtml = null;// images.AddRange( ScanAllFilterImages( wb, out modifiedSourceHtml ) );//-----------------------------------------------------------------------------------------// sb.AppendLine( modifiedSourceHtml );//-----------------------------------------------------------------------------------------// 如果后面扫描 filter 时改动了源文件内容的话,// 下面这行就应该注释掉,而使用上面的代码。//----------------------------------------------------------------------------------------- sb.AppendLine( wb.DocumentText );//----------------------------------------------------------------------------------------- sb.AppendLine(); SortedList<string, object> paths = new SortedList<string, object>();//-----------------------------------------------------------------------------------------// for ( int i=0; i < images.Count; ++i )// {// string path = new Uri( images[i] ).LocalPath;//----------------------------------------------------------------------------------------- for ( int i=0; i < wb.Document.Images.Count; ++i ) { string path = new Uri( wb.Document.Images[i].GetAttribute( "src" ) ).LocalPath;//----------------------------------------------------------------------------------------- if ( paths.ContainsKey( path ) ) continue; paths.Add( path, null ); sb.AppendLine( "------=传说中的分割线=------" ); sb.AppendLine( "Content-Type:application/octet-stream" ); sb.AppendLine( "Content-Transfer-Encoding:base64" ); sb.AppendLine( "Content-Location:" + path ); sb.AppendLine(); sb.AppendLine( Convert.ToBase64String( File.ReadAllBytes( path ), Base64FormattingOptions.InsertLineBreaks ) ); sb.AppendLine(); } paths.Clear(); sb.AppendLine( "------=传说中的分割线=------" ); File.WriteAllText( mhtPath, sb.ToString() ); } mht好东西,图片全到里面了.. 不要在studio中直接打开,要发布之后在IIS运行就可以了 帮忙写个sql语句 实现动态网址的下载 如何修改下段程序,使<channel>下能建立子节点<item>,而<item>中还有其他节点? 求一算法 需要封装一个StreamWriter的方法ReadLine(). 欢迎讨论! 为什么我的tcp lienster只能侦听一次! 请问怎样在c#里将一个short类型的数字转化为Byte数组 兄弟们,我问一个很菜的问题,希望大家帮我!!内详 如何限制一个文本框只能输入数字?最好还能限制输入数字的大小。 【C#】我正在做个象棋小程序,但是不知道怎么传输数据 socket 同步和异步的区别是啥? 帮我读读代码?
public static void SaveToMht( string mhtPath, WebBrowser wb )
{
StringBuilder sb = new StringBuilder();
sb.AppendLine( "MIME-Version:1.0" );
sb.AppendLine( "Content-Type:multipart/related;" );
sb.AppendLine( "\ttype=\"text/html\";" );
sb.AppendLine( "\tboundary=\"----=传说中的分割线=------\"" );
sb.AppendLine();
sb.AppendLine( "------=传说中的分割线=------" );
sb.AppendLine( "Content-Type:text/html;" );
sb.AppendLine( "Content-Transfer-Encoding:8bit" );
sb.AppendLine();//-----------------------------------------------------------------------------------------
// 特别注意:这部分代码是需要改进的地方。
// 因为所有的图片都需要保存到 mht 里面,所以扫描图片的方式有待改进。
//
// 我的代码里只用了 document.images 来扫描所有 img 标签里的图片,
// 需要改进的是增加对具有 background-image 的 style 的元素的扫描,
// 以及,想办法处理 filter 里出现的图片。
//
// 处理 filter 里的图片是最麻烦的部分,你可能要附加一部分脚本来专门处理,
// 因为 mht 里的图片数据是不能被 filter 直接识别的。一个可行的方案是,
// 找出这些 filter 图片放置的位置,用 img 代替之,然后在 onload 中附加一段脚本,
// 将这些 img 重新处理为 filter,以还原其保存时的状态。
//
// 另外需要注意的一点是,
// 因为可能会增加 img 来暂代 filter 里的图片,这样就会对源文件的内容造成一些改动,
// 改动后的源文件最好是用一个 out(或者 ref) string 传回,让函数直接返回被处理出来的 image。
//-----------------------------------------------------------------------------------------
// List<string> images = new List<HtmlElement>();
// images.AddRange( ScanAllImgTags( wb ) );
// images.AddRange( ScanAllBackgroundImages( wb ) );
// string modifiedSourceHtml = null;
// images.AddRange( ScanAllFilterImages( wb, out modifiedSourceHtml ) );
//-----------------------------------------------------------------------------------------
// sb.AppendLine( modifiedSourceHtml );
//-----------------------------------------------------------------------------------------
// 如果后面扫描 filter 时改动了源文件内容的话,
// 下面这行就应该注释掉,而使用上面的代码。
//-----------------------------------------------------------------------------------------
sb.AppendLine( wb.DocumentText );
//-----------------------------------------------------------------------------------------
sb.AppendLine(); SortedList<string, object> paths = new SortedList<string, object>();
//-----------------------------------------------------------------------------------------
// for ( int i=0; i < images.Count; ++i )
// {
// string path = new Uri( images[i] ).LocalPath;
//-----------------------------------------------------------------------------------------
for ( int i=0; i < wb.Document.Images.Count; ++i )
{
string path = new Uri( wb.Document.Images[i].GetAttribute( "src" ) ).LocalPath;
//-----------------------------------------------------------------------------------------
if ( paths.ContainsKey( path ) ) continue;
paths.Add( path, null );
sb.AppendLine( "------=传说中的分割线=------" );
sb.AppendLine( "Content-Type:application/octet-stream" );
sb.AppendLine( "Content-Transfer-Encoding:base64" );
sb.AppendLine( "Content-Location:" + path );
sb.AppendLine();
sb.AppendLine( Convert.ToBase64String( File.ReadAllBytes( path ),
Base64FormattingOptions.InsertLineBreaks ) );
sb.AppendLine();
}
paths.Clear();
sb.AppendLine( "------=传说中的分割线=------" );
File.WriteAllText( mhtPath, sb.ToString() );
}
图片全到里面了..