我需要捕获apsx表单的缩略图,我可以使用下面的代码。但问题是我有大约12000个表单要捕获,我的代码运行大约2分钟,捕获1500个缩略图并停止执行没有给出任何异常或错误。 请帮忙。
private Bitmap GenerateThumbnail(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
{
this.Url = Url;
this.BrowserWidth = BrowserWidth;
this.BrowserHeight = BrowserHeight;
this.Height = ThumbnailHeight;
this.Width = ThumbnailWidth;
Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
thread.Join();
return ThumbnailImage;
}
private void GenerateThumbnailInteral()
{
System.Windows.Forms.WebBrowser webBrowser = new System.Windows.Forms.WebBrowser();
webBrowser.ScrollBarsEnabled = false;
webBrowser.Navigate(this.Url);
webBrowser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
while (webBrowser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
{
System.Windows.Forms.Application.DoEvents();
}
webBrowser.Dispose();
}
private void WebBrowser_DocumentCompleted(object sender, System.Windows.Forms.WebBrowserDocumentCompletedEventArgs e)
{
System.Windows.Forms.WebBrowser webBrowser = (System.Windows.Forms.WebBrowser)sender;
webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
webBrowser.ScrollBarsEnabled = false;
this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
webBrowser.BringToFront();
webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds);
this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
this.ThumbnailImageSmall = (Bitmap)ThumbnailImage.GetThumbnailImage(200, 200, null, IntPtr.Zero);
}
private void SaveImageThumbnail(List<FormClass> objFormClass)
{
try
{
string url = string.Empty;
string folderName = string.Empty;
foreach (FormClass form in objFormClass)//12000 forms
{
InternetSetCookie(url, authCookieName, authCookieValue);
InternetSetCookie(url, "RP_si", rsiCookieValue);
filepath = "D:\\Previews\\";
folderName = form.FormId.ToString();
url = string.Format("http://localhost/CRM/Sockets/{0}", form.FileName);
if (!Directory.Exists(filepath + folderName))
{
Directory.CreateDirectory(filepath + folderName);
}
Bitmap image = GenerateThumbnail(url, 1024, 700, 600, 500);
image.Save(string.Format("{0}{1}/{2}-medium.jpg", filepath, folderName, form.FormId), System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
catch (Exception exp)
{
Logger.Error("Unhandled Exception", exp);
}
}
public bool SaveThumbnails(string filePath, string authCookieName, string authCookieValue, string rpsiCookieValue)
{
this.authCookieName = authCookieName;
this.authCookieValue = authCookieValue;
this.rsiCookieValue = rpsiCookieValue;
this.filepath = filePath;
try
{
List<FormClass> globalForms = formRepo.GetForms().ToList();//Returns 12000 forms from database
SaveImageThumbnail(globalForms.ToList());
return true;
}
catch (Exception ex)
{
Logger.Error(ex.Message, ex);
}
}
从控制器我这样称呼这个方法。
public void SaveThumbnailImage(string Guid)
{
try
{
var formsService = SvcUnityContainer.DefaultContainer.WithStandardMappings().Resolve<IFormsService>();
new Thread(() => formsService.SaveThumbnails("D:\\Previews\\", FormsAuthentication.FormsCookieName, HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName].Value, HttpContext.Request.Cookies["RP_si"].Value)).Start();
}
catch (Exception exp)
{
Logger.Error(exp.Message, exp);
}
}
答案 0 :(得分:1)
600 x 500 x 1500 = 450000000像素,即1350兆字节的像素数据(每像素3个字节)。
流程耗尽内存。
处置位图至关重要。一旦声明了所有内存并且垃圾收集器启动以释放它,您将遇到延迟,但该过程应该在稍后继续。
改变这个:
Bitmap image = GenerateThumbnail(url, 1024, 700, 600, 500);
image.Save(
string.Format("{0}{1}/{2}-medium.jpg", filepath, folderName, form.FormId),
System.Drawing.Imaging.ImageFormat.Jpeg);
到此:
using(Bitmap image = GenerateThumbnail(url, 1024, 700, 600, 500))
{
image.Save(
string.Format("{0}{1}/{2}-medium.jpg", filepath, folderName, form.FormId),
System.Drawing.Imaging.ImageFormat.Jpeg);
}
旁注:您可能需要考虑重新使用相同的Web浏览器,这可能有助于加快程序的速度。如果可以(干净地)重复使用它,连续1500次实例化和处理Web浏览器可能会浪费时间。