Response.OutputStream

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}
 
T

tdavisjr

A couple things to look for. Not saying your issue is relate to these.

Are you setting the ContentType property of the Response object to
image/jpeg or image/gif ?
Is the output image the same size as the original image?
 
L

landers

Thanks for you reply.

The content type should be automatic.

The image is the same size.
 
K

Kevin Spencer

"Automatically" (by default) the ContentType will be "text.html".

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
L

landers

Interesting. However, not entirely correct. If you write an image to the
outputstream before anything else, the output is an image. This is same as
viewing an image file using Internet Explorer.

Nevertheless, this does not help me with my original query. Thanks anyway.

L.
 
K

Kevin Spencer

Interesting. However, not entirely correct. If you write an image to the
outputstream before anything else, the output is an image. This is same
as viewing an image file using Internet Explorer.

That's funny. Here's a quote from the MSDN documentation for
HttpResponse.ContentType:

"The HTTP MIME type of the output stream. The default value is " text/html
"."

See:

http://msdn.microsoft.com/library/d...ystemwebhttpresponseclasscontenttypetopic.asp


--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
You can lead a fish to a bicycle,
but it takes a very long time,
and the bicycle has to *want* to change.
 
L

landers

I think somewhere down the line we got our wires crossed.

When I say "automatic", I mean I do not have set it to anything, the browser
will handle how to interpret it.

Nevertheless, I have sorted the problem out by getting the ImageCodecInfo
for Jpeg, setting an encoding parameter for Quality, giving it a value of
100, and then writing this out to the outputstream.

I can post an example if you need one.

L.
 

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

Members online

No members online now.

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top