Adam
Adam

Reputation: 1493

Get the maximized window when using Application.Current.Windows?

I have some code that gathers all the windows associated with an application, renders them as bitmaps, and stores them in a List<BitmapSource> for later processing and handling.

My problem is, if the window is minimized, the image is just an enlarged toolbar instead of an image of the entire window. Is there a way to make sure I capture the enlarged window or maybe maximize all windows before I gather them?

Edit: I'd rather not maximize all the windows just to take screencaps if the user has some minimized.

Here is the relavent code:

    public static List<BitmapSource> RenderWindows()
    {
        var windows = Application.Current.Windows
                                         .OfType<Window>()
                                         .Where(x => x.GetType() != typeof(AskAQuestionDialog) & x.GetType() != typeof(SelectScreenShots));

        var bitmaps = new List<BitmapSource>();

        foreach (var window in windows)
        {
            var bitmap = new RenderTargetBitmap((int)window.Width, (int)window.Height, 96d, 96d, PixelFormats.Default);
            bitmap.Render(window);

            bitmaps.Add(bitmap);
        }

        return bitmaps;
    }

Upvotes: 0

Views: 186

Answers (1)

phil
phil

Reputation: 287

I use following function to get "screenshots" of all application windows (works also with minimized windows)

    /// <summary>
    /// Gets a JPG "screenshot" of the current UIElement
    /// </summary>
    /// <param name="source">UIElement to screenshot</param>
    /// <param name="scale">Scale to render the screenshot</param>
    /// <param name="quality">JPG Quality</param>
    /// <returns>Byte array of JPG data</returns>
    public static byte[] GetJpgImage(this UIElement source, double scale, int quality)
    {
        double actualHeight = source.RenderSize.Height;
        double actualWidth = source.RenderSize.Width;

        double renderHeight = actualHeight * scale;
        double renderWidth = actualWidth * scale;

        RenderTargetBitmap renderTarget = new RenderTargetBitmap((int)renderWidth, (int)renderHeight, 96, 96, PixelFormats.Pbgra32);
        VisualBrush sourceBrush = new VisualBrush(source);

        DrawingVisual drawingVisual = new DrawingVisual();
        DrawingContext drawingContext = drawingVisual.RenderOpen();

        using (drawingContext)
        {
            drawingContext.PushTransform(new ScaleTransform(scale, scale));
            drawingContext.DrawRectangle(sourceBrush, null, new Rect(new Point(0, 0), new Point(actualWidth, actualHeight)));
        }

        renderTarget.Render(drawingVisual);

        JpegBitmapEncoder jpgEncoder = new JpegBitmapEncoder();
        jpgEncoder.QualityLevel = quality;
        jpgEncoder.Frames.Add(BitmapFrame.Create(renderTarget));            

        byte[] imageArray;

        using (MemoryStream outputStream = new MemoryStream())
        {
            jpgEncoder.Save(outputStream);
            imageArray = outputStream.ToArray();
        }

        return imageArray;
    }

to get the image-list

        List<ImageSource> list = new List<ImageSource>();
        WindowCollection collection = Application.Current.Windows;
        TypeConverter tc = TypeDescriptor.GetConverter(typeof(ImageSource));
        for (int i = 0; i < collection.Count; i++)
        {
            byte[] imgBytes = ScreenShot.GetJpgImage(collection[i], 1, 90);
            ImageSource img = (ImageSource)tc.ConvertFrom(imgBytes);
            list.Add(img);
        }

Upvotes: 1

Related Questions