Quantcast
Channel: magick Discussions Rss Feed
Viewing all articles
Browse latest Browse all 3693

New Post: How to implement "best-fit" supplied text in a given region in image

$
0
0
You could use the FontTypeMetrics method of MagickImage to calculate the size of text you are drawing. Below is a simple example for when you are trying to write a single word:
string word = "Word!";

using (MagickImage image = new MagickImage(Color.Yellow, 100, 100))
{
  image.FillColor = Color.Purple;
  image.Font = "Garamond";
  image.Density = new MagickGeometry(72, 72);
  
  image.FontPointsize = 1;
  TypeMetric typeMetric = image.FontTypeMetrics(word);
  while (typeMetric.TextWidth < image.Width)
  {
    image.FontPointsize++;
    typeMetric = image.FontTypeMetrics(word);
  }
  image.FontPointsize--;
  
  image.Annotate(word, Gravity.Center);
  image.Write("output.png");
}

Viewing all articles
Browse latest Browse all 3693

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>