protected virtual Stream ConvertSvg(Stream svgInput, SvgMagicOptions options, bool failOnError = false) { if (svgInput == null || !svgInput.CanRead) { if (failOnError) { throw new SvgMagicException("Svg input stream was null or could not be read", HttpStatusCode.NotFound); } return(null); } var svg = SvgDocument.Open <SvgDocument>(svgInput); if (svg == null) { if (failOnError) { throw new SvgMagicException("Unable to open stream as Svg", HttpStatusCode.UnsupportedMediaType); } return(null); } if ((svg.Height.Type == SvgUnitType.Percentage) || (svg.Width.Type == SvgUnitType.Percentage)) { if (failOnError) { throw new SvgMagicException("Svg's with percentage based dimensions are not supported for fallback conversion", HttpStatusCode.UnsupportedMediaType); } return(null); } if (options.HasDimensions()) { svg.Height = options.Height; svg.Width = options.Width; } else if (options.Height > 0) { var aspectRatio = svg.Height.Value / svg.Width.Value; svg.Height = options.Height; svg.Width = options.Height / aspectRatio; } else if (options.Width > 0) { var aspectRatio = svg.Width / svg.Height; svg.Width = options.Width; svg.Height = options.Width / aspectRatio; } else { options.Height = float.Parse(svg.Height.Value.ToString()); options.Width = float.Parse(svg.Width.Value.ToString()); } var outputStream = new MemoryStream(); using (var bmp = svg.Draw()) { Thread.Sleep(50); switch (options.Format) { case SvgMagicImageFormat.Bmp: bmp.Save(outputStream, ImageFormat.Bmp); break; case SvgMagicImageFormat.Png: bmp.Save(outputStream, ImageFormat.Png); break; case SvgMagicImageFormat.Jpeg: bmp.Save(outputStream, ImageFormat.Jpeg); break; case SvgMagicImageFormat.Gif: bmp.Save(outputStream, ImageFormat.Gif); break; default: throw new SvgMagicException(string.Format("Unknown image format specified - {0}", options.Format), HttpStatusCode.UnsupportedMediaType); } } return(outputStream); }