Here is the test image where you can see the text does not size properly inside the rounded corner image on the top and bottom. The code I wrote is below.
Yes. Here is the sample code below that produces the image above. Please tell me how I can resize the text within the bounds of the rounded corner image on top and bottom. Any help is much appreciated.
Code: Select all
private void TextOnImage()
{
var image = new MagickImage(MagickColors.Red, 1000, 1000);
MagickImage imageresult;
MagickImage modimage;
try
{
using (var images = new MagickImageCollection())
{
int widthOfImage = GetProperFontSize(image);
var headerSnippetreadSettings = new MagickReadSettings()
{
BackgroundColor = MagickColors.LightBlue,
FontFamily = "Showcard Gothic",
FontPointsize = widthOfImage,
FillColor = MagickColors.Black,
FontWeight = FontWeight.Bold
};
// This will make the background of the label Khaki.
var readSettings = new MagickReadSettings()
{
BackgroundColor = MagickColors.LightBlue,
FontFamily = "Helvetica-Condensed-Light", // -font Helvetica-Condensed-Light
FontPointsize = widthOfImage, // -pointsize
FillColor = MagickColors.Black,
FontWeight = FontWeight.Bold
};
// Create the label image.
var branding = new MagickImage("label:MyDomain.com", readSettings);
// Create the label image.
var topheading = new MagickImage("label:Our Site Is Full of Great Cooking Tips", headerSnippetreadSettings);
// Create the label image.
var bottomheading = new MagickImage("label:Great Cooking Tips From Our Kitchen to Your Table", headerSnippetreadSettings);
branding.Extent(image.Width, 100, Gravity.Center);
topheading.Extent(image.Width, 100, Gravity.Center);
bottomheading.Extent(image.Width, 100, Gravity.Center);
// add the images to the collection
images.Add(topheading);
images.Add(image);
images.Add(bottomheading);
images.Add(branding);
// Append the images to create the output image.
using (var result = images.AppendVertically())
{
imageresult = new MagickImage(result);
// imageresult gets disposed of in the rounding code below, so need to write the imageresult to a new image
modimage = new MagickImage(imageresult);
}
}
using (imageresult)
{
using (var mask = new MagickImage(MagickColors.White, imageresult.Width, imageresult.Height))
{
var size = 150;
new ImageMagick.Drawables()
.FillColor(MagickColors.Black)
.StrokeColor(MagickColors.Black)
.Polygon(new PointD(0, 0), new PointD(0, size), new PointD(size, 0))
.Polygon(new PointD(mask.Width, 0), new PointD(mask.Width, size), new PointD(mask.Width - size, 0))
.Polygon(new PointD(0, mask.Height), new PointD(0, mask.Height - size), new PointD(size, mask.Height))
.Polygon(new PointD(mask.Width, mask.Height), new PointD(mask.Width, mask.Height - size), new PointD(mask.Width - size, mask.Height))
.FillColor(MagickColors.White)
.StrokeColor(MagickColors.White)
.Circle(size, size, size, 0)
.Circle(mask.Width - size, size, mask.Width - size, 0)
.Circle(size, mask.Height - size, 0, mask.Height - size)
.Circle(mask.Width - size, mask.Height - size, mask.Width - size, mask.Height)
// .BorderColor(MagickColors.Red)
.Draw(mask);
// This copies the pixels that were already transparent on the mask.
using (var imageAlpha = imageresult.Clone())
{
imageAlpha.Alpha(AlphaOption.Extract);
imageAlpha.Opaque(MagickColors.White, MagickColors.None);
mask.Composite(imageAlpha, CompositeOperator.Over);
}
mask.HasAlpha = false;
imageresult.HasAlpha = false;
imageresult.Composite(mask, CompositeOperator.CopyAlpha);
// imageresult gets disposed of so need to write the imageresult to a new image
modimage = new MagickImage(imageresult);
}
}
modimage.Write(@"D:\TestImage.png");
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private int GetProperFontSize(MagickImage img)
{
var width = img.Width;
if (width > 480 && width <= 680)
{
return 40;
}
if (width > 680 && width <= 800)
{
return 44;
}
if (width > 800 && width <= 1024)
{
return 52;
}
if (width > 1024 && width <= 1600)
{
return 64;
}
if (width > 1600 && width <= 2048)
{
return 70;
}
if (width > 2048 && width <= 2560)
{
return 86;;
}
if (width > 2560 && width <= 6000)
{
return 100;
}
return 16;
}