簡體   English   中英

使用Application.Current.Windows時獲取最大化的窗口?

[英]Get the maximized window when using Application.Current.Windows?

我有一些代碼收集與應用程序關聯的所有窗口,將它們渲染為位圖,並將它們存儲在List<BitmapSource>以供以后處理和處理。

我的問題是,如果將窗口最小化,則圖像只是一個放大的工具欄,而不是整個窗口的圖像。 有沒有一種方法可以確保在捕獲它們之前捕獲放大的窗口或最大化所有窗口?

編輯:如果用戶已將某些窗口最小化,我寧願不要最大化所有窗口只是為了截屏。

這是相關的代碼:

    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;
    }

我使用以下功能來獲取所有應用程序窗口的“屏幕截圖”(也適用於最小化的窗口)

    /// <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;
    }

獲取圖像列表

        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);
        }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM