Пример #1
0
        /// <summary>
        /// Captures a specific area from the screen.
        /// </summary>
        public static CaptureImage Rectangle(Rectangle bounds, CaptureSettings settings = null)
        {
            // Calculate the size of the output rectangle
            var outputRectangle = CaptureUtilities.ScaleAccordingToSettings(bounds, settings);

            Bitmap bmp;

            if (outputRectangle.Width == bounds.Width || outputRectangle.Height == bounds.Height)
            {
                // Capture directly without any resizing
                bmp = CaptureDesktopToBitmap(bounds.Width, bounds.Height, (dest, src) =>
                {
                    Gdi32.BitBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, CopyPixelOperation.SourceCopy | CopyPixelOperation.CaptureBlt);
                });
            }
            else
            {
                //  Capture with scaling
                bmp = CaptureDesktopToBitmap(outputRectangle.Width, outputRectangle.Height, (dest, src) =>
                {
                    Gdi32.SetStretchBltMode(dest, StretchMode.STRETCH_HALFTONE);
                    Gdi32.StretchBlt(dest, outputRectangle.X, outputRectangle.Y, outputRectangle.Width, outputRectangle.Height, src, bounds.X, bounds.Y, bounds.Width, bounds.Height, TernaryRasterOperations.SRCCOPY | TernaryRasterOperations.CAPTUREBLT);
                });
            }
            return(new CaptureImage(bmp, bounds, settings));
        }
Пример #2
0
        /// <inheritdoc />
        public override void Draw(Graphics g)
        {
            var outputPoint  = new Point();
            var cursorBitmap = CaptureUtilities.CaptureCursor(ref outputPoint);

            if (cursorBitmap == null)
            {
                outputPoint = CaptureUtilities.GetMousePosition();
            }
            // Fix the coordinates for multi-screen scenarios
            outputPoint.X -= CaptureImage.OriginalBounds.Left;
            outputPoint.Y -= CaptureImage.OriginalBounds.Top;
            // Check for scaling and handle that
            var scale = CaptureUtilities.GetScale(CaptureImage.OriginalBounds, CaptureImage.Settings);

            if (scale != 1)
            {
                outputPoint.X = (outputPoint.X * scale).ToInt();
                outputPoint.Y = (outputPoint.Y * scale).ToInt();
                var origInterpolationMode = g.InterpolationMode;
                g.InterpolationMode = InterpolationMode.HighQualityBicubic;

                if (cursorBitmap != null)
                {
                    g.DrawImage(cursorBitmap, outputPoint.X, outputPoint.Y, (cursorBitmap.Width * scale).ToInt(),
                                (cursorBitmap.Height * scale).ToInt());
                }
                else
                {
                    g.FillEllipse(new SolidBrush(Color.Red), outputPoint.X, outputPoint.Y, 30, 30);
                }


                g.InterpolationMode = origInterpolationMode;
            }
            else
            {
                if (cursorBitmap != null)
                {
                    g.DrawImage(cursorBitmap, outputPoint.X, outputPoint.Y);
                }
                else
                {
                    g.FillEllipse(new SolidBrush(Color.Red), outputPoint.X, outputPoint.Y, 30, 30);
                }
            }
            // Cleanup
            if (cursorBitmap != null)
            {
                cursorBitmap.Dispose();
            }
        }