Esempio n. 1
0
        /// <summary>
        /// Start the preview on the Control.
        /// </summary>
        /// <param name="dataSource">Stream reference to access source file.</param>
        public override void DoPreview <T>(T dataSource)
        {
            CleanupWebView2UserDataFolder();

            string svgData = null;
            bool   blocked = false;

            try
            {
                using (var stream = new ReadonlyStream(dataSource as IStream))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        svgData = reader.ReadToEnd();
                    }
                }

                blocked = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
            }
            catch (Exception ex)
            {
                PreviewError(ex, dataSource);
                return;
            }

            try
            {
                svgData = SvgPreviewHandlerHelper.AddStyleSVG(svgData);
            }
            catch (Exception ex)
            {
                PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError {
                    Message = ex.Message
                });
            }

            InvokeOnControlThread(() =>
            {
                try
                {
                    _infoBarAdded = false;

                    // Add a info bar on top of the Preview if any blocked element is present.
                    if (blocked)
                    {
                        _infoBarAdded = true;
                        AddTextBoxControl(Properties.Resource.BlockedElementInfoText);
                    }

                    AddWebViewControl(svgData);
                    Resize += FormResized;
                    base.DoPreview(dataSource);
                    PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewed());
                }
                catch (Exception ex)
                {
                    PreviewError(ex, dataSource);
                }
            });
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha)
        {
            phbmp    = IntPtr.Zero;
            pdwAlpha = WTS_ALPHATYPE.WTSAT_UNKNOWN;

            if (cx == 0 || cx > MaxThumbnailSize)
            {
                return;
            }

            string svgData = null;

            using (var stream = new ReadonlyStream(this.Stream as IStream))
            {
                using (var reader = new StreamReader(stream))
                {
                    svgData = reader.ReadToEnd();
                    try
                    {
                        // Fixes #17527 - Inkscape v1.1 swapped order of default and svg namespaces in svg file (default first, svg after).
                        // That resulted in parser being unable to parse it correctly and instead of svg, text was previewed.
                        // MS Edge and Firefox also couldn't preview svg files with mentioned order of namespaces definitions.
                        svgData = SvgPreviewHandlerHelper.SwapNamespaces(svgData);
                        svgData = SvgPreviewHandlerHelper.AddStyleSVG(svgData);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            if (svgData != null)
            {
                using (Bitmap thumbnail = GetThumbnail(svgData, cx))
                {
                    if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
                    {
                        phbmp    = thumbnail.GetHbitmap();
                        pdwAlpha = WTS_ALPHATYPE.WTSAT_RGB;
                    }
                }
            }
        }
Esempio n. 3
0
        /// <inheritdoc/>
        public void GetThumbnail(uint cx, out IntPtr phbmp, out WTS_ALPHATYPE pdwAlpha)
        {
            phbmp    = IntPtr.Zero;
            pdwAlpha = WTS_ALPHATYPE.WTSAT_UNKNOWN;

            if (cx == 0 || cx > MaxThumbnailSize)
            {
                return;
            }

            string svgData = null;

            using (var stream = new ReadonlyStream(this.Stream as IStream))
            {
                using (var reader = new StreamReader(stream))
                {
                    svgData = reader.ReadToEnd();
                    try
                    {
                        svgData = SvgPreviewHandlerHelper.AddStyleSVG(svgData);
                    }
                    catch (Exception)
                    {
                    }
                }
            }

            if (svgData != null)
            {
                using (Bitmap thumbnail = GetThumbnail(svgData, cx))
                {
                    if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0)
                    {
                        phbmp    = thumbnail.GetHbitmap();
                        pdwAlpha = WTS_ALPHATYPE.WTSAT_RGB;
                    }
                }
            }
        }
        /// <summary>
        /// Start the preview on the Control.
        /// </summary>
        /// <param name="dataSource">Stream reference to access source file.</param>
        public override void DoPreview <T>(T dataSource)
        {
            CleanupWebView2UserDataFolder();

            string svgData = null;
            bool   blocked = false;

            try
            {
                using (var stream = new ReadonlyStream(dataSource as IStream))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        svgData = reader.ReadToEnd();
                    }
                }

                blocked = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
            }
            catch (Exception ex)
            {
                PreviewError(ex, dataSource);
                return;
            }

            try
            {
                // Fixes #17527 - Inkscape v1.1 swapped order of default and svg namespaces in svg file (default first, svg after).
                // That resulted in parser being unable to parse it correctly and instead of svg, text was previewed.
                // MS Edge and Firefox also couldn't preview svg files with mentioned order of namespaces definitions.
                svgData = SvgPreviewHandlerHelper.SwapNamespaces(svgData);
                svgData = SvgPreviewHandlerHelper.AddStyleSVG(svgData);
            }
            catch (Exception ex)
            {
                PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError {
                    Message = ex.Message
                });
            }

            InvokeOnControlThread(() =>
            {
                try
                {
                    _infoBarAdded = false;

                    // Add a info bar on top of the Preview if any blocked element is present.
                    if (blocked)
                    {
                        _infoBarAdded = true;
                        AddTextBoxControl(Properties.Resource.BlockedElementInfoText);
                    }

                    AddWebViewControl(svgData);
                    Resize += FormResized;
                    base.DoPreview(dataSource);
                    PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewed());
                }
                catch (Exception ex)
                {
                    PreviewError(ex, dataSource);
                }
            });
        }
        /// <summary>
        /// Start the preview on the Control.
        /// </summary>
        /// <param name="dataSource">Stream reference to access source file.</param>
        public override void DoPreview <T>(T dataSource)
        {
            string svgData = null;
            bool   blocked = false;

            try
            {
                using (var stream = new ReadonlyStream(dataSource as IStream))
                {
                    using (var reader = new StreamReader(stream))
                    {
                        svgData = reader.ReadToEnd();
                    }
                }

                blocked = SvgPreviewHandlerHelper.CheckBlockedElements(svgData);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                PreviewError(ex, dataSource);
                return;
            }

            try
            {
                svgData = SvgPreviewHandlerHelper.AddStyleSVG(svgData);
            }
#pragma warning disable CA1031 // Do not catch general exception types
            catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
            {
                _browser.ScrollBarsEnabled = true;
                PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewError {
                    Message = ex.Message
                });
            }

            InvokeOnControlThread(() =>
            {
                try
                {
                    _infoBarAdded = false;

                    // Add a info bar on top of the Preview if any blocked element is present.
                    if (blocked)
                    {
                        _infoBarAdded = true;
                        AddTextBoxControl(Resource.BlockedElementInfoText);
                    }

                    AddBrowserControl(svgData);
                    Resize += FormResized;
                    base.DoPreview(dataSource);
                    PowerToysTelemetry.Log.WriteEvent(new SvgFilePreviewed());
                }
#pragma warning disable CA1031 // Do not catch general exception types
                catch (Exception ex)
#pragma warning restore CA1031 // Do not catch general exception types
                {
                    PreviewError(ex, dataSource);
                }
            });
        }