public void QuantizersPerformanceTest()
 {
     using var bmpRef = Icons.Information.ExtractBitmap(new Size(256, 256));
     new PerformanceTest {
         TestName = $"{bmpRef.Width}x{bmpRef.Height}@{bmpRef.GetColorCount()}", Iterations = 25, CpuAffinity = null
     }
     .AddCase(() =>
     {
         using var result = bmpRef.CloneBitmap();
         result.Quantize(PredefinedColorsQuantizer.SystemDefault8BppPalette());
     }, nameof(PredefinedColorsQuantizer.SystemDefault8BppPalette))
     .AddCase(() =>
     {
         using var result = bmpRef.CloneBitmap();
         result.Quantize(OptimizedPaletteQuantizer.Octree());
     }, nameof(OptimizedPaletteQuantizer.Octree))
     .AddCase(() =>
     {
         using var result = bmpRef.CloneBitmap();
         result.Quantize(OptimizedPaletteQuantizer.MedianCut());
     }, nameof(OptimizedPaletteQuantizer.MedianCut))
     .AddCase(() =>
     {
         using var result = bmpRef.CloneBitmap();
         result.Quantize(OptimizedPaletteQuantizer.Wu());
     }, nameof(OptimizedPaletteQuantizer.Wu))
     .DoTest()
     .DumpResults(Console.Out);
 }
Esempio n. 2
0
        public void SaveAsIconTest(PixelFormat pixelFormat)
        {
            var        ms        = new MemoryStream();
            IQuantizer quantizer = pixelFormat.IsIndexed() ? OptimizedPaletteQuantizer.Octree(1 << pixelFormat.ToBitsPerPixel()) : null;
            var        refImage  = Convert(Icons.Information.ExtractBitmap(new Size(64, 64)), pixelFormat, quantizer);

            refImage.SaveAsIcon(ms);
            ms.Position = 0;
            var bmp = new Bitmap(ms);

            Assert.AreEqual(ImageFormat.Icon, bmp.RawFormat);
            Assert.AreEqual(PixelFormat.Format32bppArgb, bmp.PixelFormat);
            SaveImage($"{pixelFormat}", bmp, true);
        }
Esempio n. 3
0
        public void SaveAsTiffTest(PixelFormat pixelFormat, PixelFormat savedFormat, PixelFormat?savedFormatLinux = null)
        {
            var        ms        = new MemoryStream();
            IQuantizer quantizer = pixelFormat.IsIndexed() ? OptimizedPaletteQuantizer.Octree(1 << pixelFormat.ToBitsPerPixel(), Color.Silver, 0) : null;
            var        refImage  = Convert(Icons.Information.ExtractBitmap(new Size(256, 256)), pixelFormat, quantizer);

            refImage.SaveAsTiff(ms);
            ms.Position = 0;
            var bmp = new Bitmap(ms);

            Assert.AreEqual(ImageFormat.Tiff, bmp.RawFormat);
            Assert.AreEqual(OSUtils.IsWindows ? savedFormat : savedFormatLinux ?? savedFormat, bmp.PixelFormat);
            SaveImage($"{pixelFormat}", bmp, true);
        }
Esempio n. 4
0
        public void AddBitmapSaveUncompressedTest(PixelFormat pixelFormat)
        {
            var        ms        = new MemoryStream();
            IQuantizer quantizer = pixelFormat.IsIndexed() ? OptimizedPaletteQuantizer.Octree(1 << pixelFormat.ToBitsPerPixel()) : null;
            var        size      = OSUtils.IsWindows ? new Size(256, 256) : new Size(64, 64);
            var        refImage  = Convert(Icons.Information.ExtractBitmap(size), pixelFormat, quantizer);

            using (RawIcon icon = new RawIcon())
            {
                icon.Add(refImage);
                icon.Save(ms, true);
            }

            ms.Position = 0;
            var bmp = new Bitmap(ms);

            Assert.AreEqual(ImageFormat.Icon, bmp.RawFormat);
            Assert.AreEqual(PixelFormat.Format32bppArgb, bmp.PixelFormat);
            Assert.IsTrue(!pixelFormat.IsIndexed() && !pixelFormat.HasAlpha() || bmp.GetPixel(0, 0).A == 0, "Transparency expected");
            SaveImage($"{pixelFormat}", bmp, true);
        }
Esempio n. 5
0
        public static byte[] Capture(Screen monitor)
        {
            byte[] imageBytes = null;
            try
            {
                Bitmap    resultBmp     = null;
                Rectangle monitorRect   = monitor.Bounds;
                HWND      desktopWindow = User32.GetDesktopWindow();
                HDC       windowDc      = User32.GetWindowDC(desktopWindow);
                HDC       memDc         = Gdi32.CreateCompatibleDC(windowDc);
                var       bitmap        = Gdi32.CreateCompatibleBitmap(windowDc, monitorRect.Width, monitorRect.Height);
                var       oldBitmap     = Gdi32.SelectObject(memDc, bitmap);

                bool result = Gdi32.BitBlt(memDc, 0, 0, monitorRect.Width, monitorRect.Height, windowDc, monitorRect.X, monitorRect.Y, Gdi32.RasterOperationMode.SRCCOPY);

                User32.CURSORINFO pci = new();
                pci.cbSize = (uint)Marshal.SizeOf(typeof(User32.CURSORINFO));

                if (User32.GetCursorInfo(ref pci))
                {
                    if (pci.flags == User32.CursorState.CURSOR_SHOWING)
                    {
                        User32.DrawIcon(memDc, pci.ptScreenPos.X - monitorRect.X, pci.ptScreenPos.Y - monitorRect.Y, pci.hCursor.DangerousGetHandle());
                    }
                }

                if (result)
                {
                    resultBmp = bitmap.ToBitmap();
                }

                Gdi32.SelectObject(memDc, oldBitmap);
                Gdi32.DeleteObject(bitmap);
                Gdi32.DeleteDC(memDc);
                User32.ReleaseDC(desktopWindow, windowDc);

                var shrunkImage = resultBmp.GetThumbnailImage(320, 240, null, IntPtr.Zero);
                resultBmp.Dispose();

                var cloned = shrunkImage.ConvertPixelFormatAsync(PixelFormat.Format8bppIndexed, OptimizedPaletteQuantizer.Octree(), ErrorDiffusionDitherer.FloydSteinberg, new TaskConfig()).GetAwaiter().GetResult();
                shrunkImage.Dispose();

                short[] colors = new short[256];

                var pal = (Color[])cloned.Palette.Entries.Clone();
                Parallel.For(0, cloned.Palette.Entries.Length, i =>
                {
                    colors[i] = ConvertColor(pal[i].R, pal[i].G, pal[i].B);
                });

                imageBytes = new byte[colors.Length * sizeof(short) + cloned.Width * cloned.Height];
                Buffer.BlockCopy(colors, 0, imageBytes, 0, colors.Length * sizeof(short));

                BitmapData imageData = cloned.LockBits(new Rectangle(0, 0, cloned.Width, cloned.Height), ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                Marshal.Copy(imageData.Scan0, imageBytes, colors.Length * sizeof(short), cloned.Width * cloned.Height);
                cloned.UnlockBits(imageData);
                cloned.Dispose();

                _errorCount = 0;
            }
            catch (Exception ex)
            {
                _errorCount++;
                return(CaptureErr(monitor, ex));
            }

            return(imageBytes);
        }