mirror of
https://github.com/greenshot/greenshot
synced 2025-08-20 05:23:24 -07:00
This commit allows us to experiment with different screen capture methods via plugins. e.g. a Desktop-Duplication API implementation could be supplied for Windows 8, which might capture DirectX better.
This commit is contained in:
parent
2797c076b4
commit
fc0791bdb6
6 changed files with 242 additions and 194 deletions
|
@ -37,43 +37,31 @@ namespace GreenshotPlugin.Core {
|
|||
/// The time the Capture was taken and the Title of the window (or a region of) that is captured
|
||||
/// </summary>
|
||||
public class CaptureDetails : ICaptureDetails {
|
||||
private string title;
|
||||
public string Title {
|
||||
get {return title;}
|
||||
set {title = value;}
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private string filename;
|
||||
public string Filename {
|
||||
get {return filename;}
|
||||
set {filename = value;}
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private DateTime dateTime;
|
||||
public DateTime DateTime {
|
||||
get {return dateTime;}
|
||||
set {dateTime = value;}
|
||||
}
|
||||
|
||||
private float dpiX;
|
||||
public float DpiX {
|
||||
get {
|
||||
return dpiX;
|
||||
}
|
||||
set {
|
||||
dpiX = value;
|
||||
}
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private float dpiY;
|
||||
public float DpiY {
|
||||
get {
|
||||
return dpiY;
|
||||
}
|
||||
set {
|
||||
dpiY = value;
|
||||
}
|
||||
public float DpiX {
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
public float DpiY {
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private Dictionary<string, string> metaData = new Dictionary<string, string>();
|
||||
public Dictionary<string, string> MetaData {
|
||||
get {return metaData;}
|
||||
|
@ -86,37 +74,36 @@ namespace GreenshotPlugin.Core {
|
|||
metaData.Add(key, value);
|
||||
}
|
||||
}
|
||||
|
||||
private CaptureMode captureMode;
|
||||
|
||||
public CaptureMode CaptureMode {
|
||||
get {return captureMode;}
|
||||
set {captureMode = value;}
|
||||
get;
|
||||
set;
|
||||
}
|
||||
|
||||
private List<IDestination> captureDestinations = new List<IDestination>();
|
||||
|
||||
private List<IDestination> _captureDestinations = new List<IDestination>();
|
||||
public List<IDestination> CaptureDestinations {
|
||||
get {return captureDestinations;}
|
||||
set {captureDestinations = value;}
|
||||
get {return _captureDestinations;}
|
||||
set {_captureDestinations = value;}
|
||||
}
|
||||
|
||||
public void ClearDestinations() {
|
||||
captureDestinations.Clear();
|
||||
_captureDestinations.Clear();
|
||||
}
|
||||
|
||||
public void RemoveDestination(IDestination destination) {
|
||||
if (captureDestinations.Contains(destination)) {
|
||||
captureDestinations.Remove(destination);
|
||||
if (_captureDestinations.Contains(destination)) {
|
||||
_captureDestinations.Remove(destination);
|
||||
}
|
||||
}
|
||||
|
||||
public void AddDestination(IDestination captureDestination) {
|
||||
if (!captureDestinations.Contains(captureDestination)) {
|
||||
captureDestinations.Add(captureDestination);
|
||||
if (!_captureDestinations.Contains(captureDestination)) {
|
||||
_captureDestinations.Add(captureDestination);
|
||||
}
|
||||
}
|
||||
|
||||
public bool HasDestination(string designation) {
|
||||
foreach(IDestination destination in captureDestinations) {
|
||||
foreach(IDestination destination in _captureDestinations) {
|
||||
if (designation.Equals(destination.Designation)) {
|
||||
return true;
|
||||
}
|
||||
|
@ -125,7 +112,7 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
|
||||
public CaptureDetails() {
|
||||
dateTime = DateTime.Now;
|
||||
DateTime = DateTime.Now;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -133,47 +120,47 @@ namespace GreenshotPlugin.Core {
|
|||
/// This class is used to pass an instance of the "Capture" around
|
||||
/// Having the Bitmap, eventually the Windows Title and cursor all together.
|
||||
/// </summary>
|
||||
public class Capture : IDisposable, ICapture {
|
||||
private static ILog LOG = LogManager.GetLogger(typeof(Capture));
|
||||
private List<ICaptureElement> elements = new List<ICaptureElement>();
|
||||
public class Capture : ICapture {
|
||||
private static readonly ILog LOG = LogManager.GetLogger(typeof(Capture));
|
||||
private List<ICaptureElement> _elements = new List<ICaptureElement>();
|
||||
|
||||
private Rectangle screenBounds;
|
||||
private Rectangle _screenBounds = Rectangle.Empty;
|
||||
/// <summary>
|
||||
/// Get/Set the Screenbounds
|
||||
/// </summary>
|
||||
public Rectangle ScreenBounds {
|
||||
get {
|
||||
if (screenBounds == null) {
|
||||
screenBounds = WindowCapture.GetScreenBounds();
|
||||
if (_screenBounds == Rectangle.Empty) {
|
||||
_screenBounds = WindowCapture.GetScreenBounds();
|
||||
}
|
||||
return screenBounds;
|
||||
return _screenBounds;
|
||||
}
|
||||
set {screenBounds = value;}
|
||||
set {_screenBounds = value;}
|
||||
}
|
||||
|
||||
private Image image;
|
||||
private Image _image;
|
||||
/// <summary>
|
||||
/// Get/Set the Image
|
||||
/// </summary>
|
||||
public Image Image {
|
||||
get {return image;}
|
||||
get {return _image;}
|
||||
set {
|
||||
if (image != null) {
|
||||
image.Dispose();
|
||||
if (_image != null) {
|
||||
_image.Dispose();
|
||||
}
|
||||
image = value;
|
||||
_image = value;
|
||||
if (value != null) {
|
||||
if (value.PixelFormat.Equals(PixelFormat.Format8bppIndexed) || value.PixelFormat.Equals(PixelFormat.Format1bppIndexed) || value.PixelFormat.Equals(PixelFormat.Format4bppIndexed)) {
|
||||
LOG.Debug("Converting Bitmap to PixelFormat.Format32bppArgb as we don't support: " + value.PixelFormat);
|
||||
try {
|
||||
// Default Bitmap PixelFormat is Format32bppArgb
|
||||
image = new Bitmap(value);
|
||||
_image = new Bitmap(value);
|
||||
} finally {
|
||||
// Always dispose, even when a exception occured
|
||||
value.Dispose();
|
||||
}
|
||||
}
|
||||
LOG.DebugFormat("Image is set with the following specifications: {0} - {1}", image.Size, image.PixelFormat);
|
||||
LOG.DebugFormat("Image is set with the following specifications: {0} - {1}", _image.Size, _image.PixelFormat);
|
||||
} else {
|
||||
LOG.Debug("Image is removed.");
|
||||
}
|
||||
|
@ -181,65 +168,65 @@ namespace GreenshotPlugin.Core {
|
|||
}
|
||||
|
||||
public void NullImage() {
|
||||
image = null;
|
||||
_image = null;
|
||||
}
|
||||
|
||||
private Icon cursor;
|
||||
private Icon _cursor;
|
||||
/// <summary>
|
||||
/// Get/Set the image for the Cursor
|
||||
/// </summary>
|
||||
public Icon Cursor {
|
||||
get {return cursor;}
|
||||
get {return _cursor;}
|
||||
set {
|
||||
if (cursor != null) {
|
||||
cursor.Dispose();
|
||||
if (_cursor != null) {
|
||||
_cursor.Dispose();
|
||||
}
|
||||
cursor = (Icon)value.Clone();
|
||||
_cursor = (Icon)value.Clone();
|
||||
}
|
||||
}
|
||||
|
||||
private bool cursorVisible = false;
|
||||
private bool _cursorVisible;
|
||||
/// <summary>
|
||||
/// Set if the cursor is visible
|
||||
/// </summary>
|
||||
public bool CursorVisible {
|
||||
get {return cursorVisible;}
|
||||
set {cursorVisible = value;}
|
||||
get {return _cursorVisible;}
|
||||
set {_cursorVisible = value;}
|
||||
}
|
||||
|
||||
private Point cursorLocation = Point.Empty;
|
||||
private Point _cursorLocation = Point.Empty;
|
||||
/// <summary>
|
||||
/// Get/Set the CursorLocation
|
||||
/// </summary>
|
||||
public Point CursorLocation {
|
||||
get {return cursorLocation;}
|
||||
set {cursorLocation = value;}
|
||||
get {return _cursorLocation;}
|
||||
set {_cursorLocation = value;}
|
||||
}
|
||||
|
||||
private Point location = Point.Empty;
|
||||
private Point _location = Point.Empty;
|
||||
/// <summary>
|
||||
/// Get/set the Location
|
||||
/// </summary>
|
||||
public Point Location {
|
||||
get {return location;}
|
||||
set {location = value;}
|
||||
get {return _location;}
|
||||
set {_location = value;}
|
||||
}
|
||||
|
||||
private CaptureDetails captureDetails;
|
||||
private CaptureDetails _captureDetails;
|
||||
/// <summary>
|
||||
/// Get/set the CaptureDetails
|
||||
/// </summary>
|
||||
public ICaptureDetails CaptureDetails {
|
||||
get {return captureDetails;}
|
||||
set {captureDetails = (CaptureDetails)value;}
|
||||
get {return _captureDetails;}
|
||||
set {_captureDetails = (CaptureDetails)value;}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Default Constructor
|
||||
/// </summary>
|
||||
public Capture() {
|
||||
screenBounds = WindowCapture.GetScreenBounds();
|
||||
captureDetails = new CaptureDetails();
|
||||
_screenBounds = WindowCapture.GetScreenBounds();
|
||||
_captureDetails = new CaptureDetails();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -274,15 +261,15 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="disposing"></param>
|
||||
protected virtual void Dispose(bool disposing) {
|
||||
if (disposing) {
|
||||
if (image != null) {
|
||||
image.Dispose();
|
||||
if (_image != null) {
|
||||
_image.Dispose();
|
||||
}
|
||||
if (cursor != null) {
|
||||
cursor.Dispose();
|
||||
if (_cursor != null) {
|
||||
_cursor.Dispose();
|
||||
}
|
||||
}
|
||||
image = null;
|
||||
cursor = null;
|
||||
_image = null;
|
||||
_cursor = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -291,8 +278,8 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="cropRectangle">Rectangle with bitmap coordinates</param>
|
||||
public bool Crop(Rectangle cropRectangle) {
|
||||
LOG.Debug("Cropping to: " + cropRectangle.ToString());
|
||||
if (ImageHelper.Crop(ref image, ref cropRectangle)) {
|
||||
location = cropRectangle.Location;
|
||||
if (ImageHelper.Crop(ref _image, ref cropRectangle)) {
|
||||
_location = cropRectangle.Location;
|
||||
// Change mouse location according to the cropRegtangle (including screenbounds) offset
|
||||
MoveMouseLocation(-cropRectangle.Location.X, -cropRectangle.Location.Y);
|
||||
// Move all the elements
|
||||
|
@ -301,12 +288,12 @@ namespace GreenshotPlugin.Core {
|
|||
|
||||
// Remove invisible elements
|
||||
List <ICaptureElement> newElements = new List<ICaptureElement>();
|
||||
foreach(ICaptureElement captureElement in elements) {
|
||||
foreach(ICaptureElement captureElement in _elements) {
|
||||
if (captureElement.Bounds.IntersectsWith(cropRectangle)) {
|
||||
newElements.Add(captureElement);
|
||||
}
|
||||
}
|
||||
elements = newElements;
|
||||
_elements = newElements;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
@ -320,7 +307,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="x">x coordinates to move the mouse</param>
|
||||
/// <param name="y">y coordinates to move the mouse</param>
|
||||
public void MoveMouseLocation(int x, int y) {
|
||||
cursorLocation.Offset(x, y);
|
||||
_cursorLocation.Offset(x, y);
|
||||
}
|
||||
|
||||
// TODO: Enable when the elements are usable again.
|
||||
|
@ -388,13 +375,13 @@ namespace GreenshotPlugin.Core {
|
|||
Bounds = bounds;
|
||||
}
|
||||
|
||||
private List<ICaptureElement> children = new List<ICaptureElement>();
|
||||
private List<ICaptureElement> _children = new List<ICaptureElement>();
|
||||
public List<ICaptureElement> Children {
|
||||
get {
|
||||
return children;
|
||||
return _children;
|
||||
}
|
||||
set {
|
||||
children = value;
|
||||
_children = value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -410,9 +397,9 @@ namespace GreenshotPlugin.Core {
|
|||
// CaptureElements are regarded equal if their bounds are equal. this should be sufficient.
|
||||
public override bool Equals(object obj) {
|
||||
bool ret = false;
|
||||
if (obj != null && GetType().Equals(obj.GetType())) {
|
||||
if (obj != null && GetType() == obj.GetType()) {
|
||||
CaptureElement other = obj as CaptureElement;
|
||||
if (Bounds.Equals(other.Bounds)) {
|
||||
if (other != null && Bounds.Equals(other.Bounds)) {
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
|
@ -428,7 +415,7 @@ namespace GreenshotPlugin.Core {
|
|||
/// </summary>
|
||||
public class WindowCapture {
|
||||
private static readonly ILog LOG = LogManager.GetLogger(typeof(WindowCapture));
|
||||
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
|
||||
private static readonly CoreConfiguration Configuration = IniConfig.GetIniSection<CoreConfiguration>();
|
||||
|
||||
/// <summary>
|
||||
/// Used to cleanup the unmanged resource in the iconInfo for the CaptureCursor method
|
||||
|
@ -461,8 +448,10 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Retrieves the cursor location safely, accounting for DPI settings in Vista/Windows 7. This implementation
|
||||
/// can conveniently be used when the cursor location is needed to deal with a fullscreen bitmap.
|
||||
/// <returns>Point with cursor location, relative to the top left corner of the monitor setup (which itself might
|
||||
/// actually not be on any screen)</returns>
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// Point with cursor location, relative to the top left corner of the monitor setup (which itself might actually not be on any screen)
|
||||
/// </returns>
|
||||
public static Point GetCursorLocationRelativeToScreenBounds() {
|
||||
return GetLocationRelativeToScreenBounds(User32.GetCursorLocation());
|
||||
}
|
||||
|
@ -537,7 +526,6 @@ namespace GreenshotPlugin.Core {
|
|||
/// Helper method to create an exception that might explain what is wrong while capturing
|
||||
/// </summary>
|
||||
/// <param name="method">string with current method</param>
|
||||
/// <param name="capture">ICapture</param>
|
||||
/// <param name="captureBounds">Rectangle of what we want to capture</param>
|
||||
/// <returns></returns>
|
||||
private static Exception CreateCaptureException(string method, Rectangle captureBounds) {
|
||||
|
@ -554,12 +542,12 @@ namespace GreenshotPlugin.Core {
|
|||
/// </summary>
|
||||
/// <param name="process">Process owning the window</param>
|
||||
/// <returns>true if it's allowed</returns>
|
||||
public static bool isDWMAllowed(Process process) {
|
||||
public static bool IsDwmAllowed(Process process) {
|
||||
if (process != null) {
|
||||
if (conf.NoDWMCaptureForProduct != null && conf.NoDWMCaptureForProduct.Count > 0) {
|
||||
if (Configuration.NoDWMCaptureForProduct != null && Configuration.NoDWMCaptureForProduct.Count > 0) {
|
||||
try {
|
||||
string productName = process.MainModule.FileVersionInfo.ProductName;
|
||||
if (productName != null && conf.NoDWMCaptureForProduct.Contains(productName.ToLower())) {
|
||||
if (productName != null && Configuration.NoDWMCaptureForProduct.Contains(productName.ToLower())) {
|
||||
return false;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
@ -573,14 +561,14 @@ namespace GreenshotPlugin.Core {
|
|||
/// <summary>
|
||||
/// Helper method to check if it is allowed to capture the process using GDI
|
||||
/// </summary>
|
||||
/// <param name="processName">Process owning the window</param>
|
||||
/// <param name="process">Process owning the window</param>
|
||||
/// <returns>true if it's allowed</returns>
|
||||
public static bool isGDIAllowed(Process process) {
|
||||
public static bool IsGdiAllowed(Process process) {
|
||||
if (process != null) {
|
||||
if (conf.NoGDICaptureForProduct != null && conf.NoGDICaptureForProduct.Count > 0) {
|
||||
if (Configuration.NoGDICaptureForProduct != null && Configuration.NoGDICaptureForProduct.Count > 0) {
|
||||
try {
|
||||
string productName = process.MainModule.FileVersionInfo.ProductName;
|
||||
if (productName != null && conf.NoGDICaptureForProduct.Contains(productName.ToLower())) {
|
||||
if (productName != null && Configuration.NoGDICaptureForProduct.Contains(productName.ToLower())) {
|
||||
return false;
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
@ -598,6 +586,25 @@ namespace GreenshotPlugin.Core {
|
|||
/// <param name="captureBounds">Rectangle with the bounds to capture</param>
|
||||
/// <returns>A Capture Object with a part of the Screen as an Image</returns>
|
||||
public static ICapture CaptureRectangle(ICapture capture, Rectangle captureBounds) {
|
||||
if (capture == null) {
|
||||
capture = new Capture();
|
||||
}
|
||||
// If the CaptureHandler has a handle use this, otherwise use the CaptureRectangle here
|
||||
capture.Image = CaptureHandler.CaptureScreenRectangle != null ? CaptureHandler.CaptureScreenRectangle(captureBounds) : CaptureRectangle(captureBounds);
|
||||
capture.Location = captureBounds.Location;
|
||||
if (capture.CaptureDetails != null) {
|
||||
((Bitmap)capture.Image).SetResolution(capture.CaptureDetails.DpiX, capture.CaptureDetails.DpiY);
|
||||
}
|
||||
return capture.Image == null ? null : capture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// This method will use User32 code to capture the specified captureBounds from the screen
|
||||
/// </summary>
|
||||
/// <param name="capture">ICapture where the captured Bitmap will be stored</param>
|
||||
/// <param name="captureBounds">Rectangle with the bounds to capture</param>
|
||||
/// <returns>A Capture Object with a part of the Screen as an Image</returns>
|
||||
public static ICapture CaptureRectangleFromDesktopScreen(ICapture capture, Rectangle captureBounds) {
|
||||
if (capture == null) {
|
||||
capture = new Capture();
|
||||
}
|
||||
|
@ -606,10 +613,7 @@ namespace GreenshotPlugin.Core {
|
|||
if (capture.CaptureDetails != null) {
|
||||
((Bitmap)capture.Image).SetResolution(capture.CaptureDetails.DpiX, capture.CaptureDetails.DpiY);
|
||||
}
|
||||
if (capture.Image == null) {
|
||||
return null;
|
||||
}
|
||||
return capture;
|
||||
return capture.Image == null ? null : capture;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -622,9 +626,9 @@ namespace GreenshotPlugin.Core {
|
|||
if (captureBounds.Height <= 0 || captureBounds.Width <= 0) {
|
||||
LOG.Warn("Nothing to capture, ignoring!");
|
||||
return null;
|
||||
} else {
|
||||
LOG.Debug("CaptureRectangle Called!");
|
||||
}
|
||||
LOG.Debug("CaptureRectangle Called!");
|
||||
|
||||
// .NET GDI+ Solution, according to some post this has a GDI+ leak...
|
||||
// See http://connect.microsoft.com/VisualStudio/feedback/details/344752/gdi-object-leak-when-calling-graphics-copyfromscreen
|
||||
// Bitmap capturedBitmap = new Bitmap(captureBounds.Width, captureBounds.Height);
|
||||
|
@ -668,69 +672,70 @@ namespace GreenshotPlugin.Core {
|
|||
|
||||
// Throw so people can report the problem
|
||||
throw exceptionToThrow;
|
||||
} else {
|
||||
// select the bitmap object and store the old handle
|
||||
using (SafeSelectObjectHandle selectObject = safeCompatibleDCHandle.SelectObject(safeDibSectionHandle)) {
|
||||
// bitblt over (make copy)
|
||||
GDI32.BitBlt(safeCompatibleDCHandle, 0, 0, captureBounds.Width, captureBounds.Height, desktopDCHandle, captureBounds.X, captureBounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
|
||||
}
|
||||
}
|
||||
// select the bitmap object and store the old handle
|
||||
using (safeCompatibleDCHandle.SelectObject(safeDibSectionHandle)) {
|
||||
// bitblt over (make copy)
|
||||
GDI32.BitBlt(safeCompatibleDCHandle, 0, 0, captureBounds.Width, captureBounds.Height, desktopDCHandle, captureBounds.X, captureBounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
|
||||
}
|
||||
|
||||
// get a .NET image object for it
|
||||
// A suggestion for the "A generic error occurred in GDI+." E_FAIL/0×80004005 error is to re-try...
|
||||
bool success = false;
|
||||
ExternalException exception = null;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
// Collect all screens inside this capture
|
||||
List<Screen> screensInsideCapture = new List<Screen>();
|
||||
foreach (Screen screen in Screen.AllScreens) {
|
||||
if (screen.Bounds.IntersectsWith(captureBounds)) {
|
||||
screensInsideCapture.Add(screen);
|
||||
}
|
||||
// get a .NET image object for it
|
||||
// A suggestion for the "A generic error occurred in GDI+." E_FAIL/0×80004005 error is to re-try...
|
||||
bool success = false;
|
||||
ExternalException exception = null;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
try {
|
||||
// Collect all screens inside this capture
|
||||
List<Screen> screensInsideCapture = new List<Screen>();
|
||||
foreach (Screen screen in Screen.AllScreens) {
|
||||
if (screen.Bounds.IntersectsWith(captureBounds)) {
|
||||
screensInsideCapture.Add(screen);
|
||||
}
|
||||
// Check all all screens are of an equal size
|
||||
bool offscreenContent = false;
|
||||
using (Region captureRegion = new Region(captureBounds)) {
|
||||
// Exclude every visible part
|
||||
foreach (Screen screen in screensInsideCapture) {
|
||||
captureRegion.Exclude(screen.Bounds);
|
||||
}
|
||||
// If the region is not empty, we have "offscreenContent"
|
||||
using (Graphics screenGraphics = Graphics.FromHwnd(User32.GetDesktopWindow())) {
|
||||
offscreenContent = !captureRegion.IsEmpty(screenGraphics);
|
||||
}
|
||||
}
|
||||
// Check all all screens are of an equal size
|
||||
bool offscreenContent;
|
||||
using (Region captureRegion = new Region(captureBounds)) {
|
||||
// Exclude every visible part
|
||||
foreach (Screen screen in screensInsideCapture) {
|
||||
captureRegion.Exclude(screen.Bounds);
|
||||
}
|
||||
// Check if we need to have a transparent background, needed for offscreen content
|
||||
if (offscreenContent) {
|
||||
using (Bitmap tmpBitmap = Bitmap.FromHbitmap(safeDibSectionHandle.DangerousGetHandle())) {
|
||||
// Create a new bitmap which has a transparent background
|
||||
returnBitmap = ImageHelper.CreateEmpty(tmpBitmap.Width, tmpBitmap.Height, PixelFormat.Format32bppArgb, Color.Transparent, tmpBitmap.HorizontalResolution, tmpBitmap.VerticalResolution);
|
||||
// Content will be copied here
|
||||
using (Graphics graphics = Graphics.FromImage(returnBitmap)) {
|
||||
// For all screens copy the content to the new bitmap
|
||||
foreach (Screen screen in Screen.AllScreens) {
|
||||
Rectangle screenBounds = screen.Bounds;
|
||||
// Make sure the bounds are offsetted to the capture bounds
|
||||
screenBounds.Offset(-captureBounds.X, -captureBounds.Y);
|
||||
graphics.DrawImage(tmpBitmap, screenBounds, screenBounds.X, screenBounds.Y, screenBounds.Width, screenBounds.Height, GraphicsUnit.Pixel);
|
||||
}
|
||||
// If the region is not empty, we have "offscreenContent"
|
||||
using (Graphics screenGraphics = Graphics.FromHwnd(User32.GetDesktopWindow())) {
|
||||
offscreenContent = !captureRegion.IsEmpty(screenGraphics);
|
||||
}
|
||||
}
|
||||
// Check if we need to have a transparent background, needed for offscreen content
|
||||
if (offscreenContent) {
|
||||
using (Bitmap tmpBitmap = Image.FromHbitmap(safeDibSectionHandle.DangerousGetHandle())) {
|
||||
// Create a new bitmap which has a transparent background
|
||||
returnBitmap = ImageHelper.CreateEmpty(tmpBitmap.Width, tmpBitmap.Height, PixelFormat.Format32bppArgb, Color.Transparent, tmpBitmap.HorizontalResolution, tmpBitmap.VerticalResolution);
|
||||
// Content will be copied here
|
||||
using (Graphics graphics = Graphics.FromImage(returnBitmap)) {
|
||||
// For all screens copy the content to the new bitmap
|
||||
foreach (Screen screen in Screen.AllScreens) {
|
||||
Rectangle screenBounds = screen.Bounds;
|
||||
// Make sure the bounds are offsetted to the capture bounds
|
||||
screenBounds.Offset(-captureBounds.X, -captureBounds.Y);
|
||||
graphics.DrawImage(tmpBitmap, screenBounds, screenBounds.X, screenBounds.Y, screenBounds.Width, screenBounds.Height, GraphicsUnit.Pixel);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// All screens, which are inside the capture, are of equal size
|
||||
// assign image to Capture, the image will be disposed there..
|
||||
returnBitmap = Bitmap.FromHbitmap(safeDibSectionHandle.DangerousGetHandle());
|
||||
}
|
||||
// We got through the capture without exception
|
||||
success = true;
|
||||
break;
|
||||
} catch (ExternalException ee) {
|
||||
LOG.Warn("Problem getting bitmap at try " + i + " : ", ee);
|
||||
exception = ee;
|
||||
} else {
|
||||
// All screens, which are inside the capture, are of equal size
|
||||
// assign image to Capture, the image will be disposed there..
|
||||
returnBitmap = Image.FromHbitmap(safeDibSectionHandle.DangerousGetHandle());
|
||||
}
|
||||
// We got through the capture without exception
|
||||
success = true;
|
||||
break;
|
||||
} catch (ExternalException ee) {
|
||||
LOG.Warn("Problem getting bitmap at try " + i + " : ", ee);
|
||||
exception = ee;
|
||||
}
|
||||
if (!success) {
|
||||
LOG.Error("Still couldn't create Bitmap!");
|
||||
}
|
||||
if (!success) {
|
||||
LOG.Error("Still couldn't create Bitmap!");
|
||||
if (exception != null) {
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue