mirror of
https://github.com/ShareX/ShareX.git
synced 2024-10-01 09:47:22 +13:00
Workaround for DrawToBitmap issue
This commit is contained in:
parent
81e7aa24d5
commit
370c758b12
2 changed files with 58 additions and 20 deletions
|
@ -591,4 +591,12 @@ public struct HARDWAREINPUT
|
|||
public short wParamL;
|
||||
public short wParamH;
|
||||
}
|
||||
|
||||
[ComImport]
|
||||
[Guid("0000010D-0000-0000-C000-000000000046")]
|
||||
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
|
||||
public interface IViewObject
|
||||
{
|
||||
void Draw([MarshalAs(UnmanagedType.U4)] uint dwAspect, int lindex, IntPtr pvAspect, [In] IntPtr ptd, IntPtr hdcTargetDev, IntPtr hdcDraw, [MarshalAs(UnmanagedType.Struct)] ref RECT lprcBounds, [In] IntPtr lprcWBounds, IntPtr pfnContinue, [MarshalAs(UnmanagedType.U4)] uint dwContinue);
|
||||
}
|
||||
}
|
|
@ -45,6 +45,9 @@ public class WebpageCapture : IDisposable
|
|||
public WebpageCapture()
|
||||
{
|
||||
webBrowser = new WebBrowser();
|
||||
webBrowser.AllowNavigation = true;
|
||||
webBrowser.ScriptErrorsSuppressed = true;
|
||||
webBrowser.ScrollBarsEnabled = false;
|
||||
webBrowser.DocumentCompleted += webBrowser_DocumentCompleted;
|
||||
}
|
||||
|
||||
|
@ -57,8 +60,6 @@ public void CapturePage(string url, Size browserSize)
|
|||
{
|
||||
if (!string.IsNullOrEmpty(url))
|
||||
{
|
||||
webBrowser.ScriptErrorsSuppressed = true;
|
||||
webBrowser.ScrollBarsEnabled = false;
|
||||
webBrowser.Size = browserSize;
|
||||
webBrowser.Navigate(url);
|
||||
}
|
||||
|
@ -66,33 +67,62 @@ public void CapturePage(string url, Size browserSize)
|
|||
|
||||
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
|
||||
{
|
||||
if (webBrowser.ReadyState == WebBrowserReadyState.Complete)
|
||||
{
|
||||
TaskEx.RunDelayed(GetWebpageBitmap, CaptureDelay);
|
||||
}
|
||||
TaskEx.RunDelayed(GetWebpageBitmap, CaptureDelay);
|
||||
}
|
||||
|
||||
private void GetWebpageBitmap()
|
||||
{
|
||||
Rectangle rect = webBrowser.Document.Body.ScrollRectangle;
|
||||
webBrowser.Size = new Size(rect.Width, rect.Height);
|
||||
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
|
||||
|
||||
try
|
||||
if (webBrowser.Document != null && webBrowser.Document.Body != null)
|
||||
{
|
||||
webBrowser.DrawToBitmap(bmp, rect);
|
||||
OnCaptureCompleted(bmp);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DebugHelper.WriteException(ex);
|
||||
Rectangle rect = webBrowser.Document.Body.ScrollRectangle;
|
||||
webBrowser.Size = new Size(rect.Width, rect.Height);
|
||||
Bitmap bmp = new Bitmap(rect.Width, rect.Height);
|
||||
|
||||
if (bmp != null)
|
||||
try
|
||||
{
|
||||
bmp.Dispose();
|
||||
GetImage(webBrowser.ActiveXInstance, bmp, Color.White);
|
||||
OnCaptureCompleted(bmp);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
DebugHelper.WriteException(ex);
|
||||
|
||||
OnCaptureCompleted(null);
|
||||
if (bmp != null)
|
||||
{
|
||||
bmp.Dispose();
|
||||
}
|
||||
|
||||
OnCaptureCompleted(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static void GetImage(object obj, Image destination, Color backgroundColor)
|
||||
{
|
||||
using (Graphics graphics = Graphics.FromImage(destination))
|
||||
{
|
||||
IntPtr deviceContextHandle = IntPtr.Zero;
|
||||
RECT rectangle = new RECT();
|
||||
|
||||
rectangle.Right = destination.Width;
|
||||
rectangle.Bottom = destination.Height;
|
||||
|
||||
graphics.Clear(backgroundColor);
|
||||
|
||||
try
|
||||
{
|
||||
deviceContextHandle = graphics.GetHdc();
|
||||
|
||||
IViewObject viewObject = obj as IViewObject;
|
||||
viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rectangle, IntPtr.Zero, IntPtr.Zero, 0);
|
||||
}
|
||||
finally
|
||||
{
|
||||
if (deviceContextHandle != IntPtr.Zero)
|
||||
{
|
||||
graphics.ReleaseHdc(deviceContextHandle);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue