Example #1
0
        public string DeleteFromSQL(Note badNote)
        {
            string badPath = null;

            if (badNote.GetType() == typeof(Screenshot))
            {
                Screenshot badScreen = badNote as Screenshot;
                badPath = badScreen.ImgPath;
                if (badScreen.GetImg() != null)
                {
                    try
                    {
                        badScreen.Dispose();
                        badScreen = null;
                        GC.Collect();
                        File.Delete(badPath);
                    }
                    catch
                    {
                    }
                }
            }

            badNote.IsDelete = true;
            UpdateNote(badNote);
            return(badPath);
        }
        protected override void Dispose(bool disposing)
        {
            if (Screenshot != null)
            {
                Screenshot.Dispose();
            }

            base.Dispose(disposing);
        }
        private bool disposedValue = false; // To detect redundant calls

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                Screenshot?.Dispose();

                disposedValue = true;
            }
        }
        protected void ProcessCapture(RetrieveImageDataParams data)
        {
            var screenshot = new Screenshot(data.RequestId, data.Data, data.Width, data.Height, data.Pitch);

            try
            {
                Interface.SendScreenshotResponse(screenshot);
                LastCaptureTime = Timer.Elapsed;
            }
            catch (RemotingException ex)
            {
                TraceMessage("RemotingException: " + ex.Message);
                screenshot.Dispose();
                // Ignore remoting exceptions
                // .NET Remoting will throw an exception if the host application is unreachable
            }
            catch (Exception e)
            {
                DebugMessage(e.ToString());
                screenshot.Dispose();
            }
        }
Example #5
0
        void ReleaseDesignerOutlets()
        {
            if (Screenshot != null)
            {
                Screenshot.Dispose();
                Screenshot = null;
            }

            if (CloseButton != null)
            {
                CloseButton.Dispose();
                CloseButton = null;
            }

            if (TweetButton != null)
            {
                TweetButton.Dispose();
                TweetButton = null;
            }

            if (PostButton != null)
            {
                PostButton.Dispose();
                PostButton = null;
            }

            if (ButtonSplitter != null)
            {
                ButtonSplitter.Dispose();
                ButtonSplitter = null;
            }

            if (ScreenshotSpinner != null)
            {
                ScreenshotSpinner.Dispose();
                ScreenshotSpinner = null;
            }

            if (PrevBtn != null)
            {
                PrevBtn.Dispose();
                PrevBtn = null;
            }

            if (NextBtn != null)
            {
                NextBtn.Dispose();
                NextBtn = null;
            }
        }
Example #6
0
        private unsafe void ProcessCapture()
        {
            try
            {
                Screenshot         ssht = _capture.Capture();
                Image <Gray, byte> process;

                fixed(byte *p = ssht.Data)
                {
                    IntPtr ptr = (IntPtr)p;

                    Screen = new Image <Bgra, byte>(ssht.Width, ssht.Height, ssht.Stride, ptr)
                    {
                        ROI = BOX_SIZE
                    };
                    process = Screen.Convert <Gray, byte>();
                }
                ssht.Dispose();

                _force = new Vec2();

                // Read player position from memory
                _playerPos = GetPlayerPosition();

                Rectangle powerDetectionROI = new Rectangle(
                    (int)Math.Max(_playerPos.X - 100, 0),
                    (int)Math.Max(_playerPos.Y - 75, 0),
                    (int)Math.Min(BOX_SIZE.Width - (_playerPos.X - 100), 200),
                    (int)Math.Min(BOX_SIZE.Height - (_playerPos.Y - 75), 100));
                // Look for power
                process.ROI = powerDetectionROI;

                using (Image <Gray, float> result = process.MatchTemplate(_imgPower, TemplateMatchingType.SqdiffNormed))
                {
                    double minDistSq = double.MaxValue;
                    int    minX = 0, minY = 0;
                    for (int y = 0; y < result.Height; y++)
                    {
                        for (int x = 0; x < result.Width; x++)
                        {
                            if (result.Data[y, x, 0] < 0.20)
                            {
                                double dist =
                                    (x - _playerPos.X) * (x - _playerPos.X) +
                                    (y - _playerPos.Y) * (y - _playerPos.Y);
                                if (dist < minDistSq)
                                {
                                    minDistSq = dist;
                                    minX      = x;
                                    minY      = y;
                                }
                            }
                        }
                    }
                    if (minDistSq != double.MaxValue)
                    {
                        Rectangle match = new Rectangle(minX + process.ROI.X, minY + process.ROI.Y, _imgPower.Width,
                                                        _imgPower.Height);
                        if (DO_DRAWING)
                        {
                            Screen.Draw(match, new Bgra(0, 255, 255, 255), 2);
                            Screen.Draw(new LineSegment2DF(match.Location, _playerPos), new Bgra(0, 255, 255, 255), 1);
                        }
                        Vec2 acc = Vec2.CalculateForce(
                            new Vec2(match.X + _imgPower.Width / 2.0, match.Y + _imgPower.Height / 2.0),
                            new Vec2(_playerPos.X, _playerPos.Y), 4000);
                        _force += acc;
                    }
                }

                // Processing bounding box
                Rectangle bulletDetectionROI = new Rectangle(
                    (int)Math.Max(_playerPos.X - INITIAL_DETECTION_RADIUS, 0),
                    (int)Math.Max(_playerPos.Y - INITIAL_DETECTION_RADIUS, 0),
                    (int)Math.Min(BOX_SIZE.Width - ((int)_playerPos.X - INITIAL_DETECTION_RADIUS), INITIAL_DETECTION_RADIUS * 2),
                    (int)Math.Min(BOX_SIZE.Height - ((int)_playerPos.Y - INITIAL_DETECTION_RADIUS), INITIAL_DETECTION_RADIUS * 2));
                process.ROI = bulletDetectionROI;


                if (TEST_MODE)
                {
                    return;
                }
                Vec2 _playerVec = new Vec2(_playerPos.X, _playerPos.Y);

                var binthresh = process.SmoothBlur(3, 3).ThresholdBinary(new Gray(240), new Gray(255)); //220
                // Detect blobs (bullets) on screen
                CvBlobs resultingImgBlobs = new CvBlobs();
                uint    noBlobs           = _bDetect.Detect(binthresh, resultingImgBlobs);
                int     blobCount         = 0;
                resultingImgBlobs.FilterByArea(10, 500);
                foreach (CvBlob targetBlob in resultingImgBlobs.Values)
                {
                    if (DO_DRAWING)
                    {
                        Screen.ROI = new Rectangle(process.ROI.X + BOX_SIZE.X, process.ROI.Y + BOX_SIZE.Y,
                                                   process.ROI.Width,
                                                   process.ROI.Height);
                        Screen.FillConvexPoly(targetBlob.GetContour(), new Bgra(0, 0, 255, 255));
                    }
                    // Find closest point on blob contour to player
                    Point  minPoint = targetBlob.GetContour()[0];
                    double minDist  = double.MaxValue;
                    foreach (var point in targetBlob.GetContour())
                    {
                        Point  adj  = new Point(point.X + process.ROI.X, point.Y + process.ROI.Y);
                        double dist =
                            (adj.X - _playerPos.X) * (adj.X - _playerPos.X) +
                            (adj.Y - _playerPos.Y) * (adj.Y - _playerPos.Y);

                        if (dist < minDist)
                        {
                            minPoint = adj;
                            minDist  = dist;
                        }
                    }
                    // Ensure the bullet is in the correct range
                    if (minDist < _detectionRadius * _detectionRadius)
                    {
                        // Calculate forces
                        Vec2 acc = Vec2.CalculateForce(new Vec2(minPoint.X, minPoint.Y),
                                                       _playerVec, -5000);
                        _force += acc;
                        if (DO_DRAWING)
                        {
                            Screen.ROI = BOX_SIZE;
                            Screen.Draw(new LineSegment2DF(_playerPos, minPoint), new Bgra(0, 255, 128, 255), 1);
                        }
                        blobCount++;
                    }
                }
                Screen.ROI  = BOX_SIZE;
                process.ROI = Rectangle.Empty;

                // Calculate new detection orb radius
                //float nRad = Math.Max(20.0f, INITIAL_DETECTION_RADIUS/(1 + blobCount*0.3f));
                if (blobCount >= 1)
                {
                    _detectionRadius = (_detectionRadius * 29 + 5.0f) / 30.0f;
                }
                else
                {
                    _detectionRadius = (_detectionRadius * 59 + INITIAL_DETECTION_RADIUS) / 60.0f;
                }

                // Account for border force, to prevent cornering
                //if (BOX_SIZE.Width - _playerPos.X < 120)
                _force += new Vec2(Vec2.CalculateForce(BOX_SIZE.Width - _playerPos.X, -4000), 0);
                //if (_playerPos.X < 120)
                _force += new Vec2(Vec2.CalculateForce(_playerPos.X, 4000), 0);
                if (BOX_SIZE.Height - _playerPos.Y < 50)
                {
                    _force += new Vec2(0, Vec2.CalculateForce(BOX_SIZE.Height - _playerPos.Y, -2000));
                }
                if (_playerPos.Y < 200)
                {
                    _force += new Vec2(0, Vec2.CalculateForce(_playerPos.Y, 2000));
                }
                // Corners are the devil
                _force += Vec2.CalculateForce(new Vec2(BOX_SIZE.Width, BOX_SIZE.Height),
                                              _playerVec, -2000);
                _force += Vec2.CalculateForce(new Vec2(0, BOX_SIZE.Height),
                                              _playerVec, -2000);
                _force += Vec2.CalculateForce(new Vec2(0, 0),
                                              _playerVec, -2000);
                _force += Vec2.CalculateForce(new Vec2(BOX_SIZE.Width, 0),
                                              _playerVec, -2000);

                // Assist force
                if (ShouldAssist)
                {
                    Vec2   sub  = new Vec2(AssistPoint.X, AssistPoint.Y) - _playerVec;
                    double dist = sub.Length();
                    _force += new Vec2(sub.X / dist * 2, sub.Y / dist * 2);
                }
                //imageToShow.Draw("BLOB_AREA: " + percBlob, new Point(10, 20), FontFace.HersheyPlain, 1, new Bgra(255, 255, 255, 255), 1);

                if (DO_DRAWING)
                {
                    Screen.Draw(
                        new Rectangle((int)(_playerPos.X - 3), (int)(_playerPos.Y - 3), 6, 6),
                        new Bgra(0, 255, 0, 255),
                        2);

                    Screen.Draw(new CircleF(_playerPos, _detectionRadius), new Bgra(0, 255, 255, 255), 1);
                    if (ShouldAssist)
                    {
                        Screen.Draw(
                            new LineSegment2DF(_playerPos, AssistPoint),
                            new Bgra(128, 0, 255, 255), 2);
                        Screen.Draw("ASSIST", new Point(10, 40), FontFace.HersheyPlain, 1, new Bgra(0, 255, 0, 255), 1);
                    }
                    // Draw force vector
                    Screen.Draw(
                        new LineSegment2DF(_playerPos,
                                           new PointF((float)(_playerPos.X + _force.X), (float)(_playerPos.Y + _force.Y))),
                        new Bgra(0, 128, 255, 255), 5);
                    Screen.Draw(powerDetectionROI, new Bgra(255, 255, 0, 255), 1);
                    Screen.Draw(bulletDetectionROI, new Bgra(0, 0, 255, 255), 1);
                    if (DoMovement)
                    {
                        Screen.Draw("DO_MOVEMENT", new Point(10, 20), FontFace.HersheyPlain, 1, new Bgra(0, 255, 0, 255),
                                    1);
                    }
                    _form.imageBox.Image = Screen;
                }
                process.Dispose();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
        private ScreenshotResource CaptureDirectX(IntPtr wnd)
        {
            if (!this.AttachHookToProcess())
            {
                throw new ScreenshotCaptureException("Error attaching to DirectX");
            }
            ScreenshotResource result = null;

            // Bitmap img = null;
            // captureProcess.BringProcessWindowToFront();
            // Initiate the screenshot of the CaptureInterface, the appropriate event handler within the target process will take care of the rest

            if (this.captureConfig.Direct3DVersion == Direct3DVersion.Direct3D9 ||
                this.captureConfig.Direct3DVersion == Direct3DVersion.Direct3D9Simple)
            {
                var start = DateTime.Now.Ticks;
                var task  = Task <Screenshot> .Factory.FromAsync(
                    (rect, timeout, callback, ctxt) => this.captureProcess.CaptureInterface.BeginGetScreenshot(rect, timeout, callback),
                    this.captureProcess.CaptureInterface.EndGetScreenshot,
                    emptyRect,
                    waitForScreenshotTimeout,
                    null);

                Screenshot screen = null;
                try
                {
                    task.Wait();
                    screen = task.Result;

                    var stop = DateTime.Now.Ticks;
                    var proc = TimeSpan.FromTicks(stop - start).Milliseconds;
                    TraceLog.Log("DX Capture speed: {0}", proc);

                    if (screen == null && directxRetryCount == 0)
                    {
                        Log.Debug("No data received from DirectX hook, retrying once.");
                        directxRetryCount++;
                        return(CaptureDirectX(wnd));
                    }
                    else if (screen == null)
                    {
                        Log.Debug("No data received from DirectX hook.");
                        return(null);
                    }
                    directxRetryCount = 0;

                    task.Dispose();
                    try
                    {
                        var width      = screen.Width;
                        var height     = screen.Height;
                        var bitmapData = screen.CapturedBitmap;
                        var img        = new Bitmap(width, height, PixelFormat.Format32bppRgb);
                        var bmpData    = img.LockBits(new Rectangle(0, 0, img.Width, img.Height), ImageLockMode.WriteOnly, img.PixelFormat);

                        Marshal.Copy(bitmapData, 0, bmpData.Scan0, bitmapData.Length);
                        img.UnlockBits(bmpData);

                        result = new ScreenshotResource(img);
                    }
                    catch (Exception ex)
                    {
                        Log.Debug("Error decoding DirectX pixels: {0}", ex);
                        return(null);
                    }
                }
                finally
                {
                    if (screen != null)
                    {
                        screen.Dispose();
                    }
                }
            }
            else if (this.captureConfig.Direct3DVersion == Direct3DVersion.Direct3D9SharedMem)
            {
                try
                {
                    if (!hookReadyWaitHandle.WaitOne(2000))
                    {
                        Log.Debug("Waiting for DirectX hook initialization.");
                        return(null);
                    }

                    captureDxWaitHandle.Set();
                    if (this.copyDataMem == null)
                    {
                        try
                        {
                            this.copyDataMem       = MemoryMappedFile.OpenExisting("CaptureHookSharedMemData", MemoryMappedFileRights.Read);
                            this.copyDataMemAccess = this.copyDataMem.CreateViewAccessor(0, 0, MemoryMappedFileAccess.Read);
                            this.copyDataMemAccess.SafeMemoryMappedViewHandle.AcquirePointer(ref this.copyDataMemPtr);
                        }
                        catch (FileNotFoundException)
                        {
                            // not hooked
                            Log.Debug("Shared memory not found");
                            return(null);
                        }
                    }
                    if (copyDataMemAccess == null)
                    {
                        // not hooked
                        Log.Debug("Shared memory not opened yet");
                        return(null);
                    }

                    int      lastRendered;
                    CopyData copyData = (*((CopyData *)this.copyDataMemPtr));
                    if (copyData.height <= 0 || copyData.textureId == Guid.Empty)
                    {
                        return(null);
                    }
                    lastRendered = copyData.lastRendered;

                    if (lastRendered != -1)
                    {
                        if (!this.sharedMemMutexes[lastRendered].WaitOne(1000))
                        {
                            Log.Warn("Failed acquiring shared texture lock in time (1000).");
                            return(null);
                        }
                        if (this.lastKnownTextureId != copyData.textureId)
                        {
                            for (int i = 0; i < 2; i++)
                            {
                                if (this.sharedTexturesAccess[i] != null)
                                {
                                    this.sharedTexturesAccess[i].SafeMemoryMappedViewHandle.ReleasePointer();
                                    this.sharedTexturesAccess[i].Dispose();
                                    this.sharedTexturesAccess[i] = null;
                                }
                                if (this.sharedTextures[i] != null)
                                {
                                    this.sharedTextures[i].Dispose();
                                    this.sharedTextures[i] = null;
                                }
                            }
                        }

                        if (this.sharedTextures[lastRendered] == null)
                        {
                            this.sharedTextures[lastRendered]       = MemoryMappedFile.OpenExisting(copyData.textureId.ToString() + lastRendered, MemoryMappedFileRights.ReadWrite);
                            this.sharedTexturesAccess[lastRendered] = this.sharedTextures[lastRendered].CreateViewAccessor(
                                0,
                                copyData.height * copyData.pitch,
                                MemoryMappedFileAccess.ReadWrite);
                            this.sharedTexturesAccess[lastRendered].SafeMemoryMappedViewHandle.AcquirePointer(ref sharedTexturesPtr[lastRendered]);
                        }

                        var img = new Bitmap(
                            copyData.width,
                            copyData.height,
                            copyData.pitch,
                            PixelFormat.Format32bppRgb,
                            new IntPtr(sharedTexturesPtr[lastRendered]));
                        this.lastKnownTextureId = copyData.textureId;
                        result = new DxScreenshotResource(img, this.sharedMemMutexes[lastRendered]);
                    }
                }
                catch (Exception ex)
                {
                    Log.Error(ex);
                }
            }

            return(result);
        }