Solved

Font scaling between image resolutions.

Posted on 2008-06-18
4
743 Views
Last Modified: 2013-12-16
Hi All,

I've been tinkering with this for a while now and I think my brain has finally melted so hopefully somebody can help clear things up for me.

I have a photo app that manipulates images that are 2540x1778 @ 254ppi. For obvious reasons, when manipulating the image in the application we use a scaled down version (currently 500x349 @ 96ppi). The trouble I am currently having is upscaling text from the smaller image back to the full size image when it comes to saving, particularly taking into account the difference in resolutions.

Hopefully somebody can make this simple for me, or at least point me in the right direction.
0
Comment
Question by:Carl Tawn
  • 2
  • 2
4 Comments
 
LVL 6

Expert Comment

by:Bruce_1975
ID: 21812417
Hi Carl,
The position of the text is not affected by the resolution. You just have to scale it by the size of the image.
The size of the text is affected by the scale. So you have to scale the size of the used font. I have not tested the snipped but it should work.

Regards,
Bruce
private void DrawString(Graphics G, int PreviewResolution, int OriginalResolution, Font TextFont, string Text, Brush TextBrush, PointF Position)
{
    float scale = (float)OriginalResolution / (float)PreviewResolution;
    Font font = new Font(TextFont.Name, TextFont.Size * scale);
    G.DrawString(Text, font, TextBrush, Position);
}

Open in new window

0
 
LVL 52

Author Comment

by:Carl Tawn
ID: 21812659
Hi Bruce,

Thanks for your reply. I'm already doing something pretty much the same as that, but its not 100% right, the up-scaled text is slightly off and I can't quite suss out why. I am currently using a transparent label for the text (probably not the best way as its a bit flickery, any ideas on a better method would be handy!) so i'm not sure if i'm just confusing myself when it comes to which values (i.e. label size/clientsize or text length) i'm supposed to be using as the basis of the scaling.

The label and, from the looks of it DrawString(), seem to force a slight margin between where the text actually sits in relation to the point where I tell it to draw. Maybe this is throwing off my calculations too.
0
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 500 total points
ID: 21813031
I can't see the discribed behavior if I draw it directly to the image.
May be I this is caused by the smaller image I used or there is an issue with the transparent label (Text offset depending on the size or something like that.)

... I played around with Graphics.DrawString and Label... they have different draw methods for the same font. The scaling between images goes right but there is a slight difference in line thickness and character with.
Because of this I think the drawing method of the label is the problem and you have to draw the string with Graphics :o(

Regards,
Bruce

BTW:  change the scaling in the snipped to
float scale = (float)PreviewResolution / (float)OriginalResolution;
0
 
LVL 52

Author Comment

by:Carl Tawn
ID: 21822088
Finally its working!! I switched from using a label to a picturebox with the text drawn in it so that it would render the same on both the preview and full size image. I had to do a little bit of tinkering with the StringFormat object to get it to play ball, but at least its sorted now.

Thanks for the help Bruce, much appreciated.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question