Response.OutputStream

Discussion in 'ASP .Net' started by landers, Jan 5, 2006.

  1. landers

    landers Guest

    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}
     
    landers, Jan 5, 2006
    #1
    1. Advertising

  2. landers

    tdavisjr Guest

    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?
     
    tdavisjr, Jan 5, 2006
    #2
    1. Advertising

  3. landers

    landers Guest

    Thanks for you reply.

    The content type should be automatic.

    The image is the same size.

    "tdavisjr" <> wrote in message
    news:...
    >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?
    >
     
    landers, Jan 5, 2006
    #3
  4. "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.

    "landers" <> wrote in message
    news:...
    > Thanks for you reply.
    >
    > The content type should be automatic.
    >
    > The image is the same size.
    >
    > "tdavisjr" <> wrote in message
    > news:...
    >>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?
    >>

    >
    >
     
    Kevin Spencer, Jan 5, 2006
    #4
  5. landers

    landers Guest

    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.


    "Kevin Spencer" <> wrote in message
    news:eIAa%...
    > "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.
    >
    > "landers" <> wrote in message
    > news:...
    >> Thanks for you reply.
    >>
    >> The content type should be automatic.
    >>
    >> The image is the same size.
    >>
    >> "tdavisjr" <> wrote in message
    >> news:...
    >>>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?
    >>>

    >>
    >>

    >
    >
     
    landers, Jan 5, 2006
    #5
  6. > 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.


    "landers" <> wrote in message
    news:...
    > 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.
    >
    >
    > "Kevin Spencer" <> wrote in message
    > news:eIAa%...
    >> "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.
    >>
    >> "landers" <> wrote in message
    >> news:...
    >>> Thanks for you reply.
    >>>
    >>> The content type should be automatic.
    >>>
    >>> The image is the same size.
    >>>
    >>> "tdavisjr" <> wrote in message
    >>> news:...
    >>>>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?
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    Kevin Spencer, Jan 6, 2006
    #6
  7. landers

    landers Guest

    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.



    "Kevin Spencer" <> wrote in message
    news:...
    >> 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.
    >
    >
    > "landers" <> wrote in message
    > news:...
    >> 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.
    >>
    >>
    >> "Kevin Spencer" <> wrote in message
    >> news:eIAa%...
    >>> "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.
    >>>
    >>> "landers" <> wrote in message
    >>> news:...
    >>>> Thanks for you reply.
    >>>>
    >>>> The content type should be automatic.
    >>>>
    >>>> The image is the same size.
    >>>>
    >>>> "tdavisjr" <> wrote in message
    >>>> news:...
    >>>>>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?
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
     
    landers, Jan 8, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jenny

    Response.OutputStream

    Jenny, Aug 4, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    9,508
    vMike
    Aug 4, 2003
  2. Ryan Hartman

    large files via Response.OutputStream.Write

    Ryan Hartman, Nov 4, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    7,375
    Ryan Hartman
    Nov 11, 2003
  3. Richard
    Replies:
    3
    Views:
    3,611
    Craig Deelsnyder
    Jan 2, 2004
  4. André
    Replies:
    3
    Views:
    1,941
    André
    Dec 1, 2006
  5. Replies:
    2
    Views:
    3,712
Loading...

Share This Page