예제 #1
0
        private void buttonLoad_Click(object sender, EventArgs e)
        {
            var srcScreenshot = LoadTestScreenshot(comboBoxFileName.Text + ".jpg");

            pictureBoxSrc.Image = srcScreenshot;

            var fastBitmap  = ScreenshotUtilities.ConvertToFastBitmap(srcScreenshot);
            var debugBounds = new List <Rectangle>();

            foreach (var scanner in scanners)
            {
                var resultOb = scanner.Process(fastBitmap);
                if (resultOb != null)
                {
                    Console.WriteLine("{0} found!\n\n{1}", scanner.ScannerName, resultOb);
                    break;
                }
            }

            var processedScreenshot = ScreenshotUtilities.CreateBitmapWithShapes(fastBitmap, debugBounds);

            pictureBoxAnalyzed.Image = processedScreenshot;

            var savePath = samplesFolder + "analyzed.png";

            if (File.Exists(savePath))
            {
                File.Delete(savePath);
            }
            processedScreenshot.Save(savePath, ImageFormat.Png);
        }
예제 #2
0
        private void Scan()
        {
            gameLogic.OnScanPrep();
            if (numScanSnapshotDelayTicks > 0)
            {
                return;
            }

            var srcScreenshot = screenReader.DoWork();

            if (srcScreenshot != null)
            {
                if (cachedSourceScreen != null)
                {
                    cachedSourceScreen.Dispose();
                }
                cachedSourceScreen = srcScreenshot.Clone(new Rectangle(0, 0, srcScreenshot.Width, srcScreenshot.Height), srcScreenshot.PixelFormat);

                if (screenReader.GetState() != ScreenReader.EState.WindowTooSmall &&
                    (numScanProcessDelayTicks <= 0))
                {
                    var forcedSize = screenReader.GetExpectedSize();
                    var fastBitmap = ScreenshotUtilities.ConvertToFastBitmap(srcScreenshot, forcedSize.Width, forcedSize.Height);

                    foreach (var scanner in scanners)
                    {
                        var resultOb = scanner.Process(fastBitmap);
                        if (resultOb != null)
                        {
                            gameLogic.screenScanner = scanner;
                            gameLogic.screenData    = resultOb;
                            break;
                        }
                    }

                    gameLogic.OnScan();
                    if (hasDetailCtrl)
                    {
                        using (Graphics graphics = Graphics.FromImage(srcScreenshot))
                        {
                            gameLogic.DrawScanHighlights(graphics);
                        }
                    }
                }

                pictureBoxAnalyzed.Image      = srcScreenshot;
                labelScreenshotFailed.Visible = false;
            }
            else
            {
                labelScreenshotFailed.Visible = hasDetailCtrl;
            }

            SetScreenState(screenReader.GetState());
        }
예제 #3
0
        private async Task AnalyzeFrame()
        {
            textScanResults.Text = "";

            var _bitmap = new RenderTargetBitmap();
            await _bitmap.RenderAsync(previewImage);

            var pixels = await _bitmap.GetPixelsAsync();

            int pixelW = _bitmap.PixelWidth;
            int pixelH = _bitmap.PixelHeight;

            byte[] usePixels = null;

            BitmapBounds cropBounds = new BitmapBounds()
            {
                X = 0, Y = 0, Width = 0, Height = 0
            };

            if (pixelW == 462 && pixelH == 864)
            {
                cropBounds = new BitmapBounds()
                {
                    X = 1, Y = 46, Width = 458, Height = 813
                };
            }
            else if (pixelW == 440 && pixelH == 822)
            {
                cropBounds = new BitmapBounds()
                {
                    X = 2, Y = 42, Width = 436, Height = 778
                };
            }

            if (cropBounds.Width > 0)
            {
                var scaledW = 338;
                var scaledH = 600;

                try
                {
                    // transform: crop
                    InMemoryRandomAccessStream streamCrop = new InMemoryRandomAccessStream();
                    {
                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, streamCrop);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)pixelW, (uint)pixelH, 96, 96, pixels.ToArray());
                        encoder.BitmapTransform.Bounds = cropBounds;
                        await encoder.FlushAsync();
                    }

                    InMemoryRandomAccessStream streamSize = new InMemoryRandomAccessStream();
                    {
                        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(streamCrop);

                        var pixelsProvider = await decoder.GetPixelDataAsync();

                        var inPixels = pixelsProvider.DetachPixelData();

                        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.BmpEncoderId, streamSize);

                        encoder.SetPixelData(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Ignore, (uint)cropBounds.Width, (uint)cropBounds.Height, 96, 96, inPixels);
                        encoder.BitmapTransform.ScaledHeight      = (uint)scaledH;
                        encoder.BitmapTransform.ScaledWidth       = (uint)scaledW;
                        encoder.BitmapTransform.InterpolationMode = BitmapInterpolationMode.Linear;
                        await encoder.FlushAsync();
                    }

                    {
                        BitmapDecoder decoder = await BitmapDecoder.CreateAsync(streamSize);

                        var pixelsProvider = await decoder.GetPixelDataAsync();

                        pixelW    = (int)scaledW;
                        pixelH    = (int)scaledH;
                        usePixels = pixelsProvider.DetachPixelData();
                    }

                    streamCrop.Dispose();
                    streamSize.Dispose();
                }
                catch (Exception ex)
                {
                }
            }

            try
            {
                byte[] pixelData     = (usePixels != null) ? usePixels : pixels.ToArray();
                var    analyzeBitmap = ScreenshotUtilities.ConvertToFastBitmap(pixelData, pixelW, pixelH);

                foreach (var scanner in scanners)
                {
                    object resultOb = scanner.Process(analyzeBitmap);
                    if (resultOb != null)
                    {
                        textScanResults.Text = scanner.ScannerName + " found!\n\n" + resultOb;
                        break;
                    }
                }

                StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
                StorageFile   exportFile    = await storageFolder.CreateFileAsync("image-test.jpg", CreationCollisionOption.ReplaceExisting);

                using (IRandomAccessStream stream = await exportFile.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, stream);

                    byte[] bytes = pixels.ToArray();
                    encoder.SetPixelData(BitmapPixelFormat.Bgra8,
                                         BitmapAlphaMode.Ignore,
                                         (uint)pixelW,
                                         (uint)pixelH,
                                         200,
                                         200,
                                         pixelData);

                    await encoder.FlushAsync();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }