调用示例:
Bitmap pic = PicHistory.GetHistory(this);
if (pic == null)
{
pic = Sci.WebPageTool.ToBitmap(page, delay, width, height, scroll); // 抓取网页为图像
if(pic != null) PicHistory.SaveHistory(this, pic);
}
1、PicHistory.cs
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
namespace Sci
{
public class PicHistory
{
#region 历史数据处理逻辑
/// <summary>
/// 获取history目录
/// </summary>
public static string historyDir()
{
string dirPath = AppDomain.CurrentDomain.BaseDirectory + "history\\pic";
//string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//string dirPath = Path.Combine(dir, "history");
if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
return dirPath;
}
/// <summary>
/// 根据id保存历史数据
/// </summary>
/// <param name="id"></param>
/// <param name="data"></param>
public static void SaveHistory(string id, Bitmap pic)
{
string historyPath = Path.Combine(historyDir(), id);
pic.Save(historyPath, ImageFormat.Jpeg);
}
/// <summary>
/// 获取id对应的历史数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static Bitmap GetHistory(string id)
{
string historyPath = Path.Combine(historyDir(), id);
if (File.Exists(historyPath))
{
DateTime now = DateTime.Now;
long second = now.AddSeconds(1).Ticks - now.Ticks;
long nowSecond = DateTime.Now.Ticks / 10000000;
long preSecond = new FileInfo(historyPath).LastWriteTime.Ticks / 10000000;
if (nowSecond - preSecond > 60 * 60) return null; // 若当前时间距离上次修改超出1个小时,则不使用历史数据
else return Bitmap.FromFile(historyPath) as Bitmap; // 读取历史数据内容
}
return null;
}
/// <summary>
/// 将data保存至_page对应的历史数据中
/// </summary>
/// <param name="_page"></param>
/// <param name="data"></param>
public static void SaveHistory(Page _page, Bitmap data)
{
string url = _page.Request.Url.ToString();
string id = "p" + url.GetHashCode() + ".jpg"; // 每个页面url对应一个唯一的id
SaveHistory(id, data);
}
/// <summary>
/// 获取_page对应的历史数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static Bitmap GetHistory(Page _page)
{
string url = _page.Request.Url.ToString();
string id = "p" + url.GetHashCode() + ".jpg"; // 每个页面url对应一个唯一的id
return GetHistory(id);
}
#endregion
}
}
2、PageHistory.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
namespace Sci
{
public class PageHistory
{
#region 历史数据处理逻辑
/// <summary>
/// 获取history目录
/// </summary>
public static string historyDir()
{
string dirPath = AppDomain.CurrentDomain.BaseDirectory + "history";
//string dir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
//string dirPath = Path.Combine(dir, "history");
if (!Directory.Exists(dirPath)) Directory.CreateDirectory(dirPath);
return dirPath;
}
/// <summary>
/// 根据id保存历史数据
/// </summary>
/// <param name="id"></param>
/// <param name="data"></param>
public static void SaveHistory(string id, string data)
{
string historyPath = Path.Combine(historyDir(), id);
File.WriteAllText(historyPath, data, Encoding.Default);
}
/// <summary>
/// 获取id对应的历史数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static string GetHistory(string id)
{
string historyPath = Path.Combine(historyDir(), id);
if (File.Exists(historyPath))
{
DateTime now = DateTime.Now;
long second = now.AddSeconds(1).Ticks - now.Ticks;
long nowSecond = DateTime.Now.Ticks / 10000000;
long preSecond = new FileInfo(historyPath).LastWriteTime.Ticks / 10000000;
if (nowSecond - preSecond > 60 * 60 * 24 * 7) return ""; // 若当前时间距离上次修改超出7天,则不使用历史数据
else return File.ReadAllText(historyPath, Encoding.Default); // 读取历史数据内容
}
return "";
}
/// <summary>
/// 将data保存至_page对应的历史数据中
/// </summary>
/// <param name="_page"></param>
/// <param name="data"></param>
public static void SaveHistory(Page _page, string data)
{
string url = _page.Request.Url.ToString();
string id = MD5.Encrypt(url); // 每个页面url对应一个唯一的id
SaveHistory(id, data);
}
/// <summary>
/// 获取_page对应的历史数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public static string GetHistory(Page _page)
{
string url = _page.Request.Url.ToString();
string id = MD5.Encrypt(url); // 每个页面url对应一个唯一的id
return GetHistory(id);
}
#endregion
}
}