如何在C#中的图像上绘制字符串时根据图像大小调整文本大小 [英] How to Adjust the Text Size according to image size while Drawing String on an Image in C#

查看:247
本文介绍了如何在C#中的图像上绘制字符串时根据图像大小调整文本大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用图形类的Drawstring方法在图像上绘制文本。在绘制之前指定字体。

I'm using the Drawstring method of the Graphics Class to Draw a Text on an Image.The Font is Specified before drawing.

G.DrawString(mytext, font, brush, 0, 0)

问题出现在相同的文字绘制在尺寸较小的图像上时。绘制的文字看起来更大。我正在寻找解决方案来更改字体大小根据图像大小,以便在不同大小的图像上绘制时,文字不会变得更大或更小。

The Problem arises when the same text is drawn on an image with smaller size.The Text drawn appears to be larger.I'm looking for a solution to alter the font size according to the image size so that the text don't appear larger or smaller when drawn on images of different sizes.

我将不同的图像尺寸与其上绘制相同字体大小的文字。
http://i.stack.imgur.com/ZShUI.jpg

I'm attaching the images with different sizes with the text of same font size drawn on it. http://i.stack.imgur.com/ZShUI.jpg

http://i.stack。 imgur.com/GUfbM.jpg

我无法直接发布图片,因为我不允许。

I can't directly post the image because I'm not allowed.

推荐答案

通过在单独的图像上绘图,然后将该图像拍到原始图像上,您将获得最精确的缩放比例。你可以这样做:

You would get most precise scaling by drawing on separate image and then slapping that image onto original one. You'd do that as follows:


  1. 创建具有足够空间的内存位图

  2. 绘制
  3. 将包含文本的图像缩放至您需要的大小,以将原始图像中的图像绘制到原始图像上

代码:

Bitmap textBmp = new Bitmap(100, 100);
Graphics textBmpG = Graphics.FromImage(textBmp);
textBmpG .DrawString("test 1", new Font(FontFamily.GenericSansSerif, 16), Brushes.Red, new PointF(0, 0));
Graphics origImgG = Graphics.FromImage(originalImg);
origImgG.DrawImage(textBmpG, new Rectangle(50, 50, 50, 50), new Rectangle(0, 0, 100, 100), GraphicsUnit.Pixel);

注意最后一行和 Rectangle 参数。使用它们将文本位图缩放到原始图像上。或者,您还可以选择 Graphics.MeasureString 方法来确定文本的宽度,并尝试尝试,直到您找到最好的文本为止。

Take notice of last line and Rectangle parameters. Use them to scale your text bitmap onto original image. Alternatively, you can also choose Graphics.MeasureString method to determine how wide your text would be and make attempts until you get best one you can.


  1. 使用 Graphics.MeasureString ()来衡量您的字符串在图片上的大小。
  2. 相应地逐步减少/增加字体

  1. Use Graphics.MeasureString() to measure how big your string would be on the image
  2. Decrease/increase font step by step accordingly

正如你在评论中所要求的,我会在这里给你更详细的建议。假设您的原始图像宽度为WI1,并且使用 Graphics.MeasureString 的文本宽度为WT1。如果将图像调整为宽度WI2,那么您的完美文本宽度将为 WT2 = WT1 * WI2 / WI1 。使用 DrawText 方法可能无法获得这个确切的宽度,因为当您将字体增加1时,它可能跳过该值。所以你必须做几次尝试并找到最好的。如果生成的文字宽度较小(使用MeasureString测量),选择一种字体大小,将其增大,直到它变得比目标大,并且您有最接近的匹配。同样的事情,如果它太大。逐步减小字体。

As you requested in comment I'll give you more detailed suggestion here. Say your original image width is WI1, and width of text on it using Graphics.MeasureString is WT1. If you resize your image to width WI2, then your perfect text width would be WT2 = WT1 * WI2 / WI1. Using DrawText method you may not be able to get this exact width because when you increase font by 1 it may jump over that value. So you have to make several attempts and find best. Pick a size of font, if resulting text width is smaller (measure with MeasureString), increase it until it becomes bigger than target and you've got about closest match. Same thing goes if it's too big. Decrease font step by step.

这是非常快速和肮脏的,因为您看到很多绘图,但我想不出更好的解决方案,除非您使用等宽字体。

This is quick and dirty as you see, because you have many draws, but I can't think of better solution, unless you're using monospaced fonts.

这些解决方案之间的区别在于,您首先可以获得符合您需要的精确大小的文本,但由于缩放,您可能会失去一些字体可读性。第二种解决方案会提供良好的可读性,但不能获得像素完美的文本大小。

Difference between those solutions would be that in first you can get text to fit EXACT size you need, but you probably would loose some font readability due to scaling. Second solution would give good readability, but you can't get pixel perfect size of text.

这篇关于如何在C#中的图像上绘制字符串时根据图像大小调整文本大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆