110,537
社区成员
发帖
与我相关
我的任务
分享
screenfrag = Image.FromHbitmap(bm.GetHbitmap());
修改后代码
引用gid32.dll 中的Deleteobject(IntPtr hObject)
Intptr iptr = bm.GetHbitmap();
screenfrag = Image.FromHbitmap(iptr);
Deleteobject(iptr);
所以,以后一定不要用方法的返回值来做参数,产生泄露非常有隐蔽性。
谢谢各位了!也请给我更多的指导
public void GetPageImage(string path)
{
//Test Is OK
if (true)
{
bool flag = firstScreen;
//m_browser.Navigate(url);
////浏览器Dom载入完毕
//while (m_browser.ReadyState != WebBrowserReadyState.Complete)
//{
// Application.DoEvents();
//}
mshtml.IHTMLDocument2 myDoc = (mshtml.IHTMLDocument2)m_browser.Document.DomDocument;
//处理由于webbrowser上、左边框阴影带来的拼接bug
int URLExtraHeight = 0;
int URLExtraLeft = 0;
if ((myDoc as HTMLDocumentClass).body == null)
{
//写日志
EtlDataProcess.InsertMailErrLog("图片保存失败","【"+path+"】图片保存失败,可能是因为格式加载过长导致。");
return;
}
(myDoc as HTMLDocumentClass).body.setAttribute("scroll", "yes", 0);
//document完整高度
int heightsize = (int)(myDoc as HTMLDocumentClass).body.getAttribute("scrollHeight", 0);
int widthsize = (int)(myDoc as HTMLDocumentClass).body.getAttribute("scrollWidth", 0);
////Get Screen Height
int screenHeight = (int)(myDoc as HTMLDocumentClass).body.getAttribute("clientHeight", 0);
int screenWidth = (int)(myDoc as HTMLDocumentClass).body.getAttribute("clientWidth", 0);
IntPtr myIntptr = (IntPtr)m_browser.Handle;
//寻找IE对象句柄
IntPtr wbHandle = FindWindowEx(myIntptr, IntPtr.Zero, "Shell Embedding", null);
wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Shell DocObject View", null);
wbHandle = FindWindowEx(wbHandle, IntPtr.Zero, "Internet Explorer_Server", null);
IntPtr hwnd = wbHandle;
Bitmap bm = new Bitmap(screenWidth, screenHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Bitmap bm2 = new Bitmap(widthsize, heightsize, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics g = null;
Graphics g2 = Graphics.FromImage(bm2);
//切割用的临时对象
Bitmap tempbm = null;
Graphics tempg = null;
IntPtr hdc;
Image screenfrag = null;
Image firstScreenfrag = null;
#region 拼接
int brwTop = 0;
int brwLeft = 0;
int myPage = 0;
//Get Screen Height (for bottom up screen drawing)
while ((myPage * screenHeight) < heightsize)
{
(myDoc as HTMLDocumentClass).body.setAttribute("scrollTop", (screenHeight - 5) * myPage, 0);
++myPage;
}
//Rollback the page count by one
--myPage;
//*************************************************************************
int myPageWidth = 0;
//screenWidth+ URLExtraLeft
while ((myPageWidth * screenWidth) < widthsize)
{
(myDoc as HTMLDocumentClass).body.setAttribute("scrollLeft", (screenWidth - 5) * myPageWidth, 0);
brwLeft = (int)(myDoc as HTMLDocumentClass).body.getAttribute("scrollLeft", 0);
for (int i = myPage; i >= 0; --i)
{
//Shoot visible window
g = Graphics.FromImage(bm);
hdc = g.GetHdc();
(myDoc as HTMLDocumentClass).body.setAttribute("scrollTop", (screenHeight - 5) * i, 0);
brwTop = (int)(myDoc as HTMLDocumentClass).body.getAttribute("scrollTop", 0);
PrintWindow(hwnd, hdc, 0);
g.ReleaseHdc(hdc);
//g.ReleaseHdc();
//g.Flush();
screenfrag = Image.FromHbitmap(bm.GetHbitmap());
//切除URLExtraLeft、URLExtraHeight长度的webbrowser带来的bug
tempbm = new Bitmap(screenWidth - URLExtraLeft, screenHeight - URLExtraHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
tempg = Graphics.FromImage(tempbm);
tempg.DrawImage(screenfrag, -URLExtraLeft, -URLExtraHeight);
//拼接到g2
g2.DrawImage(tempbm, brwLeft + URLExtraLeft, brwTop + URLExtraLeft);
//tempg.ReleaseHdc();
tempg.Dispose();
tempg = null;
tempbm.Dispose();
tempbm = null;
screenfrag.Dispose();
screenfrag = null;
g.Dispose();
g = null;
}
//g2.ReleaseHdc();
//*************************************************************************
//是否得到第一屏
//if (flag)
//{
// firstScreenfrag = (Image)tempbm.Clone();
// flag = false;
//}
++myPageWidth;
}
g2.Dispose();
int finalWidth = (int)widthsize;
int finalHeight = (int)heightsize;
Bitmap finalImage = new Bitmap(finalWidth, finalHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics gFinal = Graphics.FromImage((Image)finalImage);
gFinal.DrawImage(bm2, 0, 0, finalWidth, finalHeight);
#endregion
//Get Time Stamp
DateTime myTime = DateTime.Now;
String format = "yyyy_MM_dd_hh_mm_ss";
//Create Directory to save image to.
//Directory.CreateDirectory(path);
//Write Image.
EncoderParameters eps = new EncoderParameters(1);
long myQuality = Convert.ToInt64(100);
eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, myQuality);
ImageCodecInfo ici = GetEncoderInfo("image/jpeg");
try
{
//保存
finalImage.Save(path + "", ici, eps);
}
catch
{
//写日志
EtlDataProcess.InsertMailErrLog("图片保存失败", "【" + path + "】图片保存失败,可能是因为图片取得错误。");
}
finally
{
#region Clean
//Clean Up.
if(myDoc!=null)
myDoc.close();
myDoc = null;
if(g!=null)
g.Dispose();
g = null;
if(g2!=null)
g2.Dispose();
g2 = null;
if (gFinal != null)
gFinal.Dispose();
gFinal = null;
if(bm!=null)
bm.Dispose();
bm = null;
if(bm2!=null)
bm2.Dispose();
bm2 = null;
if (finalImage != null)
finalImage.Dispose();
finalImage = null;
if(tempbm!=null)
tempbm.Dispose();
tempbm = null;
if (tempg != null)
tempg.Dispose();
tempg = null;
screenfrag = null;
#endregion
}
//保存第一屏
//if (firstScreen)
//{
// firstScreenfrag.Save(path + @"\Img_FirstScreen_" + myTime.ToString(format) + ".jpg", ici, eps);
// firstScreenfrag.Dispose();
//}
//Process.Start("explorer.exe", path);
}
}
Graphics g2 = Graphics.FromImage(bm2);
int finalWidth = (int)widthsize;
int finalHeight = (int)heightsize;
Bitmap finalImage = new Bitmap(finalWidth, finalHeight, System.Drawing.Imaging.PixelFormat.Format16bppRgb555);
Graphics gFinal = Graphics.FromImage((Image)finalImage);
gFinal.DrawImage(bm2, 0, 0, finalWidth, finalHeight);
finalImage.Save(path + "", ici, eps);