Reputation: 53
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?
Upvotes: 1
Views: 1187
Reputation: 8143
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.
Upvotes: 2