473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

DrawString on to an image (text on an image)

I would like to add text to an image. I have tried to use DrawString and it
works on some images but on others it is very very small. I am pretty sure
it has something to do with the size of the image but I have had varying
results on multiple images.

Is there anyway to make the text a fixed size on the image. Similar to
putting a date on a photograph.
Dec 15 '06 #1
3 2487
Hello JR1,
>I would like to add text to an image. I have tried to use DrawString and
it
works on some images but on others it is very very small. I am pretty sure
it has something to do with the size of the image but I have had varying
results on multiple images.
Sure it has to do with the size of the image... you don't mention what
"varying results" you have seen.
>Is there anyway to make the text a fixed size on the image. Similar to
putting a date on a photograph.
I believe (hard to say for sure without seeing your code) your problem is
that actually the text is a fixed size right now. What you'd need to do is
calculate the font size you use in relation to the image size. But it
really also depends on how you look at the image in the end...

Let me try to explain. Consider for a moment an image that's 100 pixels
high. If you draw some text on it that has a font height of 10 pixels,
that text would obviously take up 10% of the image's height. Assuming
you'd display that image somewhere at a 1:1 translation, meaning 1 pixel
in the image would take up exactly 1 pixel on screen, that might just look
alright, even though the 10 pixel height of the text would be pretty small.

Now let's assume you might be looking at a photo with a pretty high
resolution. This might easily be a few thousand pixels high - let's assume
3000 for the example. If you'd draw a 10 pixel high line of text on that
photo and display the image at a 1:1 translation on screen, the 10 pixel
high text would still be 10 pixels high, right? Should appear exactly the
same size as before. The real trouble is that you probably wouldn't want
to display the image on screen at that 1:1 translation, because on an
average screen of around 1000 pixels height, you'd see only a third of
that image. So maybe you'd use a 1:3 translation, effectively scaling the
image down to a third of its height to display it on screen completely.
Now the problem with that would be that your text, having been drawn in 10
pixels height originally, would be down to 3 or 4 pixels after the
scaling, rendering it unreadable.

There's no one solution to that problem that always works. For instance,
you could decide to render the text at 30 pixels height, if you knew that
a display translation of 1:3 would be used. But that could be wrong if
somebody wanted to use a 1:4 translation on an even smaller display, so
you'd have to calculate that at the point where you know what the
translation is going to be.

The other option would be to calculate your text height as a proportion of
the image height - in my first example, we were at 10%, so for the second
example, you'd arrive at 300 pixels using that approach! Of course the
text would now look a lot bigger in the second image, even with the 1:3
translation. You'd have to use a 10:1 translation for the first image,
scaling it up to the whole screen height, to get the text size to be the
same for comparison purposes. But the mathematics work - in both cases
you'd end up displaying a 100 pixel high string on screen.

I hope I haven't confused you completely now.... the gist of the whole
story is that you can make the decision about the font size at various
different points, and there is no "correct" point where that decision
should be made. It depends on the purpose of your application, and doing
it one way may always have drawbacks when looking from a different
perspective.
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 16 '06 #2
Thank you very much for your detailed response! In my particular case the
images are all viewed at approximately the same size, regarless of what the
resolution of the actual image was taken at. So that being said, if I have
an image and I know that I will be viewing it at a resolution of A x B (which
could be the native resolution or a 1 : 3 transalation) how do I determine:
1st the scale at which I should draw the font and 2nd what function should be
called to set it.
Here is a sample of the code I am using now:

RasterImageGdiP lusGraphicsCont ainer container =
viewer.Image.Cr eateGdiPlusGrap hics();
container.Graph ics.SmoothingMo de = SmoothingMode.H ighQuality;
Brush bru = new SolidBrush(Colo r.OrangeRed);
Font font1 = new Font(FontFamily .GenericSansSer if, 12,
FontStyle.Bold) ;
PointF point1 = new PointF(1, 1);
container.Graph ics.DrawString( "01/01/2006", font1, bru, 1, 1);
viewer.Refresh( );

When I change the font size it does nothing on the image. Is there a
function that you know of that will give me the current resolution of the
image and then I can calculate it from there. Then how do I go about writing
the text at that scale.

Thanks again for your help!!


"Oliver Sturm" wrote:
Hello JR1,
I would like to add text to an image. I have tried to use DrawString and
it
works on some images but on others it is very very small. I am pretty sure
it has something to do with the size of the image but I have had varying
results on multiple images.

Sure it has to do with the size of the image... you don't mention what
"varying results" you have seen.
Is there anyway to make the text a fixed size on the image. Similar to
putting a date on a photograph.

I believe (hard to say for sure without seeing your code) your problem is
that actually the text is a fixed size right now. What you'd need to do is
calculate the font size you use in relation to the image size. But it
really also depends on how you look at the image in the end...

Let me try to explain. Consider for a moment an image that's 100 pixels
high. If you draw some text on it that has a font height of 10 pixels,
that text would obviously take up 10% of the image's height. Assuming
you'd display that image somewhere at a 1:1 translation, meaning 1 pixel
in the image would take up exactly 1 pixel on screen, that might just look
alright, even though the 10 pixel height of the text would be pretty small.

Now let's assume you might be looking at a photo with a pretty high
resolution. This might easily be a few thousand pixels high - let's assume
3000 for the example. If you'd draw a 10 pixel high line of text on that
photo and display the image at a 1:1 translation on screen, the 10 pixel
high text would still be 10 pixels high, right? Should appear exactly the
same size as before. The real trouble is that you probably wouldn't want
to display the image on screen at that 1:1 translation, because on an
average screen of around 1000 pixels height, you'd see only a third of
that image. So maybe you'd use a 1:3 translation, effectively scaling the
image down to a third of its height to display it on screen completely.
Now the problem with that would be that your text, having been drawn in 10
pixels height originally, would be down to 3 or 4 pixels after the
scaling, rendering it unreadable.

There's no one solution to that problem that always works. For instance,
you could decide to render the text at 30 pixels height, if you knew that
a display translation of 1:3 would be used. But that could be wrong if
somebody wanted to use a 1:4 translation on an even smaller display, so
you'd have to calculate that at the point where you know what the
translation is going to be.

The other option would be to calculate your text height as a proportion of
the image height - in my first example, we were at 10%, so for the second
example, you'd arrive at 300 pixels using that approach! Of course the
text would now look a lot bigger in the second image, even with the 1:3
translation. You'd have to use a 10:1 translation for the first image,
scaling it up to the whole screen height, to get the text size to be the
same for comparison purposes. But the mathematics work - in both cases
you'd end up displaying a 100 pixel high string on screen.

I hope I haven't confused you completely now.... the gist of the whole
story is that you can make the decision about the font size at various
different points, and there is no "correct" point where that decision
should be made. It depends on the purpose of your application, and doing
it one way may always have drawbacks when looking from a different
perspective.
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 16 '06 #3
Hello JR1,
>Thank you very much for your detailed response! In my particular case
the
images are all viewed at approximately the same size, regarless of what the
resolution of the actual image was taken at. So that being said, if I have
an image and I know that I will be viewing it at a resolution of A x B
(which
could be the native resolution or a 1 : 3 transalation) how do I determine:
1st the scale at which I should draw the font
Assuming you have these values:

int imageHeightInPi xels = ...;
int displayBoxHeigh tInPixels = ...;
int targetTextHeigh tInPixels = ...;

You could calculate the font size in pixels like this:

int fontSize = targetTextHeigh tInPixels * imageHeightInPi xels / displayBoxHeigh tInPixels;

Then you'd use that font size when constructing your font object:

Font font1 = new Font(FontFamily .GenericSansSer if, fontSize, FontStyle.Bold, GraphicsUnit.Pi xel);

I haven't done an actual test right now, but I can't imagine that
shouldn't change the text on your image. Let me know if it works!
Oliver Sturm
--
http://www.sturmnet.org/blog
Dec 16 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
14864
by: Mark Keats | last post by:
Hi there, I have some code for an ASP.Net page that produces an image of an e-mail address to prevent those programs that spider the net for e-mail addresses on pages grabbing it. The code is as follows (but on my version it gets the e-mail address from a database rather than QueryString). How can I crop the image to the exact size of...
1
4354
by: David Lindgren | last post by:
Hello! I am using the DrawString method with different StringAlignments passed to it and the result varies alot! Take a look at this screenshot: http://www.lowrad.net/files/alignment_screenshot.jpg As you can see I am printing four different text in each of the the parts of this image. ( The three parts are three images)
0
1211
by: Marc Ouellette | last post by:
Hi All. In the code below it will output the text string that is using the text from txtTextToDraw.Text using the font properties of the lblFont. Then problem is that not matter the font size in the label. It will also DrawString Bigger. If I change the font type, size of the label the DrawString output will change but it is always bigger...
3
2038
by: Jonah Olsson | last post by:
Hi guys! I'm trying to use Swedish characters with DrawString but no luck. Do I need to set the Charset somewhere?? I've been searching Google Groups for hours now, but I can't find any help or tutorial. Below is my code. Thanks for any kind of help! Regards, Jonah Olsson
2
4529
by: Lloyd Dupont | last post by:
In my .NET application I have some text rendered through GDI. It draws and print nicely. Now I would like to implement image export. So I create a new System.Drawing.Bitmap(width, height) then I create a Graphic g = Graphics.FromImage(bmp) then I use HDC hdc = g.GetHdc(); and use ExtTextOut ....
5
1400
by: johnb41 | last post by:
I need to print out a string of text and obviously i'm using the DrawString command. But the string must be placed AFTER some "programmatically generated text" (also printed using DrawString). That text would range from a single line, to maybe up to 10 lines. So i cannot hard code the position for the Drawstring command... it must come...
2
6677
by: RobinS | last post by:
Is it possible to left-justify, center, or right-justify text on a panel when drawing it using DrawString? I looked at the StringAlignment class, but all it has is far, near, and center. Thanks, Robin S.
2
2523
by: ChrisNightingale | last post by:
Hi everybody, I have an odd issue which I'm not sure how to resolve. I'm basically implementing a print mechanism which takes a series of controls and reproduces them on a print document. So what I'm trying to do is print out a label so what I basically did is use Graphics.DrawString passing in the text of the label, the font of the label...
2
12635
by: Tony Johansson | last post by:
Hello! If I use the DrawString below with object of StringFormat as the last object it works good. If I instead remove object StringFormat below as the last object of DrawString I get some rows that are not printed correctly. It's look like when toner is too low for the printer.This is only for some rows. Can somebody explain why this...
0
7504
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.