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:
RKrom 2012-11-05 16:05:07 +00:00
parent fd46837c8d
commit 35b16e8fd9
4 changed files with 48 additions and 110 deletions

View file

@ -762,6 +762,7 @@ namespace Greenshot.Helpers {
LOG.InfoFormat("Capturing window with mode {0}", windowCaptureMode); LOG.InfoFormat("Capturing window with mode {0}", windowCaptureMode);
bool captureTaken = false; bool captureTaken = false;
windowRectangle.Intersect(captureForWindow.ScreenBounds);
// Try to capture // Try to capture
while (!captureTaken) { while (!captureTaken) {
ICapture tmpCapture = null; ICapture tmpCapture = null;
@ -771,8 +772,49 @@ namespace Greenshot.Helpers {
if (windowToCapture.Iconic) { if (windowToCapture.Iconic) {
// Restore the window making sure it's visible! // Restore the window making sure it's visible!
windowToCapture.Restore(); windowToCapture.Restore();
} else {
windowToCapture.ToForeground();
} }
tmpCapture = windowToCapture.CaptureGDIWindow(captureForWindow); 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) { if (tmpCapture != null) {
captureForWindow = tmpCapture; captureForWindow = tmpCapture;
@ -803,7 +845,7 @@ namespace Greenshot.Helpers {
} else { } else {
windowToCapture.ToForeground(); windowToCapture.ToForeground();
} }
windowRectangle.Intersect(captureForWindow.ScreenBounds);
try { try {
captureForWindow = WindowCapture.CaptureRectangle(captureForWindow, windowRectangle); captureForWindow = WindowCapture.CaptureRectangle(captureForWindow, windowRectangle);
captureTaken = true; captureTaken = true;

View file

@ -587,7 +587,9 @@ namespace GreenshotPlugin.Core {
} }
capture.Image = CaptureRectangle(captureBounds); capture.Image = CaptureRectangle(captureBounds);
capture.Location = captureBounds.Location; capture.Location = captureBounds.Location;
((Bitmap)capture.Image).SetResolution(capture.CaptureDetails.DpiX, capture.CaptureDetails.DpiY); if (capture.CaptureDetails != null) {
((Bitmap)capture.Image).SetResolution(capture.CaptureDetails.DpiX, capture.CaptureDetails.DpiY);
}
if (capture.Image == null) { if (capture.Image == null) {
return null; return null;
} }

View file

@ -1111,114 +1111,6 @@ namespace GreenshotPlugin.Core {
return returnRegion; 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) { private bool CanFreezeOrUnfreeze(string titleOrProcessname) {
if (string.IsNullOrEmpty(titleOrProcessname)) { if (string.IsNullOrEmpty(titleOrProcessname)) {
return false; return false;

View file

@ -113,6 +113,8 @@ namespace Greenshot.Plugin {
get; get;
set; set;
} }
void NullImage();
Rectangle ScreenBounds { Rectangle ScreenBounds {
get; get;