Esempio n. 1
0
        /// <summary>
        /// Converts an SVG document into a Texture2D.
        /// </summary>
        public static Texture2D SvgToTexture2D(SvgDocument svgImage, double width, double height,
                                               GraphicsDevice graphicsDevice, bool scaleSvg = true)
        {
            if (width < 1 || height < 1)
            {
                return(new Texture2D(graphicsDevice, 1, 1));
            }

            Bitmap image = new Bitmap((int)width, (int)height, PixelFormat.Format32bppArgb);

            svgImage.ShapeRendering = SvgShapeRendering.GeometricPrecision;
            using (ISvgRenderer r = SvgRenderer.FromImage(image))
            {
                if (scaleSvg)
                {
                    float scale = image.Height / svgImage.Height; // to make the svg fit the dimensions of the img.
                    r.ScaleTransform(scale, scale);
                }
                r.SmoothingMode = SmoothingMode.AntiAlias;
                svgImage.Draw(r);
            }

            int          bufferSize   = image.Height * image.Width * 4; // rgba = 4 bytes
            MemoryStream memoryStream = new MemoryStream(bufferSize);

            image.Save(memoryStream, ImageFormat.Png);
            //image.Save(@"d:\temp\" + Path.GetRandomFileName() + ".png", ImageFormat.Png);
            return(FromStreamPremultiplied(graphicsDevice, memoryStream));
        }
Esempio n. 2
0
        public static void RenderTo(this SvgDocument document, Bitmap bitmap)
        {
            try
            {
                using (var renderer = SvgRenderer.FromImage(bitmap))
                {
                    renderer.SetBoundable(new GenericBoundable(0, 0, bitmap.Width, bitmap.Height));

                    //EO, 2014-12-05: Requested to ensure proper zooming (draw the svg in the bitmap size, ==> proper scaling)
                    //EO, 2015-01-09, Added GetDimensions to use its returned size instead of this.Width and this.Height (request of Icarrere).
                    //BBN, 2015-07-29, it is unnecassary to call again GetDimensions and transform to 1x1
                    //JA, 2015-12-18, this is actually necessary to correctly render the Draw(rasterHeight, rasterWidth) overload, otherwise the rendered graphic doesn't scale correctly
                    var size = document.GetDimensions();
                    renderer.ScaleTransform(bitmap.Width / size.Width, bitmap.Height / size.Height);

                    //EO, 2014-12-05: Requested to ensure proper zooming out (reduce size). Otherwise it clip the image.
                    document.Overflow = SvgOverflow.Auto;

                    document.Draw(renderer);
                }
            }
            catch
            {
                throw;
            }
        }
Esempio n. 3
0
        static Bitmap RenderWholeSvgToBitmap(SvgDocument svg, Size size)
        {
            var bitmap      = new Bitmap(size.Width, size.Height);
            var svgRenderer = SvgRenderer.FromImage(bitmap);

            svgRenderer.SetBoundable(new GenericBoundable(0, 0, bitmap.Width, bitmap.Height));
            var svgDim = svg.GetDimensions();

            svgRenderer.ScaleTransform(bitmap.Width / svgDim.Width, bitmap.Height / svgDim.Height);

            svg.Draw(svgRenderer);

            return(bitmap);
        }
Esempio n. 4
0
        private Bitmap CreateSourceGraphic()
        {
            var graphic = new Bitmap((int)(_bounds.Width + 2 * _inflate * _bounds.Width + _bounds.X),
                                     (int)(_bounds.Height + 2 * _inflate * _bounds.Height + _bounds.Y));

            using (var renderer = SvgRenderer.FromImage(graphic))
            {
                renderer.SetBoundable(_renderer.GetBoundable());
                var transform = new Matrix();
                transform.Translate(_bounds.Width * _inflate, _bounds.Height * _inflate);
                renderer.Transform = transform;
                //renderer.Transform = _renderer.Transform;
                //renderer.Clip = _renderer.Clip;
                _renderMethod.Invoke(renderer);
            }
            return(graphic);
        }
Esempio n. 5
0
        static Bitmap RenderSvg(SvgDocument svgDoc, double scaleFactor, int oversampling)
        {
            var dimensions  = svgDoc.GetDimensions();
            var finalWidth  = (int)Math.Round(dimensions.Width * scaleFactor);
            var finalHeight = (int)Math.Round(dimensions.Height * scaleFactor);

            var renderWidth  = finalWidth * oversampling;
            var renderHeight = finalHeight * oversampling;

            var bitmap = new Bitmap(
                renderWidth,
                renderHeight,
                PixelFormat.Format32bppArgb);

            using (Graphics g = Graphics.FromImage(bitmap))
            {
                g.TextRenderingHint  = TextRenderingHint.AntiAlias;
                g.PixelOffsetMode    = PixelOffsetMode.Half;
                g.TextContrast       = 1;
                g.SmoothingMode      = SmoothingMode.None;
                g.CompositingQuality = CompositingQuality.HighQuality;
                g.InterpolationMode  = InterpolationMode.Default;
                using (var renderer = new SvgRendererWithNoSmoothControl(SvgRenderer.FromImage(bitmap)))
                {
                    renderer.SetBoundable(
                        new GenericBoundable(0.0f, 0.0f, bitmap.Width, bitmap.Height));
                    renderer.ScaleTransform(
                        bitmap.Width / dimensions.Width,
                        bitmap.Height / dimensions.Height,
                        MatrixOrder.Append);
                    svgDoc.Overflow = SvgOverflow.Auto;
                    svgDoc.RenderElement(renderer);
                }
            }

            return(bitmap);
        }