I'm trying to write a function that adds padding to an image but I'm not sure on how to do that with ImageMagick.
public bool AddPadding(string filePath)
{
// Read from file
using (MagickImage image = new MagickImage(filePath))
{
int imageSize;
int imageX = image.Width;
int imageY = image.Height;
if(imageX > imageY)
{
imageSize = imageX;
}
else
{
imageSize = imageY;
}
MagickGeometry size = new MagickGeometry(imageSize, imageSize);
// Probably do more stuff here?
// Save the result
image.Write(filePath);
}
}
What do I need to add so that the image is centered and whitespace is added to both left and right?
You can use the Extent
method of MagickImage
for this. Below is an example of how you could do that.
public bool AddPadding(string filePath)
{
// Read from file
using (MagickImage image = new MagickImage(filePath))
{
int imageSize = Math.Max(image.Width, image.Height);
// Add padding
image.Extent(imageSize, imageSize, Gravity.Center, MagickColors.Purple);
// Save the result
image.Write(filePath);
}
}
This adds a purple padding so you might want to change the color to a different one instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.