using System;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;
namespace HTMLConversion
{
/// <summary>
/// HtmlConvertToMht 类
/// </summary>
#region HtmlConvertToMht 类的实现
public class HtmlConvertToMhtByCdo : IHtmlConvertToMht
{
public HtmlConvertToMhtByCdo()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 变量
string _CharSet="gb2312";
#endregion
#region 属性
public string CharSet
{
get
{
return _CharSet;
}
set
{
_CharSet = value;
}
}
#endregion
#region 方法
/// <summary>
/// 把Html格式二进制流转换成Mht格式的二进制流
/// </summary>
/// <param name="htmlUrl">Html格式的二进制流</param>
/// <returns>Mht格式的二进制流</returns>
public byte[] ConvertHtmlUrlToMht(string htmlUrl)
{
string strMhtContent = string.Empty;
byte[] BytMhtContent = null;
CDO.Message message = new CDO.MessageClass();
ADODB.Stream stream=null;
message.MimeFormatted =true;
message.CreateMHTMLBody(htmlUrl,CDO.CdoMHTMLFlags.cdoSuppressNone,"","");
stream = message.GetStream();
stream.Charset = _CharSet;
strMhtContent= stream.ReadText(stream.Size);
BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
return BytMhtContent;
}
/// <summary>
/// 把Html格式二进制流转换成Mht格式的二进制流,成功返回true,失败返回false
/// </summary>
/// <param name="htmlUrl">Html格式的二进制流</param>
/// <param name="filePath">Mht文件的保存路径</param>
/// <returns>操作结果 成功:true,失败;false</returns>
public bool ConvertHtmlUrlToMht(string htmlUrl,string filePath)
{
bool isConvertHtmlBytesToMht = false;
CDO.Message message = new CDO.MessageClass();
ADODB.Stream stream=null;
message.MimeFormatted =true;
message.CreateMHTMLBody(htmlUrl,CDO.CdoMHTMLFlags.cdoSuppressNone,"","");
stream = message.GetStream();
stream.SaveToFile(filePath,ADODB.SaveOptionsEnum.adSaveCreateOverWrite);
message=null;
stream.Close();
isConvertHtmlBytesToMht=true;
return isConvertHtmlBytesToMht;
}
/// <summary>
/// 把Html格式二进制流转换成Mht格式的二进制流
/// </summary>
/// <param name="htmlContent">Html格式的二进制流</param>
/// <returns>Mht格式的二进制流</returns>
public byte[] ConvertHtmlBytesToMht(byte[] htmlContent)
{
string strMhtContent = string.Empty;
string strHtmlContent = Encoding.Default.GetString(htmlContent);
CDO.Message message = new CDO.MessageClass();
ADODB.Stream stream=null;
message.MimeFormatted =true;
message.HTMLBody = strHtmlContent;
stream = message.GetStream();
stream.Charset = _CharSet;
strMhtContent= stream.ReadText(stream.Size);
byte[] BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
return BytMhtContent;
}
/// <summary>
/// 根据输入的Html文本内容(二进制)返回Mht文本内容(二进制流),成功返回true,失败返回false
/// </summary>
/// <param name="htmlContent">Html格式的二进制流</param>
/// <param name="filePath">Mht文件的保存路径</param>
/// <returns>操作结果 成功:true,失败;false</returns>
public bool ConvertHtmlBytesToMht(byte[] htmlContent,string filePath)
{
bool isConvertHtmlBytesToMht = false;
string strHtmlContent = Encoding.GetEncoding(_CharSet).GetString(htmlContent);
CDO.Message message = new CDO.MessageClass();
ADODB.Stream stream=null;
message.MimeFormatted =true;
message.HTMLBody = strHtmlContent;
stream = message.GetStream();
stream.Charset = _CharSet;
string strMhtContent= stream.ReadText(stream.Size);
byte[] BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
FileStream fileStream = new FileStream(filePath,FileMode.Create,FileAccess.Write);
fileStream.Write(BytMhtContent,0,BytMhtContent.Length);
message=null;
stream.Close();
isConvertHtmlBytesToMht=true;
return isConvertHtmlBytesToMht;
}
/// <summary>
/// 根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流)
/// </summary>
/// <param name="htmlContent">Html文本内容(字符串)</param>
/// <returns>Mht格式的二进制流</returns>
public byte[] ConvertHtmlTextToMht(string htmlContent)
{
string strMhtContent = String.Empty;
CDO.Message message = new CDO.MessageClass();
ADODB.Stream stream=null;
message.MimeFormatted =true;
message.HTMLBody = htmlContent;
stream = message.GetStream();
stream.Charset = _CharSet;
strMhtContent= stream.ReadText(stream.Size);
byte[] bytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
return bytMhtContent;
}
/// <summary>
/// 根据输入的Html文本内容(字符串)返回Mht文本内容(二进制流),成功返回true,失败返回false
/// </summary>
/// <param name="htmlContent">Html文本内容(字符串)</param>
/// <param name="filePath">Mht文件的保存路径</param>
/// <returns>操作结果:成功:true,失败:false</returns>
public bool ConvertHtmlTextToMht(string htmlContent,string filePath)
{
bool isConvertHtmlBytesToMht = false;
CDO.Message message = new CDO.MessageClass();
ADODB.Stream stream=null;
message.MimeFormatted =true;
message.HTMLBody = htmlContent;
stream = message.GetStream();
stream.Charset = _CharSet;
string strMhtContent= stream.ReadText(stream.Size);
byte[] BytMhtContent = Encoding.GetEncoding(_CharSet).GetBytes(strMhtContent);
FileStream fileStream = new FileStream(filePath,FileMode.Create,FileAccess.Write);
fileStream.Write(BytMhtContent,0,BytMhtContent.Length);
message=null;
stream.Close();
isConvertHtmlBytesToMht=true;
return isConvertHtmlBytesToMht;
}
#endregion
} // class HtmlConvertToMhtByCdo
#endregion
} // HTMLConversion