Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Creating Quality Graphics with System.Drawing


Message #1 by "Hugh McLaughlin" <hugh@k...> on Fri, 10 May 2002 11:45:57
Hello everyone and thanks for your help in advance.  I am 
working through examples of System.Drawing and am running 
into some issues regarding creating quality graphics for 
the web.  I have posted an example at:

http://www.hughmclaughlin.com/GDI/MyImageTest.aspx

A few of the issues I am encountering:

1.  How can you create shadow effects for text and 
shapes.  I have been unable to find a method to do this 
with a quality the is worthy of a professional site.

2.  In the example I have posted, the one created with an 
ASP.Net page has text that appears to be very pixelated.  
I have been unable to find anything for anti-aliasing or 
any other type of smoothing.

I suspect that I am simply missing something.  I have 
also attached my source code.  Any help would be greatly 
appreciated.  Thanks.
Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Fri, 10 May 2002 09:14:01 -0400
Hugh,

	I can't help you too much with shadowing, although some time ago I
created a VB (6) app for creating font textures for a 3D engine I was
working on.  Antialiasing was a problem there as well.  There are NT and
2000 API calls for antialiasing fonts, however.  If you're interested, send
an e-mail to webmaster@r... (my home address) and I will send you the
source code fragment which you can easily adapt.

- Chuck

-----Original Message-----
From: Hugh McLaughlin [mailto:hugh@k...]
Sent: Friday, May 10, 2002 7:46 AM
To: ASP+
Subject: [aspx] Creating Quality Graphics with System.Drawing


Hello everyone and thanks for your help in advance.  I am 
working through examples of System.Drawing and am running 
into some issues regarding creating quality graphics for 
the web.  I have posted an example at:

http://www.hughmclaughlin.com/GDI/MyImageTest.aspx

A few of the issues I am encountering:

1.  How can you create shadow effects for text and 
shapes.  I have been unable to find a method to do this 
with a quality the is worthy of a professional site.

2.  In the example I have posted, the one created with an 
ASP.Net page has text that appears to be very pixelated.  
I have been unable to find anything for anti-aliasing or 
any other type of smoothing.

I suspect that I am simply missing something.  I have 
also attached my source code.  Any help would be greatly 
appreciated.  Thanks.
Message #3 by "Inge" <Inge.Schepers@n...> on Fri, 10 May 2002 14:50:25
Hi everyone,

I did not see any attachments, so I was unable to look at your existing 
source code.  

I haven't tried shadowing yet, but you should be able to set antialiasing
using the following statements:

Graphics g = Graphics.FromImage(yourImage);
g.SmoothingMode = SmoothingMode.AntiAlias;

Note: you have to import both the System.Drawing and the
System.Drawing.Drawing2D namespaces

Hope this helps...

Inge

> Hello everyone and thanks for your help in advance.  I am 
w> orking through examples of System.Drawing and am running 
i> nto some issues regarding creating quality graphics for 
t> he web.  I have posted an example at:

> http://www.hughmclaughlin.com/GDI/MyImageTest.aspx

> A few of the issues I am encountering:

> 1.  How can you create shadow effects for text and 
s> hapes.  I have been unable to find a method to do this 
w> ith a quality the is worthy of a professional site.

> 2.  In the example I have posted, the one created with an 
A> SP.Net page has text that appears to be very pixelated.  
I>  have been unable to find anything for anti-aliasing or 
a> ny other type of smoothing.

> I suspect that I am simply missing something.  I have 
a> lso attached my source code.  Any help would be greatly 
a> ppreciated.  Thanks.
Message #4 by "Stephen Biggerstaff" <stephenb@w...> on Fri, 10 May 2002 16:47:19 +0100
Attachments are not allowed on the lists, please post code in the body of
your message.

Stephen

"Inge" <Inge.Schepers@n...> wrote in message news:173230@a...
>
> Hi everyone,
>
> I did not see any attachments, so I was unable to look at your existing
> source code.
>
> I haven't tried shadowing yet, but you should be able to set antialiasing
> using the following statements:
>
> Graphics g = Graphics.FromImage(yourImage);
> g.SmoothingMode = SmoothingMode.AntiAlias;
>
> Note: you have to import both the System.Drawing and the
> System.Drawing.Drawing2D namespaces
>
> Hope this helps...
>
> Inge
>
> > Hello everyone and thanks for your help in advance.  I am
> w> orking through examples of System.Drawing and am running
> i> nto some issues regarding creating quality graphics for
> t> he web.  I have posted an example at:
>
> > http://www.hughmclaughlin.com/GDI/MyImageTest.aspx
>
> > A few of the issues I am encountering:
>
> > 1.  How can you create shadow effects for text and
> s> hapes.  I have been unable to find a method to do this
> w> ith a quality the is worthy of a professional site.
>
> > 2.  In the example I have posted, the one created with an
> A> SP.Net page has text that appears to be very pixelated.
> I>  have been unable to find anything for anti-aliasing or
> a> ny other type of smoothing.
>
> > I suspect that I am simply missing something.  I have
> a> lso attached my source code.  Any help would be greatly
> a> ppreciated.  Thanks.
>
>


Message #5 by "Hugh McLaughlin" <hugh@k...> on Sat, 11 May 2002 02:27:52
Sorry for forgetting the code.  Too many late night hours.  Anyway, I 
tried Inge's solution without much success.  the new graphics are at:

http://www.hughmclaughlin.com/GDI/MyImageTest.aspx

And here is the code:

<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Drawing2d" %>
<%
Dim MyBitmap As New Bitmap(300, 200, PixelFormat.Format32bppRgb)
Dim MyGraphic As Graphics = Graphics.FromImage(MyBitmap)

Dim MyStringFormat As New StringFormat()
Dim MyFont As New Font("Arial Rounded MT Bold", 12, FontStyle.Bold)
Dim MyBrush As Brush = Brushes.White
Dim MyText As String="Welcome"
Dim MyTextSize As SizeF
MyTextSize=MyGraphic.MeasureString(MyText, MyFont)
Dim MyWidth As Integer=MyTextSize.Width
Dim MyHeight As Integer=MyTextSize.Height
Dim MyNewBitmap As New Bitmap(MyWidth, MyHeight, 
PixelFormat.Format32bppRgb)
Dim MyNewGraphic As Graphics = Graphics.FromImage(MyNewBitmap)
MyNewGraphic.SmoothingMode = SmoothingMode.AntiAlias

MyNewGraphic.Clear(Color.FromArgb(255, 204, 102))
'Dim backColor As Color = MyNewBitmap.GetPixel(1, 1)
'MyNewBitmap.MakeTransparent(backColor)

'Create Drop Shadow
MyNewGraphic.DrawString(MyText, MyFont, Brushes.Gray, 2, 2)
MyNewGraphic.DrawString(MyText, MyFont, MyBrush, 1, 1)

Response.ContentType = "image/gif"
MyNewBitmap.Save(Response.OutputStream, ImageFormat.Gif)
MyBitmap.Dispose()
%>


> Hi everyone,

> I did not see any attachments, so I was unable to look at your existing 
s> ource code.  

> I haven't tried shadowing yet, but you should be able to set 
antialiasing
u> sing the following statements:

> Graphics g = Graphics.FromImage(yourImage);
g> .SmoothingMode = SmoothingMode.AntiAlias;

> Note: you have to import both the System.Drawing and the
S> ystem.Drawing.Drawing2D namespaces

> Hope this helps...

> Inge

> > Hello everyone and thanks for your help in advance.  I am 
w> > orking through examples of System.Drawing and am running 
i> > nto some issues regarding creating quality graphics for 
t> > he web.  I have posted an example at:

> > http://www.hughmclaughlin.com/GDI/MyImageTest.aspx

> > A few of the issues I am encountering:

> > 1.  How can you create shadow effects for text and 
s> > hapes.  I have been unable to find a method to do this 
w> > ith a quality the is worthy of a professional site.

> > 2.  In the example I have posted, the one created with an 
A> > SP.Net page has text that appears to be very pixelated.  
I> >  have been unable to find anything for anti-aliasing or 
a> > ny other type of smoothing.

> > I suspect that I am simply missing something.  I have 
a> > lso attached my source code.  Any help would be greatly 
a> > ppreciated.  Thanks.
Message #6 by "Inge" <Inge.Schepers@n...> on Mon, 13 May 2002 07:52:25
Hi again,

I'm sorry it didn't work, Hugh.  Now, I know why.  The example I posted 
earlier only works for regular graphics, not text.  I searched the Visual 
Studio.Net Help for antialiasing in combination with graphics, and found 
the following article: Antialiasing with Text.  If you don't have Visual 
Studio, it might also be in the Help file you get with just the .Net 
Framework SDK.  Otherwise, it should be on MSDN 
(http://msdn.microsoft.com) as well, I think.

Below is the full article including sample code:

----------

GDI+ provides various quality levels for drawing text. Typically, higher 
quality rendering takes more processing time than lower quality rendering. 
To set the text quality level, set the TextRenderingHint property of a 
Graphics object to one of the elements of the TextRenderingHint 
enumeration.

GDI+ provides traditional antialiasing and a new kind of antialiasing 
based on Microsoft® ClearType® display technology. Only available on 
Microsoft® Windows® XP, ClearType smoothing improves readability on color 
LCD monitors that have a digital interface, such as the monitors in 
laptops and high-quality flat desktop displays. Readability on CRT screens 
is also somewhat improved.

ClearType is dependent on the orientation and ordering of the LCD stripes. 
Currently, ClearType is implemented only for vertical stripes that are 
ordered RGB. This might be a concern if you are using a tablet PC, where 
the display can be oriented in any direction, or if you are using a screen 
that can be turned from landscape to portrait.

The following example draws text with two different quality settings:

[Visual Basic]
Dim fontFamily As New FontFamily("Times New Roman")
Dim font As New Font( _
   fontFamily, _
   32, _
   FontStyle.Regular, _
   GraphicsUnit.Pixel)
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim string1 As String = "SingleBitPerPixel"
Dim string2 As String = "AntiAlias"

e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel
e.Graphics.DrawString(string1, font, solidBrush, New PointF(10, 10))

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
e.Graphics.DrawString(string2, font, solidBrush, New PointF(10, 60))
[C#]
FontFamily  fontFamily = new FontFamily("Times New Roman");
Font font = new Font( 
   fontFamily,
   32, 
   FontStyle.Regular,
   GraphicsUnit.Pixel);
SolidBrush  solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
string string1 = "SingleBitPerPixel";
string string2 = "AntiAlias";

e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
e.Graphics.DrawString(string1, font, solidBrush, new PointF(10, 10));

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString(string2, font, solidBrush, new PointF(10, 60));
The following illustration shows the output of the preceding code.

(Couldn't paste the images).

The only thing I have not been able to gather from the article is whether 
the antialiasing they use here is the "traditional" form or the form based 
on ClearType technology.  I haven't had time to try ii myself, but I hope 
this article will help you.

Inge



> Sorry for forgetting the code.  Too many late night hours.  Anyway, I 
t> ried Inge's solution without much success.  the new graphics are at:

> http://www.hughmclaughlin.com/GDI/MyImageTest.aspx

> And here is the code:

> <%@ Import Namespace="System.Drawing" %>
<> %@ Import Namespace="System.Drawing.Imaging" %>
<> %@ Import Namespace="System.Drawing.Drawing2d" %>
<> %
D> im MyBitmap As New Bitmap(300, 200, PixelFormat.Format32bppRgb)
D> im MyGraphic As Graphics = Graphics.FromImage(MyBitmap)

> Dim MyStringFormat As New StringFormat()
D> im MyFont As New Font("Arial Rounded MT Bold", 12, FontStyle.Bold)
D> im MyBrush As Brush = Brushes.White
D> im MyText As String="Welcome"
D> im MyTextSize As SizeF
M> yTextSize=MyGraphic.MeasureString(MyText, MyFont)
D> im MyWidth As Integer=MyTextSize.Width
D> im MyHeight As Integer=MyTextSize.Height
D> im MyNewBitmap As New Bitmap(MyWidth, MyHeight, 
P> ixelFormat.Format32bppRgb)
D> im MyNewGraphic As Graphics = Graphics.FromImage(MyNewBitmap)
M> yNewGraphic.SmoothingMode = SmoothingMode.AntiAlias

> MyNewGraphic.Clear(Color.FromArgb(255, 204, 102))
'> Dim backColor As Color = MyNewBitmap.GetPixel(1, 1)
'> MyNewBitmap.MakeTransparent(backColor)

> 'Create Drop Shadow
M> yNewGraphic.DrawString(MyText, MyFont, Brushes.Gray, 2, 2)
M> yNewGraphic.DrawString(MyText, MyFont, MyBrush, 1, 1)

> Response.ContentType = "image/gif"
M> yNewBitmap.Save(Response.OutputStream, ImageFormat.Gif)
M> yBitmap.Dispose()
%> >
Message #7 by Feduke Cntr Charles R <FedukeCR@m...> on Mon, 13 May 2002 09:51:32 -0400
Inge,

> The only thing I have not been able to gather from the article is whether 
> the antialiasing they use here is the "traditional" form or the form based

> on ClearType technology.

	The Win32 API will handle ClearType or traditional antialiasing,
where appropriate.  For ClearType to work properly, there has to be a
certain physical orientation to the makeup of the screen - LCD displays and
some CRT monitors have this orientation, and are marked as such in the
appropriate "monitor.inf" file.

	In addition, you can bypass the entire ClearType/traditional
anti-aliasing by breaking down to the Win32 API.  The real question is this:
if a server has a CRT with ClearType support, or a server is a laptop, does
the example .NET source code render the text with the ClearType antialiasing
routines and make it look like crap on standard machines, or is it smart
enough to render using traditional anti-aliasing routines?

- Chuck


-----Original Message-----
From: Inge [mailto:Inge.Schepers@n...]
Sent: Monday, May 13, 2002 3:52 AM
To: ASP+
Subject: [aspx] Re: Creating Quality Graphics with System.Drawing


Hi again,

I'm sorry it didn't work, Hugh.  Now, I know why.  The example I posted 
earlier only works for regular graphics, not text.  I searched the Visual 
Studio.Net Help for antialiasing in combination with graphics, and found 
the following article: Antialiasing with Text.  If you don't have Visual 
Studio, it might also be in the Help file you get with just the .Net 
Framework SDK.  Otherwise, it should be on MSDN 
(http://msdn.microsoft.com) as well, I think.

Below is the full article including sample code:

----------

GDI+ provides various quality levels for drawing text. Typically, higher 
quality rendering takes more processing time than lower quality rendering. 
To set the text quality level, set the TextRenderingHint property of a 
Graphics object to one of the elements of the TextRenderingHint 
enumeration.

GDI+ provides traditional antialiasing and a new kind of antialiasing 
based on Microsoft(r) ClearType(r) display technology. Only available on 
Microsoft(r) Windows(r) XP, ClearType smoothing improves readability on
color 
LCD monitors that have a digital interface, such as the monitors in 
laptops and high-quality flat desktop displays. Readability on CRT screens 
is also somewhat improved.

ClearType is dependent on the orientation and ordering of the LCD stripes. 
Currently, ClearType is implemented only for vertical stripes that are 
ordered RGB. This might be a concern if you are using a tablet PC, where 
the display can be oriented in any direction, or if you are using a screen 
that can be turned from landscape to portrait.

The following example draws text with two different quality settings:

[Visual Basic]
Dim fontFamily As New FontFamily("Times New Roman")
Dim font As New Font( _
   fontFamily, _
   32, _
   FontStyle.Regular, _
   GraphicsUnit.Pixel)
Dim solidBrush As New SolidBrush(Color.FromArgb(255, 0, 0, 255))
Dim string1 As String = "SingleBitPerPixel"
Dim string2 As String = "AntiAlias"

e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel
e.Graphics.DrawString(string1, font, solidBrush, New PointF(10, 10))

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias
e.Graphics.DrawString(string2, font, solidBrush, New PointF(10, 60))
[C#]
FontFamily  fontFamily = new FontFamily("Times New Roman");
Font font = new Font( 
   fontFamily,
   32, 
   FontStyle.Regular,
   GraphicsUnit.Pixel);
SolidBrush  solidBrush = new SolidBrush(Color.FromArgb(255, 0, 0, 255));
string string1 = "SingleBitPerPixel";
string string2 = "AntiAlias";

e.Graphics.TextRenderingHint = TextRenderingHint.SingleBitPerPixel;
e.Graphics.DrawString(string1, font, solidBrush, new PointF(10, 10));

e.Graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
e.Graphics.DrawString(string2, font, solidBrush, new PointF(10, 60));
The following illustration shows the output of the preceding code.

(Couldn't paste the images).

The only thing I have not been able to gather from the article is whether 
the antialiasing they use here is the "traditional" form or the form based 
on ClearType technology.  I haven't had time to try ii myself, but I hope 
this article will help you.

Inge



> Sorry for forgetting the code.  Too many late night hours.  Anyway, I 
t> ried Inge's solution without much success.  the new graphics are at:

> http://www.hughmclaughlin.com/GDI/MyImageTest.aspx

> And here is the code:

> <%@ Import Namespace="System.Drawing" %>
<> %@ Import Namespace="System.Drawing.Imaging" %>
<> %@ Import Namespace="System.Drawing.Drawing2d" %>
<> %
D> im MyBitmap As New Bitmap(300, 200, PixelFormat.Format32bppRgb)
D> im MyGraphic As Graphics = Graphics.FromImage(MyBitmap)

> Dim MyStringFormat As New StringFormat()
D> im MyFont As New Font("Arial Rounded MT Bold", 12, FontStyle.Bold)
D> im MyBrush As Brush = Brushes.White
D> im MyText As String="Welcome"
D> im MyTextSize As SizeF
M> yTextSize=MyGraphic.MeasureString(MyText, MyFont)
D> im MyWidth As Integer=MyTextSize.Width
D> im MyHeight As Integer=MyTextSize.Height
D> im MyNewBitmap As New Bitmap(MyWidth, MyHeight, 
P> ixelFormat.Format32bppRgb)
D> im MyNewGraphic As Graphics = Graphics.FromImage(MyNewBitmap)
M> yNewGraphic.SmoothingMode = SmoothingMode.AntiAlias

> MyNewGraphic.Clear(Color.FromArgb(255, 204, 102))
'> Dim backColor As Color = MyNewBitmap.GetPixel(1, 1)
'> MyNewBitmap.MakeTransparent(backColor)

> 'Create Drop Shadow
M> yNewGraphic.DrawString(MyText, MyFont, Brushes.Gray, 2, 2)
M> yNewGraphic.DrawString(MyText, MyFont, MyBrush, 1, 1)

> Response.ContentType = "image/gif"
M> yNewBitmap.Save(Response.OutputStream, ImageFormat.Gif)
M> yBitmap.Dispose()
%> >

  Return to Index