Exemple #1
0
        public static Bitmap CaptureHearthstone(Point point, int width, int height, IntPtr wndHandle = default(IntPtr),
                                                bool requireInForeground = true)
        {
            if (wndHandle == default(IntPtr))
            {
                wndHandle = User32.GetHearthstoneWindow();
            }

            User32.ClientToScreen(wndHandle, ref point);
            if (requireInForeground && !User32.IsHearthstoneInForeground())
            {
                return(null);
            }

            try
            {
                var bmp      = new Bitmap(width, height, PixelFormat.Format32bppArgb);
                var graphics = Graphics.FromImage(bmp);
                graphics.CopyFromScreen(point.X, point.Y, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
                return(bmp);
            }
            catch (Exception ex)
            {
                Logger.WriteLine("Error capturing hearthstone: " + ex, "Helper");
                return(null);
            }
        }
Exemple #2
0
        private static async Task ClickOnPoint(IntPtr wndHandle, Point clientPoint)
        {
            User32.ClientToScreen(wndHandle, ref clientPoint);

            Cursor.Position = new Point(clientPoint.X, clientPoint.Y);

            //mouse down
            if (SystemInformation.MouseButtonsSwapped)
            {
                User32.mouse_event((uint)User32.MouseEventFlags.RightDown, 0, 0, 0, UIntPtr.Zero);
            }
            else
            {
                User32.mouse_event((uint)User32.MouseEventFlags.LeftDown, 0, 0, 0, UIntPtr.Zero);
            }

            await Task.Delay(Config.Instance.DeckExportDelay);

            //mouse up
            if (SystemInformation.MouseButtonsSwapped)
            {
                User32.mouse_event((uint)User32.MouseEventFlags.RightUp, 0, 0, 0, UIntPtr.Zero);
            }
            else
            {
                User32.mouse_event((uint)User32.MouseEventFlags.LeftUp, 0, 0, 0, UIntPtr.Zero);
            }

            await Task.Delay(Config.Instance.DeckExportDelay);
        }
        public static Bitmap CaptureScreen(IntPtr wndHandle, Point point, int width, int height)
        {
            User32.ClientToScreen(wndHandle, ref point);
            var bmp      = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            var graphics = Graphics.FromImage(bmp);

            graphics.CopyFromScreen(point.X, point.Y, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
            return(bmp);
        }
Exemple #4
0
        public static Bitmap CaptureHearthstone(Point point, int width, int height, IntPtr wndHandle = default(IntPtr))
        {
            if (wndHandle == default(IntPtr))
            {
                wndHandle = User32.GetHearthstoneWindow();
            }

            User32.ClientToScreen(wndHandle, ref point);
            if (!User32.IsHearthstoneInForeground())
            {
                return(null);
            }

            var bmp      = new Bitmap(width, height, PixelFormat.Format32bppArgb);
            var graphics = Graphics.FromImage(bmp);

            graphics.CopyFromScreen(point.X, point.Y, 0, 0, new Size(width, height), CopyPixelOperation.SourceCopy);
            return(bmp);
        }
Exemple #5
0
        public async Task GetCardCounts(Deck deck)
        {
            var hsHandle = User32.GetHearthstoneWindow();

            if (!User32.IsHearthstoneInForeground())
            {
                //restore window and bring to foreground
                User32.ShowWindow(hsHandle, User32.SwRestore);
                User32.SetForegroundWindow(hsHandle);
                //wait it to actually be in foreground, else the rect might be wrong
                await Task.Delay(500);
            }
            if (!User32.IsHearthstoneInForeground())
            {
                MessageBox.Show("Can't find Hearthstone window.");
                Logger.WriteLine("Can't find Hearthstone window.", "ArenaImport");
                return;
            }
            await Task.Delay(1000);

            Overlay.ForceHidden = true;
            Overlay.UpdatePosition();
            const double xScale          = 0.013;
            const double yScale          = 0.017;
            const int    targetHue       = 53;
            const int    hueMargin       = 3;
            const int    numVisibleCards = 21;
            var          hsRect          = User32.GetHearthstoneRect(false);
            var          ratio           = (4.0 / 3.0) / ((double)hsRect.Width / hsRect.Height);
            var          posX            = (int)DeckExporter.GetXPos(0.92, hsRect.Width, ratio);
            var          startY          = 71.0 / 768.0 * hsRect.Height;
            var          strideY         = 29.0 / 768.0 * hsRect.Height;
            int          width           = (int)Math.Round(hsRect.Width * xScale);
            int          height          = (int)Math.Round(hsRect.Height * yScale);

            for (var i = 0; i < Math.Min(numVisibleCards, deck.Cards.Count); i++)
            {
                var posY    = (int)(startY + strideY * i);
                var capture = Helper.CaptureHearthstone(new System.Drawing.Point(posX, posY), width, height, hsHandle);
                if (capture != null)
                {
                    var yellowPixels = 0;
                    for (int x = 0; x < width; x++)
                    {
                        for (int y = 0; y < height; y++)
                        {
                            var pixel = capture.GetPixel(x, y);
                            if (Math.Abs(pixel.GetHue() - targetHue) < hueMargin)
                            {
                                yellowPixels++;
                            }
                        }
                    }
                    //Console.WriteLine(yellowPixels + " of " + width * height + " - " + yellowPixels / (double)(width * height));
                    //capture.Save("arenadeckimages/" + i + ".png");
                    var yellowPixelRatio = yellowPixels / (double)(width * height);
                    if (yellowPixelRatio > 0.25 && yellowPixelRatio < 50)
                    {
                        deck.Cards[i].Count = 2;
                    }
                }
            }

            if (deck.Cards.Count > numVisibleCards)
            {
                const int scrollClicksPerCard = 4;
                const int scrollDistance      = 120;
                var       clientPoint         = new System.Drawing.Point(posX, (int)startY);
                var       previousPos         = System.Windows.Forms.Cursor.Position;
                User32.ClientToScreen(hsHandle, ref clientPoint);
                System.Windows.Forms.Cursor.Position = new System.Drawing.Point(clientPoint.X, clientPoint.Y);
                for (int j = 0; j < scrollClicksPerCard * (deck.Cards.Count - numVisibleCards); j++)
                {
                    User32.mouse_event((uint)User32.MouseEventFlags.Wheel, 0, 0, -scrollDistance, UIntPtr.Zero);
                    await Task.Delay(30);
                }
                System.Windows.Forms.Cursor.Position = previousPos;
                await Task.Delay(100);

                var remainingCards = deck.Cards.Count - numVisibleCards;
                startY = 76.0 / 768.0 * hsRect.Height + (numVisibleCards - remainingCards) * strideY;
                for (int i = 0; i < remainingCards; i++)
                {
                    var posY    = (int)(startY + strideY * i);
                    var capture = Helper.CaptureHearthstone(new System.Drawing.Point(posX, posY), width, height, hsHandle);
                    if (capture != null)
                    {
                        var yellowPixels = 0;
                        for (int x = 0; x < width; x++)
                        {
                            for (int y = 0; y < height; y++)
                            {
                                var pixel = capture.GetPixel(x, y);
                                if (Math.Abs(pixel.GetHue() - targetHue) < hueMargin)
                                {
                                    yellowPixels++;
                                }
                            }
                        }
                        //Console.WriteLine(yellowPixels + " of " + width * height + " - " + yellowPixels / (double)(width * height));
                        //capture.Save("arenadeckimages/" + i + 21 + ".png");
                        var yellowPixelRatio = yellowPixels / (double)(width * height);
                        if (yellowPixelRatio > 0.25 && yellowPixelRatio < 50)
                        {
                            deck.Cards[numVisibleCards + i].Count = 2;
                        }
                    }
                }

                System.Windows.Forms.Cursor.Position = new System.Drawing.Point(clientPoint.X, clientPoint.Y);
                for (int j = 0; j < scrollClicksPerCard * (deck.Cards.Count - 21); j++)
                {
                    User32.mouse_event((uint)User32.MouseEventFlags.Wheel, 0, 0, scrollDistance, UIntPtr.Zero);
                    await Task.Delay(30);
                }
                System.Windows.Forms.Cursor.Position = previousPos;
            }

            Overlay.ForceHidden = false;
            Overlay.UpdatePosition();

            ActivateWindow();
        }