L
landers
Hello All,
I am having problems maintaining image quality when drawing text onto an
image.
I have a reasonable good quality GIF file with a transparent background.
Whenever I write text onto it and save it as GIF / JPeg, it does image
compression, resulting in a poor quality image.
The image is being outputted to an ASPX page using the
Response.OutputStream. I have written the resulting image to a file a
viewed this, and the same problem occurs here also. I have included my code
below. I would be grateful, if anyone can shed some light on this.
Cheers,
Landers
{snippet start}
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Dim url As String = Request.RawUrl
Dim imageUrl As String = "test.gif"
Dim imageText As String = "Hello World"
GetNewImage(imageUrl, imageText).Save(Response.OutputStream,
Drawing.Imaging.ImageFormat.Jpeg)
End Sub
Private Function DrawTextOnImage(ByVal image As Drawing.Image, ByVal
text As String) As Drawing.Image
Dim n As Drawing.Bitmap = New Drawing.Bitmap(image)
Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(n)
g.DrawImage(image, 0, 0)
g.DrawString(text.ToLower, New Drawing.Font("Verdana", 16,
FontStyle.Bold, GraphicsUnit.Pixel), Drawing.Brushes.White, 20, 8)
Return n
End Function
Private Function GetNewImage(ByVal imageUrl As String, ByVal imageText
As String) As Drawing.Image
'get image
Dim imageStream As IO.StreamReader = New
IO.StreamReader(Server.MapPath(imageUrl))
Dim image As Drawing.Image =
Drawing.Image.FromStream(imageStream.BaseStream)
imageStream.Close()
'draw text on image
Dim resultImage As Drawing.Image = DrawTextOnImage(image, imageText)
Return resultImage
End Function
{snippet end}
I am having problems maintaining image quality when drawing text onto an
image.
I have a reasonable good quality GIF file with a transparent background.
Whenever I write text onto it and save it as GIF / JPeg, it does image
compression, resulting in a poor quality image.
The image is being outputted to an ASPX page using the
Response.OutputStream. I have written the resulting image to a file a
viewed this, and the same problem occurs here also. I have included my code
below. I would be grateful, if anyone can shed some light on this.
Cheers,
Landers
{snippet start}
Private Sub Page_PreRender(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.PreRender
Dim url As String = Request.RawUrl
Dim imageUrl As String = "test.gif"
Dim imageText As String = "Hello World"
GetNewImage(imageUrl, imageText).Save(Response.OutputStream,
Drawing.Imaging.ImageFormat.Jpeg)
End Sub
Private Function DrawTextOnImage(ByVal image As Drawing.Image, ByVal
text As String) As Drawing.Image
Dim n As Drawing.Bitmap = New Drawing.Bitmap(image)
Dim g As Drawing.Graphics = Drawing.Graphics.FromImage(n)
g.DrawImage(image, 0, 0)
g.DrawString(text.ToLower, New Drawing.Font("Verdana", 16,
FontStyle.Bold, GraphicsUnit.Pixel), Drawing.Brushes.White, 20, 8)
Return n
End Function
Private Function GetNewImage(ByVal imageUrl As String, ByVal imageText
As String) As Drawing.Image
'get image
Dim imageStream As IO.StreamReader = New
IO.StreamReader(Server.MapPath(imageUrl))
Dim image As Drawing.Image =
Drawing.Image.FromStream(imageStream.BaseStream)
imageStream.Close()
'draw text on image
Dim resultImage As Drawing.Image = DrawTextOnImage(image, imageText)
Return resultImage
End Function
{snippet end}