public virtual Bitmap ConvertToBitmap([NotNull] SvgImage svgImage, [NotNull] Matrix sourceMatrix, [NotNull] Matrix viewMatrix, int sourceAlignmentWidth, int sourceAlignmentHeight) { if (svgImage == null) { throw new ArgumentNullException(nameof(svgImage)); } if (sourceMatrix == null) { throw new ArgumentNullException(nameof(sourceMatrix)); } if (viewMatrix == null) { throw new ArgumentNullException(nameof(viewMatrix)); } Bitmap result; if (svgImage.GetImage() is Image image) { using (image) { var sourceRatio = (float)sourceAlignmentWidth / sourceAlignmentHeight; var destinationRatio = (float)image.Width / image.Height; // TODO find a good TOLERANCE if (Math.Abs(sourceRatio - destinationRatio) < 0.5f) { result = new Bitmap(image, sourceAlignmentWidth, sourceAlignmentHeight); } else { int destinationWidth; int destinationHeight; if (sourceRatio < destinationRatio) { destinationWidth = sourceAlignmentWidth; destinationHeight = (int)(sourceAlignmentWidth / destinationRatio); } else { destinationWidth = (int)(sourceAlignmentHeight * destinationRatio); destinationHeight = sourceAlignmentHeight; } var x = (sourceAlignmentWidth - destinationWidth) / 2; var y = (sourceAlignmentHeight - destinationHeight) / 2; result = new Bitmap(sourceAlignmentWidth, sourceAlignmentHeight); using (var graphics = Graphics.FromImage(result)) { var rect = new Rectangle(x, y, destinationWidth, destinationHeight); graphics.DrawImage(image, rect); } } var rotateFlipType = (RotateFlipType)this.GetRotationSector(sourceMatrix, viewMatrix); result.RotateFlip(rotateFlipType); } } else { result = null; } return(result); }