Replies: 10 comments
-
Any news? |
Beta Was this translation helpful? Give feedback.
-
You can crop with
There is a way to crop an arbitrary location by specifying
svgDoc.ViewBox = new SvgViewBox()
{
MinX = 50,
MinY = 50,
Width = 100,
Height = 100,
};
svgDoc.Width = 100;
svgDoc.Height = 100;
var image = svgDoc.Draw(); |
Beta Was this translation helpful? Give feedback.
-
@H1Gdev But there is no auto-crop feature or any "crop to specific element" option, right? |
Beta Was this translation helpful? Give feedback.
-
You may be able to do so(use Graphics, SvgElement, etc.), but we don't provide it as method. |
Beta Was this translation helpful? Give feedback.
-
I see. Thank you so far. The viewbox solution works fine in my case. |
Beta Was this translation helpful? Give feedback.
-
FYI: Crop using using (var image = new Bitmap(100, 100))
{
using (var g = Graphics.FromImage(image))
{
g.TranslateTransform(-50, -50);
svgDoc.Draw(g);
}
} This has advantage of not changing the SVG DOM. |
Beta Was this translation helpful? Give feedback.
-
@H1Gdev Thank you very much. In my specific case, I created an image editor using your library to add annonations over photos. The photo is scaled to the user's screen size, but the original aspect ration remains. When rendering the whole thing as JPG, I want to crop the "letterbox" around the background image. /// <summary>Internal method to render the SVG as bitmap.</summary>
/// <param name="imgFormat">The rendered image as memory stream.</param>
/// <returns>A bitmap object containing the rendered image.</returns>
private Bitmap RenderAsBitmap(ImageFormat imgFormat = null) {
if (string.IsNullOrEmpty(Shape))
throw new InvalidOperationException("This method can only be executed, when the Shape parameter is set.");
// Replace the links with a Base64 representation
string svgSrc = Regex.Replace(
Shape,
@"(href="")((?>\/QCProcessing)?\/Diagnosis\/[^""]*)("")",
$"$1data:{MimeMapping.GetMimeMapping(Picture.OriginalFileName)};base64,{Convert.ToBase64String(Picture.Data)}$3"
);
// Create SVG object from source code
SvgDocument svg = SvgDocument.FromSvg<SvgDocument>(svgSrc);
// Create a Bitmap object from original picture for size comparison
using Bitmap originalBackgroundImage = GetImageFromByteArray(Picture.Data);
// Calculate aspect ratios
float originalAspectRatio = (float)originalBackgroundImage.Width / originalBackgroundImage.Height;
float currentAspectRatio = svg.Bounds.Width / svg.Bounds.Height;
// Calculate the actual width of the background image in context to the canvas (due to different aspect ratios of picture and screen)
float actualBgImgWidth = originalAspectRatio < currentAspectRatio ? svg.Bounds.Width / currentAspectRatio * originalAspectRatio : svg.Bounds.Width;
float actualBgImgHeight = originalAspectRatio < currentAspectRatio ? svg.Bounds.Height : svg.Bounds.Height / currentAspectRatio * originalAspectRatio;
// Crop image
svg.ViewBox = new SvgViewBox(
(svg.Bounds.Width - actualBgImgWidth) / 2,
(svg.Bounds.Height - actualBgImgHeight) / 2,
actualBgImgWidth,
actualBgImgHeight
);
svg.Width = actualBgImgWidth;
svg.Height = actualBgImgHeight;
// TODO: Scale image
//svg.Transforms ??= new Svg.Transforms.SvgTransformCollection();
//svg.Transforms.Add(new Svg.Transforms.SvgScale(
// originalBackgroundImage.Width / actualBgImgWidth,
// originalBackgroundImage.Height / actualBgImgHeight
//));
// Return a render of the image
return svg.Draw();
} |
Beta Was this translation helpful? Give feedback.
-
svg.Width = actualBgImgWidth;
svg.Height = actualBgImgHeight; I think you set the width and height to shrink here. |
Beta Was this translation helpful? Give feedback.
-
@H1Gdev So if I set this to a larger number, it would automatically rescale the whole image? |
Beta Was this translation helpful? Give feedback.
-
svgDoc.ViewBox = new SvgViewBox(
50,
50,
100,
100
);
svgDoc.Width = 120;
svgDoc.Height = 120;
svgDoc.ViewBox = new SvgViewBox(
50,
50,
100,
100
);
svgDoc.Width = 80;
svgDoc.Height = 80; |
Beta Was this translation helpful? Give feedback.
-
In Inkscape, you have the option to crop the canvas to either the outer dimensions of your drawing, or to your current selection of elements. Especially the first option would be really handy. Sometimes, my canvas is much bigger than the actual drawing, so an auto-crop function would solve many problems.
Is there already a way to achieve that?
Beta Was this translation helpful? Give feedback.
All reactions