How to Fit Graphics to Frames
Product Version | Product | Author |
---|---|---|
2021.1.118 | RadPdfProcessing | Martin Velikov |
Description
How to fit images to frames (predefined shapes, or in our case a square with a side length of 90) or vice versa.
Solution
This functionality could be achieved by using the FixedContentEditor API. Below are demonstrated the following four different scenarios:
- Fit Content Proportionally
- Fit Content To Frame (or Stretch fit)
- Fill Frame Proportionally (or Center fit)
- Fit Frame to Content
The following example demonstrates how to fit the image in a square without changing the image sides aspect ratio.
Fit Content Proportionally
double squareSide = 90;
FixedContentEditor editor = new FixedContentEditor(document.Pages.AddPage());
editor.DrawText("Fit Content Proportionally");
editor.Position.Translate(0, 20);
using (Stream sampleImage = File.OpenRead("image.jpg"))
{
Block block = new Block();
block.InsertImage(sampleImage);
Size imageSize = block.Measure();
using (editor.SavePosition())
{
double largestSide = imageSize.Width > imageSize.Height ? imageSize.Width : imageSize.Height;
double scaleFactor = squareSide / largestSide;
editor.Position.Scale(scaleFactor, scaleFactor);
editor.DrawBlock(block);
}
}
Fit Content To Frame
double squareSide = 90;
FixedContentEditor editor = new FixedContentEditor(document.Pages.AddPage());
editor.DrawText("Fit Content To Frame");
editor.Position.Translate(0, 20);
using (Stream sampleImage = File.OpenRead("image.jpg"))
{
Block block = new Block();
block.InsertImage(sampleImage, squareSide, squareSide);
editor.DrawBlock(block);
}
Fill Frame Proportionally
double squareSide = 90;
RadFixedPage page = document.Pages.AddPage();
FixedContentEditor editor = new FixedContentEditor(page);
editor.DrawText("Fill Frame Proportionally");
editor.Position.Translate(0, 20);
using (Stream sampleImage = File.OpenRead("image.jpg"))
{
Block block = new Block();
block.InsertImage(sampleImage);
Size imageSize = block.Measure();
using (editor.SavePosition())
{
double smallestSide = imageSize.Width < imageSize.Height ? imageSize.Width : imageSize.Height;
double scaleFactor = squareSide / smallestSide;
editor.Position.Scale(scaleFactor, scaleFactor);
editor.DrawBlock(block);
Image image = page.Content[page.Content.Count - 1] as Image;
Point clippingOffset = editor.Position.Matrix.Transform(new Point());
if (imageSize.Width == smallestSide)
{
clippingOffset.Offset(0, imageSize.Height * scaleFactor / 2 - squareSide / 2);
}
else
{
clippingOffset.Offset(imageSize.Width * scaleFactor / 2 - squareSide / 2, 0);
}
image.Clipping = new Clipping()
{
Clip = new RectangleGeometry(new Rect(clippingOffset.X, clippingOffset.Y, squareSide, squareSide)),
};
}
}
The following example demonstrates how to insert an image in its original size.
Fit Frame to Content
FixedContentEditor editor = new FixedContentEditor(document.Pages.AddPage());
editor.DrawText("Fit Frame To Content");
editor.Position.Translate(0, 20);
using (Stream sampleImage = File.OpenRead("image.jpg"))
{
Block block = new Block();
block.InsertImage(sampleImage);
Size imageSize = block.Measure();
editor.DrawBlock(block);
}