示例#1
0
        public static BitmapSource Render(this PdfDocumentWrapper doc, int page, double factor, bool fixDpi = true)
        {
            var scale = DisplayDeviceHelper.GetCurrentScaleFactor();
            var dpiX  = fixDpi ? scale.Horizontal * DisplayDeviceHelper.DefaultDpi : 96;
            var dpiY  = fixDpi ? scale.Vertical * DisplayDeviceHelper.DefaultDpi : 96;

            Bitmap bitmap;

            lock (LockObj)
            {
                var size       = doc.PdfDocument.PageSizes[page];
                var realWidth  = (int)Math.Round(size.Width * scale.Horizontal * factor);
                var realHeight = (int)Math.Round(size.Height * scale.Vertical * factor);

                bitmap = doc.PdfDocument.Render(page, realWidth, realHeight, dpiX, dpiY, PdfRotation.Rotate0,
                                                PdfRenderFlags.LimitImageCacheSize | PdfRenderFlags.LcdText | PdfRenderFlags.Annotations |
                                                PdfRenderFlags.ForPrinting, true) as Bitmap;
            }

            var bs = bitmap?.ToBitmapSource();

            bitmap?.Dispose();

            bs?.Freeze();
            return(bs);
        }
示例#2
0
        public override Task <BitmapSource> GetRenderedFrame(int index)
        {
            var fullSize = Meta.GetSize();

            return(new Task <BitmapSource>(() =>
            {
                var settings = new MagickReadSettings
                {
                    BackgroundColor = MagickColors.None,
                    Defines = new DngReadDefines
                    {
                        OutputColor = DngOutputColor.SRGB,
                        UseCameraWhitebalance = true,
                        DisableAutoBrightness = false
                    }
                };

                try
                {
                    using (var mi = new MagickImage(Path.LocalPath, settings))
                    {
                        if (SettingHelper.Get("UseColorProfile", false, "QuickLook.Plugin.ImageViewer"))
                        {
                            if (mi.ColorSpace == ColorSpace.RGB || mi.ColorSpace == ColorSpace.sRGB || mi.ColorSpace == ColorSpace.scRGB)
                            {
                                mi.SetProfile(ColorProfile.SRGB);
                                if (ContextObject.ColorProfileName != null)
                                {
                                    mi.SetProfile(new ColorProfile(ContextObject.ColorProfileName)); // map to monitor color
                                }
                            }
                        }

                        mi.AutoOrient();

                        if (mi.Width != (int)fullSize.Width || mi.Height != (int)fullSize.Height)
                        {
                            mi.Resize((int)fullSize.Width, (int)fullSize.Height);
                        }

                        mi.Density = new Density(DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal,
                                                 DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical);

                        var img = mi.ToBitmapSourceWithDensity();

                        img.Freeze();
                        return img;
                    }
                }
                catch (Exception e)
                {
                    ProcessHelper.WriteLog(e.ToString());
                    return null;
                }
            }));
        }
示例#3
0
        private Icon GetTrayIconByDPI()
        {
            var scale = DisplayDeviceHelper.GetCurrentScaleFactor().Vertical;

            if (!App.IsWin10)
            {
                return(scale > 1 ? Resources.app : Resources.app_16);
            }

            return(OSThemeHelper.SystemUsesDarkTheme()
                ? (scale > 1 ? Resources.app_white : Resources.app_white_16)
                : (scale > 1 ? Resources.app_black : Resources.app_black_16));
        }
示例#4
0
        private Rect ResizeAndCentreNewWindow(Size size)
        {
            var desktopRect = WindowHelper.GetCurrentDesktopRectInPixel();
            var scale       = DisplayDeviceHelper.GetCurrentScaleFactor();
            var pxSize      = new Size(scale.Horizontal * size.Width, scale.Vertical * size.Height);

            var pxLocation = new Point(
                desktopRect.X + (desktopRect.Width - pxSize.Width) / 2,
                desktopRect.Y + (desktopRect.Height - pxSize.Height) / 2);

            // return absolute location and relative size
            return(new Rect(pxLocation, size));
        }
示例#5
0
        public static void DpiHack(BitmapSource img)
        {
            // a dirty hack... but is the fastest

            var newDpiX = (double)DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal;
            var newDpiY = (double)DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical;

            var dpiX = img.GetType().GetField("_dpiX",
                                              BindingFlags.NonPublic | BindingFlags.Instance);
            var dpiY = img.GetType().GetField("_dpiY",
                                              BindingFlags.NonPublic | BindingFlags.Instance);

            dpiX?.SetValue(img, newDpiX);
            dpiY?.SetValue(img, newDpiY);
        }
示例#6
0
        public GlassLayer()
        {
            InitializeComponent();

            var scale = DisplayDeviceHelper.GetCurrentScaleFactor();

            noiseBrush.Viewport = new Rect(new Size(
                                               noiseBrush.ImageSource.Width / scale.Horizontal,
                                               noiseBrush.ImageSource.Height / scale.Vertical));

            LayoutUpdated += (sender, e) =>
            {
                BindingOperations.GetBindingExpressionBase(brush, TileBrush.ViewboxProperty)?.UpdateTarget();
            };
        }
示例#7
0
        public GifProvider(Uri path, MetaProvider meta, ContextObject contextObject) : base(path, meta, contextObject)
        {
            if (!ImageAnimator.CanAnimate(Image.FromFile(path.LocalPath)))
            {
                _nativeProvider = new NativeProvider(path, meta, contextObject);
                return;
            }

            _fileHandle = (Bitmap)Image.FromFile(path.LocalPath);

            _fileHandle.SetResolution(DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Horizontal,
                                      DisplayDeviceHelper.DefaultDpi * DisplayDeviceHelper.GetCurrentScaleFactor().Vertical);

            Animator = new Int32AnimationUsingKeyFrames {
                RepeatBehavior = RepeatBehavior.Forever
            };
            Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(0, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(0))));
            Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(1, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(10))));
            Animator.KeyFrames.Add(new DiscreteInt32KeyFrame(2, KeyTime.FromTimeSpan(TimeSpan.FromMilliseconds(20))));
        }
示例#8
0
        private Rect ResizeAndCentreExistingWindow(Size size)
        {
            // align window just like in macOS ...
            //
            // |10%|    80%    |10%|
            // |---|-----------|---|---
            // |TL |     T     |TR |10%
            // |---|-----------|---|---
            // |   |           |   |
            // |L  |     C     | R |80%
            // |   |           |   |
            // |---|-----------|---|---
            // |LB |     B     |RB |10%
            // |---|-----------|---|---

            var scale = DisplayDeviceHelper.GetScaleFactorFromWindow(this);

            var limitPercentX = 0.1 * scale.Horizontal;
            var limitPercentY = 0.1 * scale.Vertical;

            // use absolute pixels for calculation
            var pxSize    = new Size(scale.Horizontal * size.Width, scale.Vertical * size.Height);
            var pxOldRect = this.GetWindowRectInPixel();

            // scale to new size, maintain centre
            var pxNewRect = Rect.Inflate(pxOldRect,
                                         (pxSize.Width - pxOldRect.Width) / 2,
                                         (pxSize.Height - pxOldRect.Height) / 2);

            var desktopRect = WindowHelper.GetDesktopRectFromWindowInPixel(this);

            var leftLimit   = desktopRect.Left + desktopRect.Width * limitPercentX;
            var rightLimit  = desktopRect.Right - desktopRect.Width * limitPercentX;
            var topLimit    = desktopRect.Top + desktopRect.Height * limitPercentY;
            var bottomLimit = desktopRect.Bottom - desktopRect.Height * limitPercentY;

            if (pxOldRect.Left < leftLimit && pxOldRect.Right < rightLimit) // L
            {
                pxNewRect.Location = new Point(Math.Max(pxOldRect.Left, desktopRect.Left), pxNewRect.Top);
            }
            else if (pxOldRect.Left > leftLimit && pxOldRect.Right > rightLimit) // R
            {
                pxNewRect.Location = new Point(Math.Min(pxOldRect.Right, desktopRect.Right) - pxNewRect.Width, pxNewRect.Top);
            }
            else // C, fix window boundary
            {
                pxNewRect.Offset(
                    Math.Max(0, desktopRect.Left - pxNewRect.Left) + Math.Min(0, desktopRect.Right - pxNewRect.Right), 0);
            }

            if (pxOldRect.Top < topLimit && pxOldRect.Bottom < bottomLimit) // T
            {
                pxNewRect.Location = new Point(pxNewRect.Left, Math.Max(pxOldRect.Top, desktopRect.Top));
            }
            else if (pxOldRect.Top > topLimit && pxOldRect.Bottom > bottomLimit) // B
            {
                pxNewRect.Location = new Point(pxNewRect.Left,
                                               Math.Min(pxOldRect.Bottom, desktopRect.Bottom) - pxNewRect.Height);
            }
            else // C, fix window boundary
            {
                pxNewRect.Offset(0,
                                 Math.Max(0, desktopRect.Top - pxNewRect.Top) + Math.Min(0, desktopRect.Bottom - pxNewRect.Bottom));
            }

            // return absolute location and relative size
            return(new Rect(pxNewRect.Location, size));
        }
示例#9
0
        internal void BeginShow(IViewer matchedPlugin, string path,
                                Action <string, ExceptionDispatchInfo> exceptionHandler)
        {
            _path  = path;
            Plugin = matchedPlugin;

            ContextObject.Reset();

            // assign monitor color profile
            ContextObject.ColorProfileName = DisplayDeviceHelper.GetMonitorColorProfileFromWindow(this);

            // get window size before showing it
            try
            {
                Plugin.Prepare(path, ContextObject);
            }
            catch (Exception e)
            {
                exceptionHandler(path, ExceptionDispatchInfo.Capture(e));
                return;
            }

            SetOpenWithButtonAndPath();

            // revert UI changes
            ContextObject.IsBusy = true;

            var newHeight = ContextObject.PreferredSize.Height + BorderThickness.Top + BorderThickness.Bottom +
                            (ContextObject.TitlebarOverlap ? 0 : windowCaptionContainer.Height);
            var newWidth = ContextObject.PreferredSize.Width + BorderThickness.Left + BorderThickness.Right;

            var newSize = new Size(newWidth, newHeight);

            // if use has adjusted the window size, keep it
            if (_customWindowSize != Size.Empty)
            {
                newSize = _customWindowSize;
            }
            else
            {
                _ignoreNextWindowSizeChange = true;
            }

            PositionWindow(newSize);

            if (Visibility != Visibility.Visible)
            {
                Dispatcher.BeginInvoke(new Action(() => this.BringToFront(Topmost)), DispatcherPriority.Render);
                Show();
            }

            //ShowWindowCaptionContainer(null, null);
            //WindowHelper.SetActivate(new WindowInteropHelper(this), ContextObject.CanFocus);

            // load plugin, do not block UI
            Dispatcher.BeginInvoke(new Action(() =>
            {
                try
                {
                    Plugin.View(path, ContextObject);
                }
                catch (Exception e)
                {
                    exceptionHandler(path, ExceptionDispatchInfo.Capture(e));
                }
            }),
                                   DispatcherPriority.Input);
        }