Solved

Font scaling between image resolutions.

Posted on 2008-06-18
4
740 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

743 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now