image rendering with c#

S

Spaceman007

Hello all,

I need to render an image with text on it. this text needs to be align to
the right.
all works fine with the scipt on the end of the message, but I was no able
to align the text to the right.

I did not find a way to do this so far, did anyone have the same problem
already?

Thanks for input!

Greets

Spaceman




<%@ Page Language="VB" Debug="True" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%
' Declare Vars
Dim objBMP As System.Drawing.Bitmap
Dim objGraphics As System.Drawing.Graphics
Dim objFont As System.Drawing.Font
dim text

text = "align right..."

' Create new image - bitmap
objBMP = New Bitmap(431, 35)

' Create a graphics object to work with from the BMP
objGraphics = System.Drawing.Graphics.FromImage(objBMP)

' Fill the image with background color
objGraphics.Clear(Color.red)

' Set anti-aliasing for text to make it better looking
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias

' Configure font to use for text
objFont = New Font("Syntax", 20, FontStyle.bold)

' Write out the text
objGraphics.DrawString(text, objFont, Brushes.white, 3, 9)


' Set the content type and return the image
Response.ContentType = "image/GIF"
objBMP.Save(Response.OutputStream, ImageFormat.Gif)

' Kill our objects
objFont.Dispose()
objGraphics.Dispose()
objBMP.Dispose()
%>
 
N

Nicholas Paldino [.NET/C# MVP]

Spaceman007,

I don't believe that there is a way to do this that will make the text
automatically align for you. Rather, what you will have to do is call the
MeasureString method on the Graphics object, passing your text to the
method. Once you have the length of the string, you will have to align it
yourself, offsetting the text from the left border (but not moving it too
much) by whatever amount you think is right.

Hope this helps.
 
J

Jerry Pisk

<rant>Apparently MVPs do not really know much, as suggested in a different
post :)</rant>

All you need to do is pass StringFormat object to Graphics.DrawString, with
Alignment set to StringAlignment.Far.

Jerry

Nicholas Paldino said:
Spaceman007,

I don't believe that there is a way to do this that will make the text
automatically align for you. Rather, what you will have to do is call the
MeasureString method on the Graphics object, passing your text to the
method. Once you have the length of the string, you will have to align it
yourself, offsetting the text from the left border (but not moving it too
much) by whatever amount you think is right.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



Spaceman007 said:
Hello all,

I need to render an image with text on it. this text needs to be align to
the right.
all works fine with the scipt on the end of the message, but I was no able
to align the text to the right.

I did not find a way to do this so far, did anyone have the same problem
already?

Thanks for input!

Greets

Spaceman




<%@ Page Language="VB" Debug="True" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%
' Declare Vars
Dim objBMP As System.Drawing.Bitmap
Dim objGraphics As System.Drawing.Graphics
Dim objFont As System.Drawing.Font
dim text

text = "align right..."

' Create new image - bitmap
objBMP = New Bitmap(431, 35)

' Create a graphics object to work with from the BMP
objGraphics = System.Drawing.Graphics.FromImage(objBMP)

' Fill the image with background color
objGraphics.Clear(Color.red)

' Set anti-aliasing for text to make it better looking
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias

' Configure font to use for text
objFont = New Font("Syntax", 20, FontStyle.bold)

' Write out the text
objGraphics.DrawString(text, objFont, Brushes.white, 3, 9)


' Set the content type and return the image
Response.ContentType = "image/GIF"
objBMP.Save(Response.OutputStream, ImageFormat.Gif)

' Kill our objects
objFont.Dispose()
objGraphics.Dispose()
objBMP.Dispose()
%>
 
N

Nicholas Paldino [.NET/C# MVP]

Jerry,

No one ever implied that MVPs are infallable.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jerry Pisk said:
<rant>Apparently MVPs do not really know much, as suggested in a different
post :)</rant>

All you need to do is pass StringFormat object to Graphics.DrawString, with
Alignment set to StringAlignment.Far.

Jerry

message news:[email protected]...
Spaceman007,

I don't believe that there is a way to do this that will make the text
automatically align for you. Rather, what you will have to do is call the
MeasureString method on the Graphics object, passing your text to the
method. Once you have the length of the string, you will have to align it
yourself, offsetting the text from the left border (but not moving it too
much) by whatever amount you think is right.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)



Spaceman007 said:
Hello all,

I need to render an image with text on it. this text needs to be align to
the right.
all works fine with the scipt on the end of the message, but I was no able
to align the text to the right.

I did not find a way to do this so far, did anyone have the same problem
already?

Thanks for input!

Greets

Spaceman




<%@ Page Language="VB" Debug="True" %>
<%@ Import Namespace="System.Drawing" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.Drawing.Text" %>
<%
' Declare Vars
Dim objBMP As System.Drawing.Bitmap
Dim objGraphics As System.Drawing.Graphics
Dim objFont As System.Drawing.Font
dim text

text = "align right..."

' Create new image - bitmap
objBMP = New Bitmap(431, 35)

' Create a graphics object to work with from the BMP
objGraphics = System.Drawing.Graphics.FromImage(objBMP)

' Fill the image with background color
objGraphics.Clear(Color.red)

' Set anti-aliasing for text to make it better looking
objGraphics.TextRenderingHint = TextRenderingHint.AntiAlias

' Configure font to use for text
objFont = New Font("Syntax", 20, FontStyle.bold)

' Write out the text
objGraphics.DrawString(text, objFont, Brushes.white, 3, 9)


' Set the content type and return the image
Response.ContentType = "image/GIF"
objBMP.Save(Response.OutputStream, ImageFormat.Gif)

' Kill our objects
objFont.Dispose()
objGraphics.Dispose()
objBMP.Dispose()
%>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top