mirror of
https://github.com/greenshot/greenshot
synced 2025-07-14 00:53:51 -07:00
Removed unneeded scroll code. Fixed a potential null-pointer bug in CaptureRectangle. Fixed a bug with GDI captures being black by counting the pixels and if there is a difference taking the screen shot instead.
git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@2238 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
parent
fd46837c8d
commit
35b16e8fd9
4 changed files with 48 additions and 110 deletions
|
@ -762,6 +762,7 @@ namespace Greenshot.Helpers {
|
|||
|
||||
LOG.InfoFormat("Capturing window with mode {0}", windowCaptureMode);
|
||||
bool captureTaken = false;
|
||||
windowRectangle.Intersect(captureForWindow.ScreenBounds);
|
||||
// Try to capture
|
||||
while (!captureTaken) {
|
||||
ICapture tmpCapture = null;
|
||||
|
@ -771,8 +772,49 @@ namespace Greenshot.Helpers {
|
|||
if (windowToCapture.Iconic) {
|
||||
// Restore the window making sure it's visible!
|
||||
windowToCapture.Restore();
|
||||
} else {
|
||||
windowToCapture.ToForeground();
|
||||
}
|
||||
tmpCapture = windowToCapture.CaptureGDIWindow(captureForWindow);
|
||||
if (tmpCapture != null) {
|
||||
// check if GDI capture any good, by comparing it with the screen content
|
||||
int blackCountGDI = ImageHelper.CountColor((Bitmap)tmpCapture.Image, Color.Black, false);
|
||||
int GDIPixels = tmpCapture.Image.Width * tmpCapture.Image.Height;
|
||||
int blackPercentageGDI = (blackCountGDI * 100) / GDIPixels;
|
||||
if (blackPercentageGDI >= 1) {
|
||||
int screenPixels = windowRectangle.Width * windowRectangle.Height;
|
||||
using (ICapture screenCapture = new Capture()) {
|
||||
screenCapture.CaptureDetails = captureForWindow.CaptureDetails;
|
||||
if (WindowCapture.CaptureRectangle(screenCapture, windowRectangle) != null) {
|
||||
int blackCountScreen = ImageHelper.CountColor((Bitmap)screenCapture.Image, Color.Black, false);
|
||||
int blackPercentageScreen = (blackCountScreen * 100) / screenPixels;
|
||||
if (screenPixels == GDIPixels) {
|
||||
// "easy compare", both have the same size
|
||||
// If GDI has more black, use the screen capture.
|
||||
if (blackPercentageGDI > blackPercentageScreen) {
|
||||
LOG.Debug("Using screen capture, as GDI had additional black.");
|
||||
// changeing the image will automatically dispose the previous
|
||||
tmpCapture.Image = screenCapture.Image;
|
||||
// Make sure it's not disposed, else the picture is gone!
|
||||
screenCapture.NullImage();
|
||||
}
|
||||
} else if (screenPixels < GDIPixels) {
|
||||
// Screen capture is cropped, window is outside of screen
|
||||
if (blackPercentageGDI > 50 && blackPercentageGDI > blackPercentageScreen) {
|
||||
LOG.Debug("Using screen capture, as GDI had additional black.");
|
||||
// changeing the image will automatically dispose the previous
|
||||
tmpCapture.Image = screenCapture.Image;
|
||||
// Make sure it's not disposed, else the picture is gone!
|
||||
screenCapture.NullImage();
|
||||
}
|
||||
} else {
|
||||
// Use the GDI capture by doing nothing
|
||||
LOG.Debug("This should not happen, how can there be more screen as GDI pixels?");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tmpCapture != null) {
|
||||
captureForWindow = tmpCapture;
|
||||
|
@ -803,7 +845,7 @@ namespace Greenshot.Helpers {
|
|||
} else {
|
||||
windowToCapture.ToForeground();
|
||||
}
|
||||
windowRectangle.Intersect(captureForWindow.ScreenBounds);
|
||||
|
||||
try {
|
||||
captureForWindow = WindowCapture.CaptureRectangle(captureForWindow, windowRectangle);
|
||||
captureTaken = true;
|
||||
|
|
|
@ -587,7 +587,9 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
capture.Image = CaptureRectangle(captureBounds);
|
||||
capture.Location = captureBounds.Location;
|
||||
if (capture.CaptureDetails != null) {
|
||||
((Bitmap)capture.Image).SetResolution(capture.CaptureDetails.DpiX, capture.CaptureDetails.DpiY);
|
||||
}
|
||||
if (capture.Image == null) {
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -1111,114 +1111,6 @@ namespace GreenshotPlugin.Core {
|
|||
return returnRegion;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// PrintWindow but with VScroll loop
|
||||
/// </summary>
|
||||
/// <returns>Image with all the scroll contents</returns>
|
||||
public Image PrintWithVScroll() {
|
||||
SCROLLINFO scrollinfo = new SCROLLINFO();
|
||||
scrollinfo.cbSize = Marshal.SizeOf( scrollinfo );
|
||||
scrollinfo.fMask = (int) ScrollInfoMask.SIF_ALL;
|
||||
User32.GetScrollInfo(Handle, (int) ScrollBarDirection.SB_VERT, ref scrollinfo );
|
||||
|
||||
LOG.DebugFormat("nMax {0}", scrollinfo.nMax);
|
||||
LOG.DebugFormat("nMin {0}", scrollinfo.nMin);
|
||||
LOG.DebugFormat("nPage {0}", scrollinfo.nPage);
|
||||
LOG.DebugFormat("nPos {0}", scrollinfo.nPos);
|
||||
LOG.DebugFormat("nTrackPos {0}", scrollinfo.nTrackPos);
|
||||
|
||||
// WindowStyleFlags style = WindowStyle;
|
||||
// LOG.InfoFormat("before: {0}", style);
|
||||
// style = style.Remove(WindowStyleFlags.WS_HSCROLL);
|
||||
// WindowStyle = style;
|
||||
// if (!User32.SetWindowPos(Handle, IntPtr.Zero, 0,0,0,0, WindowPos.SWP_NOMOVE | WindowPos.SWP_NOSIZE | WindowPos.SWP_NOZORDER | WindowPos.SWP_FRAMECHANGED| WindowPos.SWP_DRAWFRAME | WindowPos.SWP_NOACTIVATE)) {
|
||||
// Exception e = new Win32Exception();
|
||||
// LOG.Error("error with SetWindowPos", e);
|
||||
// }
|
||||
// style = WindowStyle;
|
||||
// LOG.InfoFormat("before: {0}", style);
|
||||
// style = style.Remove(WindowStyleFlags.WS_VSCROLL);
|
||||
// WindowStyle = style;
|
||||
// LOG.InfoFormat("After: {0}", style);
|
||||
// if (!User32.SetWindowPos(Handle, IntPtr.Zero, 0,0,0,0, WindowPos.SWP_NOMOVE | WindowPos.SWP_NOSIZE | WindowPos.SWP_NOZORDER | WindowPos.SWP_FRAMECHANGED| WindowPos.SWP_DRAWFRAME | WindowPos.SWP_NOACTIVATE)) {
|
||||
// Exception e = new Win32Exception();
|
||||
// LOG.Error("error with SetWindowPos", e);
|
||||
// }
|
||||
|
||||
Rectangle viewableRectangle = ClientRectangle;
|
||||
// As we work with bitmap coordinates, recalculate the location
|
||||
viewableRectangle.Offset(-WindowRectangle.X, -WindowRectangle.Y);
|
||||
|
||||
int width = viewableRectangle.Width;
|
||||
int height = viewableRectangle.Height * (scrollinfo.nMax / scrollinfo.nPage);
|
||||
int pageSkip = scrollinfo.nPage;
|
||||
if (width * height == 0) {
|
||||
LOG.Error("No window content!!");
|
||||
return null;
|
||||
}
|
||||
Bitmap returnBitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb);
|
||||
|
||||
using (Graphics graphicsTarget = Graphics.FromImage(returnBitmap)) {
|
||||
int location = 0;
|
||||
int y = 0;
|
||||
do {
|
||||
VScroll(location);
|
||||
Thread.Sleep(200);
|
||||
|
||||
Rectangle destination = new Rectangle(0, y, viewableRectangle.Width, viewableRectangle.Height);
|
||||
using (Image printedWindow = PrintWindow()) {
|
||||
if (printedWindow != null) {
|
||||
graphicsTarget.DrawImage(printedWindow, destination, viewableRectangle, GraphicsUnit.Pixel);
|
||||
graphicsTarget.Flush();
|
||||
y += viewableRectangle.Height;
|
||||
}
|
||||
}
|
||||
location += pageSkip;
|
||||
//if (location < scrollinfo.nMax && location > scrollinfo.nMax - scrollinfo.nPage) {
|
||||
// location = scrollinfo.nMax - scrollinfo.nPage;
|
||||
//}
|
||||
} while (location < scrollinfo.nMax);
|
||||
}
|
||||
VScroll(scrollinfo.nPos);
|
||||
return returnBitmap;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does the window have a HScrollbar?
|
||||
/// </summary>
|
||||
/// <returns>true if a HScrollbar is available</returns>
|
||||
public bool HasHScroll() {
|
||||
return WindowStyle.Has(WindowStyleFlags.WS_HSCROLL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Does the window have a VScrollbar?
|
||||
/// </summary>
|
||||
/// <returns>true if a VScrollbar is available</returns>
|
||||
public bool HasVScroll() {
|
||||
return WindowStyle.Has(WindowStyleFlags.WS_VSCROLL);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scroll the window to the hpos location
|
||||
/// </summary>
|
||||
/// <param name="hpos"></param>
|
||||
public void HScroll(int hpos) {
|
||||
User32.SetScrollPos(Handle, Orientation.Horizontal, hpos, true);
|
||||
User32.PostMessage(Handle, (int)WindowsMessages.WM_HSCROLL, (int)ScrollbarCommand.SB_THUMBPOSITION + (hpos<<16), 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Scroll the window to the vpos location
|
||||
/// </summary>
|
||||
/// <param name="vpos"></param>
|
||||
public void VScroll(int vpos) {
|
||||
User32.SetScrollPos(Handle, Orientation.Vertical, vpos, true);
|
||||
int ptrWparam = (int)ScrollbarCommand.SB_THUMBTRACK + (vpos << 16);
|
||||
int ptrLparam = 0;
|
||||
User32.SendMessage(Handle, (int)WindowsMessages.WM_VSCROLL, ptrWparam, ptrLparam);
|
||||
}
|
||||
|
||||
private bool CanFreezeOrUnfreeze(string titleOrProcessname) {
|
||||
if (string.IsNullOrEmpty(titleOrProcessname)) {
|
||||
return false;
|
||||
|
|
|
@ -114,6 +114,8 @@ namespace Greenshot.Plugin {
|
|||
set;
|
||||
}
|
||||
|
||||
void NullImage();
|
||||
|
||||
Rectangle ScreenBounds {
|
||||
get;
|
||||
set;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue