473,406 Members | 2,371 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,406 software developers and data experts.

Graphics rendering question

Hi,

I am using the following code to render a text string in a new bitmap
file. The code works, but the text looks, well, crappy, even though I
told it to use ClearType hints. Any idea how to make the text look
nicer?

Thanks
Andy

internal static string RenderDate( DateTime date ) {
Bitmap bitmap;
string result, formattedDate;
Font font;
Graphics canvas;
SizeF textSize;
SolidBrush brush;

result = Path.Combine(
Path.GetTempPath(),
date.ToString( "ddMMyyyy" )
);
result = Path.ChangeExtension( result, ".bmp" );
formattedDate = date.ToString( "d" );

using ( font =
new Font( FontFamily.GenericSansSerif, 12,
FontStyle.Regular )
) {
using ( bitmap = new Bitmap( 1, 1 ) ) {
using ( canvas = Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
textSize =
canvas.MeasureString( formattedDate, font );
}
}

using ( brush = new SolidBrush( Color.Black ) ) {
using ( bitmap = new Bitmap(

Convert.ToInt32( System.Math.Ceiling( textSize.Width ) ),

Convert.ToInt32( System.Math.Ceiling( textSize.Height ) )
) ) {

using ( canvas =
Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
canvas.DrawString(
formattedDate,
font,
brush,
new PointF( 0, 0 )
);
}

//
bitmap.RotateFlip( RotateFlipType.Rotate270FlipNone );

if ( File.Exists( result ) ) {
File.Delete( result );
}
bitmap.Save( result );
}
}
}

return result;
}

Oct 18 '07 #1
5 4045
Andy,

Well, the standard sans serif font, as far as I know, is not a ClearType
font. Have you tried looking at any of the other TextRenderingHint values?
like AntiAlias or AntiAliasGridFit?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
Hi,

I am using the following code to render a text string in a new bitmap
file. The code works, but the text looks, well, crappy, even though I
told it to use ClearType hints. Any idea how to make the text look
nicer?

Thanks
Andy

internal static string RenderDate( DateTime date ) {
Bitmap bitmap;
string result, formattedDate;
Font font;
Graphics canvas;
SizeF textSize;
SolidBrush brush;

result = Path.Combine(
Path.GetTempPath(),
date.ToString( "ddMMyyyy" )
);
result = Path.ChangeExtension( result, ".bmp" );
formattedDate = date.ToString( "d" );

using ( font =
new Font( FontFamily.GenericSansSerif, 12,
FontStyle.Regular )
) {
using ( bitmap = new Bitmap( 1, 1 ) ) {
using ( canvas = Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
textSize =
canvas.MeasureString( formattedDate, font );
}
}

using ( brush = new SolidBrush( Color.Black ) ) {
using ( bitmap = new Bitmap(

Convert.ToInt32( System.Math.Ceiling( textSize.Width ) ),

Convert.ToInt32( System.Math.Ceiling( textSize.Height ) )
) ) {

using ( canvas =
Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
canvas.DrawString(
formattedDate,
font,
brush,
new PointF( 0, 0 )
);
}

//
bitmap.RotateFlip( RotateFlipType.Rotate270FlipNone );

if ( File.Exists( result ) ) {
File.Delete( result );
}
bitmap.Save( result );
}
}
}

return result;
}

Oct 18 '07 #2
Andy,

Well, the standard sans serif font, as far as I know, is not a ClearType
font. Have you tried looking at any of the other TextRenderingHint values
like AntiAlias or AntiAliasGridFit?

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Andy" <an***@med-associates.comwrote in message
news:11**********************@i13g2000prf.googlegr oups.com...
Hi,

I am using the following code to render a text string in a new bitmap
file. The code works, but the text looks, well, crappy, even though I
told it to use ClearType hints. Any idea how to make the text look
nicer?

Thanks
Andy

internal static string RenderDate( DateTime date ) {
Bitmap bitmap;
string result, formattedDate;
Font font;
Graphics canvas;
SizeF textSize;
SolidBrush brush;

result = Path.Combine(
Path.GetTempPath(),
date.ToString( "ddMMyyyy" )
);
result = Path.ChangeExtension( result, ".bmp" );
formattedDate = date.ToString( "d" );

using ( font =
new Font( FontFamily.GenericSansSerif, 12,
FontStyle.Regular )
) {
using ( bitmap = new Bitmap( 1, 1 ) ) {
using ( canvas = Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
textSize =
canvas.MeasureString( formattedDate, font );
}
}

using ( brush = new SolidBrush( Color.Black ) ) {
using ( bitmap = new Bitmap(

Convert.ToInt32( System.Math.Ceiling( textSize.Width ) ),

Convert.ToInt32( System.Math.Ceiling( textSize.Height ) )
) ) {

using ( canvas =
Graphics.FromImage( bitmap ) ) {
canvas.TextRenderingHint =
TextRenderingHint.ClearTypeGridFit;
canvas.DrawString(
formattedDate,
font,
brush,
new PointF( 0, 0 )
);
}

//
bitmap.RotateFlip( RotateFlipType.Rotate270FlipNone );

if ( File.Exists( result ) ) {
File.Delete( result );
}
bitmap.Save( result );
}
}
}

return result;
}

Oct 18 '07 #3
The good Cleartype font is Segoe UI. Also, It's not clear from the
code there what color depth of graphics object you have. You will need
32 bit coloring (aka, something with an alpha channel) for the
Cleartype to work.

Oct 18 '07 #4
On Oct 18, 11:16 am, not_a_commie <notacom...@gmail.comwrote:
The good Cleartype font is Segoe UI. Also, It's not clear from the
code there what color depth of graphics object you have. You will need
32 bit coloring (aka, something with an alpha channel) for the
Cleartype to work.
Hi, I actually got this to work, sorry for not following up sooner.
Instead of saving a bitmap, I saved as a jpg at 99% quality and that
cleared the problem right up. Which is weird, because I thought a
bitmap would be better quality (I was using 32bit with Alpha
bitmaps... the code was creating a new Bitmap instance from scratch).

Andy

Oct 25 '07 #5
Andy wrote:
Hi, I actually got this to work, sorry for not following up sooner.
Instead of saving a bitmap, I saved as a jpg at 99% quality and that
cleared the problem right up. Which is weird, because I thought a
bitmap would be better quality (I was using 32bit with Alpha
bitmaps... the code was creating a new Bitmap instance from scratch).
Assuming the same bitmap dimensions, an image saved as .bmp (or other
lossless format, such as .png or .tiff) _will_ be better quality than a
..jpeg file.

So, it's practically certain that there's more to the difference in your
code than simply changing the file format.

Pete
Oct 25 '07 #6

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

Similar topics

0
by: Brian K | last post by:
I am new to Apache Batik and I am now doing a SVG Editor for my project. I use Java as programming language. I use Batik to generate the SVG from Java Graphics2D, as I would like to transform...
1
by: James dean | last post by:
Could someone explain how this works. I think the graphics card is used to do blitting and drawing shapes like rectangles. How does it draw using the Graphics card on the PC and why is this feature...
12
by: Dr. Zharkov | last post by:
Hello. Inform, please, on what site it is possible to find materials on construction of the three-dimensional graphic of function z=f(x,y) with the help of Visual Basic .NET and GDI+? Beforehand...
5
by: Tim | last post by:
hi I used to do this Dim gfx As System.Drawing.Graphics = pic1.CreateGraphics gfx.FillEllipse blah blah blah to draw straight onto a form. but this is frowned up (slow).
6
by: Chris Dunaway | last post by:
The method for printing documents in .Net can be confusing, especially for newer users. I would like to create a way to simplify this process. My idea would be implemented using a PrintDocument...
7
by: dave | last post by:
Hello: I have appl with a LOT of graphics developed in GDI+ It runs on XP( SP 2 ) and tablet PC. Q: Does 3.0 have more efficient graphics API that will work on XP(SP2) and Tablet PC ? ...
12
by: Xah Lee | last post by:
Of Interest: Introduction to 3D Graphics Programing http://xahlee.org/3d/index.html Currently, this introduction introduces you to the graphics format of Mathematica, and two Java Applet...
11
by: achiever | last post by:
I am a IIIrd electronics Engg student i want to know what is the role of graphics in c is it ok to spend a lot of time on learning c graphic or to start the new language like JAVA. Thanks in...
12
by: gsal | last post by:
What would be the easiest way to go about offering 3D graphics for the purpose of rendering geometry? Suppose engineers (my co-workes) have to design some enclosure, nozzle, bracket, or whatever...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
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...

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.