Пример #1
0
        public static ImageSource Byte2ImageSourse(byte[] data)
        {
            using var ms = new WrappingStream(new MemoryStream(data));
            var bitmap = new BitmapImage();

            bitmap.BeginInit();
            bitmap.CacheOption   = BitmapCacheOption.OnLoad;
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.StreamSource  = ms;
            bitmap.EndInit();
            bitmap.Freeze();

            return(bitmap);
        }
Пример #2
0
        public static bool TryGetPrintWindowBmp(IntPtr hWnd, out ImageSource image)
        {
            if (!NativeMethod.GetWindowRect(hWnd, out NativeMethod.RECT rect))
            {
                image = null;
                return(false);
            }

            int width  = rect.right - rect.left;
            int height = rect.bottom - rect.top;

            if (width <= 0 || height <= 0)
            {
                image = null;
                return(false);
            }

            var windowBmp = new Bitmap(width, height);

            using var g = Graphics.FromImage(windowBmp);
            var dc = g.GetHdc();

            NativeMethod.PrintWindow(hWnd, dc, 0);
            g.ReleaseHdc(dc);

            using var ms = new WrappingStream(new MemoryStream());
            windowBmp.Save(ms, ImageFormat.Png);

            var bitmap = new BitmapImage();

            bitmap.BeginInit();
            bitmap.CacheOption   = BitmapCacheOption.OnLoad;
            bitmap.CreateOptions = BitmapCreateOptions.None;
            bitmap.StreamSource  = ms;
            bitmap.EndInit();
            bitmap.Freeze();
            image = bitmap;
            return(true);
        }