Ejemplo n.º 1
0
        public void Draw(Graphics gr, RectangleF rect, RectangleF source, ImageAttributes attributes = null)
        {
            if (IsSVG)
            {
                // largely based on function above now.  Not sure when this version is used to test it really works if source is changed
                EnsureSVG();
                GraphicsState state = gr.Save();
                using (ISvgRenderer render = SvgRenderer.FromGraphics(gr))
                {
                    render.TranslateTransform(rect.Left, rect.Top, MatrixOrder.Prepend);
                    render.ScaleTransform(rect.Width / source.Width, rect.Height / source.Height, MatrixOrder.Prepend);
                    render.TranslateTransform(-source.Left, -source.Top);
                    m_SVG.Draw(render);
                }
                gr.Restore(state);

                //RectangleF viewbox = m_SVG.ViewBox;
                //// we must prevent SVG applying any scaling as it's in the wrong sequence, and must be BEFORE the source adjustments
                //// therefore we perform the output scale now: (and below m_SVG.Draw uses the viewbox size, so will apply a (1,1) scaling)
                //// I think it's correct that this uses viewbox not bound (as above function) as we are implementing the SVG scaling for the viewbox ourselves now
                //gr.ScaleTransform(rect.Width / viewbox.Width, rect.Height / viewbox.Height);
                //gr.ScaleTransform(viewbox.Width / source.Width, viewbox.Height / source.Height);
                //gr.TranslateTransform(-(source.X - viewbox.X), -(source.Y - viewbox.Y));
                //m_SVG.Draw(gr, new SizeF(viewbox.Width, viewbox.Height));
                //gr.DrawRectangle(Pens.LightBlue, viewbox.X, viewbox.Y, viewbox.Width, viewbox.Height);
                //gr.Restore(state);
                ////gr.DrawRectangle(Pens.Salmon, rect.X, rect.Y, rect.Width, rect.Height);
            }
            else
            {
                gr.DrawImage(GetNetImage(), rect, source, attributes);
            }
        }
Ejemplo n.º 2
0
 public void RenderRectangle(Graphics gr, SvgDocument document)
 {
     using (ISvgRenderer renderer = SvgRenderer.FromGraphics(gr))
     {
         document.RenderElement(renderer);
     }
 }
Ejemplo n.º 3
0
        private void btnBegin_Click(object sender, EventArgs e)
        {
            //清空文本,不能给text赋值,否则颜色设置会出现问题
            rtbInfo.Clear();
            //检测路径合法性
            if (!Directory.Exists(tbInput.Text) || !Directory.Exists(tbOutput.Text))
            {
                rtbInfo.SelectionColor = Color.Red;
                rtbInfo.AppendText("输入路径或输出路径不存在\n");
                return;
            }
            //准备转换工作
            btnBegin.Enabled       = false;
            rtbInfo.SelectionColor = Color.Green;
            rtbInfo.AppendText("开始转换,如无特殊需要请勿修改参数...\n");
            string[] svgs = Directory.GetFiles(tbInput.Text, "*.svg");
            rtbInfo.SelectionColor = Color.Black;
            rtbInfo.AppendText("共读取到" + svgs.Length + "个svg文件\n");
            int successCount = 0;

            //开始每一次转换
            for (int i = 0; i < svgs.Length; i++)
            {
                Bitmap   bitmap   = new Bitmap((int)nudWidth.Value, (int)nudHeight.Value);
                Graphics graphics = Graphics.FromImage(bitmap);
                //可能会出现路径问题或其他错误,记录成功个数
                try
                {
                    SvgDocument  svgDocument = SvgDocument.Open(svgs[i]);
                    ISvgRenderer renderer    = SvgRenderer.FromGraphics(graphics);
                    svgDocument.Width  = (int)nudWidth.Value;
                    svgDocument.Height = (int)nudHeight.Value;
                    svgDocument.Draw(renderer);
                    bitmap.Save(tbOutput.Text + "\\" + Path.GetFileNameWithoutExtension(svgs[i]) + "." + cbFormat.Text);//怕读取顺序与原顺序不一样,所以按照原名字保存
                    successCount++;
                    rtbInfo.AppendText("第" + (i + 1) + "个转换成功\n");
                }
                catch
                {
                    rtbInfo.SelectionColor = Color.Red;
                    rtbInfo.AppendText("第" + (i + 1) + "个转换失败");
                    rtbInfo.SelectionColor = Color.Black;
                }
            }
            //转换完成,完成善后工作
            rtbInfo.SelectionColor = Color.Green;
            rtbInfo.AppendText("转换完成,共" + svgs.Length + "个文件," + successCount + "个转换成功");
            btnBegin.Enabled = true;
        }
Ejemplo n.º 4
0
        protected Bitmap PaintChart(DevExpress.XtraCharts.Native.Chart chart,
                                    int width = 2000, int height = 1200, int?dpi = null)
        {
            if (_Chart.Diagram != null)
            {
                SetupDiagram(_Chart.Diagram);
            }

            if (dpi == null || dpi == 0)
            {
                dpi = DefaultDPI;
            }

            float scaleBitmap = dpi.Value / 300f;
            float scaleSVG    = scaleBitmap * 96f / 300f;

            var svg = chart.CreateSvg(new Size((int)Math.Ceiling(width * scaleSVG), (int)Math.Ceiling(height * scaleSVG)));

            var doc = SvgDocument.Open(svg);

            doc.ShapeRendering = SvgShapeRendering.GeometricPrecision;

            var bitmap = new Bitmap((int)Math.Ceiling(width * scaleBitmap), (int)Math.Ceiling(height * scaleBitmap));

            bitmap.SetResolution(dpi.Value, dpi.Value);

            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                graphics.SmoothingMode     = SmoothingMode.HighQuality;
                graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                graphics.Clear(Color.White);

                var renderer = SvgRenderer.FromGraphics(graphics);
                renderer.SmoothingMode = SmoothingMode.HighQuality;
                if (scaleSVG != 1f && scaleSVG > 0f)
                {
                    renderer.ScaleTransform(1 / scaleSVG, 1 / scaleSVG);
                }

                doc.Draw(renderer);
            }

            return(bitmap);
        }
Ejemplo n.º 5
0
        protected virtual Bitmap PaintSankey(SankeyDiagramHostControl sankey, int?dpi = null)
        {
            if (dpi == null || dpi == 0)
            {
                dpi = DefaultDPI;
            }

            float scaleBitmap = dpi.Value / 300f;
            float scaleSVG    = 1f;

            using var streamSvg = new MemoryStream();
            sankey.ExportToSvg(streamSvg);
            streamSvg.Seek(0, SeekOrigin.Begin);

            var doc = SvgDocument.Open <SvgDocument>(streamSvg);

            doc.ShapeRendering = SvgShapeRendering.GeometricPrecision;

            streamSvg.Close();

            var bitmap = new Bitmap((int)Math.Ceiling(sankey.Width * scaleBitmap), (int)Math.Ceiling(sankey.Height * scaleBitmap));

            bitmap.SetResolution(dpi.Value, dpi.Value);

            using (var graphics = Graphics.FromImage(bitmap))
            {
                graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                graphics.PixelOffsetMode   = PixelOffsetMode.HighQuality;
                graphics.SmoothingMode     = SmoothingMode.HighQuality;
                graphics.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;

                graphics.Clear(Color.White);

                var renderer = SvgRenderer.FromGraphics(graphics);
                renderer.SmoothingMode = SmoothingMode.HighQuality;
                if (scaleSVG != 1f && scaleSVG > 0f)
                {
                    renderer.ScaleTransform(1 / scaleSVG, 1 / scaleSVG);
                }

                doc.Draw(renderer);
            }

            return(bitmap);
        }
Ejemplo n.º 6
0
        public void TestSmoothingModeRestoreForGraphics()
        {
            var visualElementMock = new Mock <SvgVisualElement>();

            visualElementMock.Setup(_ => _.Attributes).CallBase();

            var visualElement = visualElementMock.Object;

            visualElement.ShapeRendering = SvgShapeRendering.Auto;

            var g        = Graphics.FromHwnd(IntPtr.Zero);
            var renderer = SvgRenderer.FromGraphics(g);

            g.SmoothingMode = SmoothingMode.AntiAlias;

            visualElement.RenderElement(renderer);

            Assert.That(g.SmoothingMode, Is.EqualTo(SmoothingMode.AntiAlias));
        }
Ejemplo n.º 7
0
        private Bitmap CreateSourceGraphic(SvgVisualElement element, SvgRenderer renderer)
        {
            if (this.sourceGraphic == null)
            {
                RectangleF bounds = element.Path.GetBounds();
                this.sourceGraphic = new Bitmap((int)bounds.Width, (int)bounds.Height);

                using (var graphics = Graphics.FromImage(this.sourceGraphic))
                {
                    graphics.Clip      = renderer.Clip;
                    graphics.Transform = renderer.Transform;

                    element.RenderElement(SvgRenderer.FromGraphics(graphics));

                    graphics.Save();
                }
            }

            return(this.sourceGraphic);
        }
Ejemplo n.º 8
0
 public void Draw(Graphics gr, RectangleF rect, ImageAttributes attributes = null)
 {
     if (IsSVG)
     {
         //EnsureSVG();  // either of these following lines can be used to test function below
         //Draw(gr, rect, new RectangleF(0, 0, m_SVG.Width, m_SVG.Height), attributes);
         //Draw(gr, new PointF[] { rect.Location, rect.TopRight(), rect.BottomLeft() }, new RectangleF(0, 0, m_SVG.Width, m_SVG.Height), attributes);
         //return;
         var state = gr.Save();
         using (ISvgRenderer render = SvgRenderer.FromGraphics(gr))
         {
             //render.Transform = gr.Transform; - redundant as it does this anyway.  Doing this ...Clone might be useful so the original matrix didn't get corrupted, but safer to just store entire state anyway
             render.TranslateTransform(rect.Left, rect.Top, MatrixOrder.Prepend);
             render.ScaleTransform(rect.Width / m_SVG.Width, rect.Height / m_SVG.Height, MatrixOrder.Prepend);
             m_SVG.Draw(render);
         }
         gr.Restore(state);
     }
     else
     {
         gr.DrawImage(GetNetImage(), rect, attributes);
     }
 }