コード例 #1
0
 private void InvalidateAndRender()
 {
     try
     {
         renderer.Render(window.Document as SvgDocument);
     }
     catch (Exception e)
     {
         MessageBox.Show("An exception occurred while rendering: " + e.Message);
     }
 }
コード例 #2
0
ファイル: SvgPictureBox.cs プロジェクト: zellus/SharpVectors
        private void InvalidateAndRender()
        {
            try
            {
                if (_svgRenderer != null)
                {
                    SvgSvgElement svgEl = (SvgSvgElement)_svgWindow.Document.RootElement;

                    SvgSizeF r = svgEl.GetSize();

                    int winWidth  = (int)svgEl.Width.BaseVal.Value;
                    int winHeight = (int)svgEl.Height.BaseVal.Value;

                    if (!r.Width.Equals(0.0) && !r.Height.Equals(0.0) && (r.Width * 4 * r.Height) < BitmapLimit)
                    {
                        winWidth  = (int)r.Width;
                        winHeight = (int)r.Height;
                    }
                    if ((winWidth * 4 * winHeight) >= BitmapLimit)
                    {
                        winWidth  = this.Width;
                        winHeight = this.Height;
                    }

                    _svgWindow.Resize(winWidth, winHeight);

                    _svgRenderer.Render(_svgWindow.Document as SvgDocument);
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                if (this.DesignMode)
                {
                    return;
                }

                var errorArgs = new SvgErrorArgs("An exception occurred while rendering", ex);
                _svgErrors?.Invoke(this, errorArgs);
                if (!errorArgs.Handled)
                {
                    MessageBox.Show(errorArgs.Message + ": " + ex.Message,
                                    _appTitle, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// The following code will load the SVGDOM, render to a emf, and
        /// place the EMF on the clipboard. Kt seems more complicated than it should
        /// see http://www.dotnet247.com/247reference/msgs/23/118514.aspx
        /// for why the straight forward solution does not work.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void miCopy_Click(object sender, EventArgs e)
        {
            if (svgUrlCombo.Text.Length > 0)
            {
                int width  = 500;
                int height = 500;

                // make SVG document with associated window so we can render it.
                SvgWindow           window   = new SvgPictureBoxWindow(width, height, null);
                GdiGraphicsRenderer renderer = new GdiGraphicsRenderer();
                renderer.Window = window;
                window.Renderer = renderer;
                window.Source   = svgUrlCombo.Text;

                // copy and paste code taken from
                // http://www.dotnet247.com/247reference/msgs/23/117611.aspx
                // putting a plain MetaFile on the clipboard does not work.
                // .Net's metafile format is not recognised by most programs.
                Graphics g   = CreateGraphics();
                IntPtr   hdc = g.GetHdc();
                Metafile m   = new Metafile(hdc, EmfType.EmfOnly);
                g.ReleaseHdc(hdc);
                g.Dispose();
                g = Graphics.FromImage(m);

                // draw the SVG here
                // NOTE: the graphics object is automatically Disposed by the
                // GdiRenderer.
                renderer.Graphics = g;
                renderer.Render(window.Document as SvgDocument);
                //window.Render();

                // put meta file on the clipboard.
                IntPtr hwnd = this.Handle;
                if (Win32.OpenClipboard(hwnd))
                {
                    Win32.EmptyClipboard();
                    IntPtr hemf = m.GetHenhmetafile();
                    Win32.SetClipboardData(14, hemf); //CF_ENHMETAFILE=14
                    Win32.CloseClipboard();
                }
            }
        }
コード例 #4
0
        public bool LoadDocument(string documentFilePath, SvgTestInfo testInfo, object extraInfo)
        {
            this.UnloadDocument();

            if (extraInfo == null)
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(documentFilePath) || File.Exists(documentFilePath) == false)
            {
                return(false);
            }

            var pngFilePath = extraInfo.ToString();

            if (string.IsNullOrWhiteSpace(pngFilePath) || File.Exists(pngFilePath) == false)
            {
                return(false);
            }

            Image pngImage = null;

            try
            {
                pngImage = Image.FromFile(pngFilePath);
                if (pngImage == null)
                {
                    return(false);
                }
                viewerExpected.Image  = pngImage;
                viewerExpected.Width  = pngImage.Width + 10;
                viewerExpected.Height = pngImage.Height + 10;
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                return(false);
            }

            try
            {
                _svgWindow        = new SvgPictureBoxWindow(pngImage.Width, pngImage.Height, _svgRenderer);
                _svgWindow.Source = documentFilePath;

                var svgImage = new Bitmap(pngImage.Width, pngImage.Height);

                using (var graWrapper = GdiGraphicsWrapper.FromImage(svgImage, true))
                {
                    _svgRenderer.GraphicsWrapper = graWrapper;
                    _svgRenderer.Render(_svgWindow.Document);
                    _svgRenderer.GraphicsWrapper = null;
                }

                viewerConverted.Image  = svgImage;
                viewerConverted.Width  = svgImage.Width + 10;
                viewerConverted.Height = svgImage.Height + 10;
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                return(false);
            }

            return(true);
        }
コード例 #5
0
ファイル: TestDockPanel.cs プロジェクト: zellus/SharpVectors
        public bool LoadDocument(string documentFilePath, SvgTestInfo testInfo, object extraInfo)
        {
            this.UnloadDocument();

            if (extraInfo == null)
            {
                return(false);
            }
            if (string.IsNullOrWhiteSpace(documentFilePath) || File.Exists(documentFilePath) == false)
            {
                return(false);
            }

            var pngFilePath = extraInfo.ToString();

            if (string.IsNullOrWhiteSpace(pngFilePath) || File.Exists(pngFilePath) == false)
            {
                return(false);
            }

            Image pngImage = null;

            try
            {
                pngImage = Image.FromFile(pngFilePath);
                if (pngImage == null)
                {
                    return(false);
                }
                viewerExpected.Image  = pngImage;
                viewerExpected.Width  = pngImage.Width + 10;
                viewerExpected.Height = pngImage.Height + 10;
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                return(false);
            }

            try
            {
                using (var svgRenderer = new GdiGraphicsRenderer(pngImage.Width, pngImage.Height))
                {
                    var svgWindow = svgRenderer.Window;

                    svgWindow.Source = documentFilePath;
                    svgRenderer.Render(svgWindow.Document);

                    var svgImage = svgRenderer.RasterImage;

                    viewerConverted.Image  = svgImage;
                    viewerConverted.Width  = svgImage.Width + 10;
                    viewerConverted.Height = svgImage.Height + 10;
                }
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());

                return(false);
            }

            return(true);
        }