2

ISSUE
I am dealing with a system where images have been scanned in at different vertical and horizontal resolutions. The resolutions range from 96dpi to 300dpi.

My users need the ability to draw text onto these images. Using GDI+ it is easy to draw text onto images.

QUESTION
What I am having difficulty understanding, is how the user can draw text onto an image and the text drawn appears the same size, regardless of the images vertical and horizontal resolutions.

What I am seeing now, if I draw text using a 28pt arial font onto a 96ppi image the text will be smaller then if I draw text using a 28pt arial font onto a 300ppi image.

How can I take the images resolution into consideration to scale the font size when drawing text onto images?

I am drawing using the TextRenderer class.

TextRenderer.DrawText(graphics, this.txtComments.Text, font, startPoint, Color.Black);

UPDATE #1

Here is the code i'm using to initialize the font

System.Drawing.Font font = new System.Drawing.Font("Comic Sans MS", 28, FontStyle.Bold,  GraphicsUnit.Point)

I have tried using GraphicsUnit.Pixel and GraphicsUnit.Point, neither one make any difference for me.

UPDATE #2

I have tried using the Graphics.DrawString method and I get the same results.

Thanks,

RDotLee
  • 1,083
  • 2
  • 15
  • 32
  • If I've understood correctly, you may want to check out the [GraphicsUnit](http://msdn.microsoft.com/en-us/library/system.drawing.graphicsunit.aspx) enumeration - by specifying the graphics unit you can change this behavior you are seeing (for example, using Pixels the 28pt text will have the same dimension in pixels no matter the image) – VisualMelon Apr 16 '13 at 21:28
  • @VisualMelon, thanks for the comment. Sorry I didn't post my code for how the font is created, I will do that. I have tried specifying both Pixels and Points for the GraphicsUnit. I will update the post showing the font creation code. – RDotLee Apr 16 '13 at 21:34
  • looks as though it may be use of the TextRenderer class, see this old SO question: http://stackoverflow.com/questions/4556885/why-does-textrenderer-measuretext-not-measure-text-correctly-using-larger-fonts is there any reason you can't use [graphics.DrawString](http://msdn.microsoft.com/en-us/library/system.drawing.graphics.drawstring.aspx) ? Might be worth trying that, even if just for diagnostic purposes. – VisualMelon Apr 16 '13 at 21:43
  • @VisualMelon, thanks for the link. I have tried graphics.DrawString as well and it gives the same results. – RDotLee Apr 16 '13 at 21:45
  • 1
    I've not found anything useful, I don't think, but it seems that the GDI (which TextRenderer uses underneath) uses Inches underneath, and so TextRendere makes use of the [Font.SizeInPoints](http://msdn.microsoft.com/en-gb/library/system.drawing.font.sizeinpoints.aspx) property - how the font thinks it can know the point value when you tell it a number of pixels is interesting to say the least. I think it may be worth you going ahead and calculating the point value for each image, which shouldn't be hard. (point value = 72 * pixel value / dpi (I think)) – VisualMelon Apr 16 '13 at 22:27
  • 1
    In response to myself, it seems the image resolution is taken from a call to [Graphics.FromHDCInternal](http://msdn.microsoft.com/en-GB/library/system.drawing.graphics.fromhdcinternal.aspx) (static method) for the SizeInPoints calulation - this number seems to stay at 96dpi, but I've not done any rigorous testing. As said above, I think you should go with calculating Point sizes as you go along, then the Font won't have to assume the dimension of the image. – VisualMelon Apr 16 '13 at 22:50
  • @VisualMelon: Thanks for all your input. When I get into work tomorrow I will give some of these a try. – RDotLee Apr 17 '13 at 02:00

0 Answers0