http://www.cnblogs.com/onlytiancai/archive/2007/03/24/makehtmlsamle.html

解决方案 »

  1.   

    楼上的朋友,我看了你给的网址了,里面的代码和我写的一样啊。我的意思是,我执行到CreateMHTMLBody时会出错,既然你写的也能成功运行,我很郁闷,我这里怎么就是执行不了呢。我的操作系统是XP,你的呢?会不会与操作系统有关系啊?望知情人不吝告之。
      

  2.   

    怎么没有人看,csdn的高手都那去了,怎么最近问的问题都没人解决阿....!!!!!!!
      

  3.   

    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() );
        }
      

  4.   

    mht好东西,
    图片全到里面了..
      

  5.   

    不要在studio中直接打开,要发布之后在IIS运行就可以了