473,382 Members | 1,424 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

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 2463
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:

RasterImageGdiPlusGraphicsContainer container =
viewer.Image.CreateGdiPlusGraphics();
container.Graphics.SmoothingMode = SmoothingMode.HighQuality;
Brush bru = new SolidBrush(Color.OrangeRed);
Font font1 = new Font(FontFamily.GenericSansSerif, 12,
FontStyle.Bold);
PointF point1 = new PointF(1, 1);
container.Graphics.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 imageHeightInPixels = ...;
int displayBoxHeightInPixels = ...;
int targetTextHeightInPixels = ...;

You could calculate the font size in pixels like this:

int fontSize = targetTextHeightInPixels * imageHeightInPixels / displayBoxHeightInPixels;

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

Font font1 = new Font(FontFamily.GenericSansSerif, fontSize, FontStyle.Bold, GraphicsUnit.Pixel);

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
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...
1
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:...
0
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...
3
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...
2
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...
5
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). ...
2
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. ...
2
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...
2
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.