Re: Drawing Text onto Image



You can definitely get better image quality from GIF than from a JPEG of the
same file-size because GIF uses lossless compression whereas JPEG is lossy.

The problem arises however when GIF is saved from GDI+ using the standard
method. The GDI+ GIF encoder does no palette manipulation and provides a
standard spead-palette for all images. This doesn't make best use of the
available palette and many colours are often not used at-all.

Either save as jpeg using less compression or save as GIF using an optimised
palette.

The GDI+ FAQ discusses both of these subjects in some detail.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.





"landers" <landley_darlington@xxxxxxxxxxx> wrote in message
news:e6qQd5YEGHA.1088@xxxxxxxxxxxxxxxxxxxxxxx
> 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}
>
>


.



Relevant Pages

  • Re: What about JPEG, GIF, etc.!!
    ... The patent on GIF expired about two years ago. ... I think GDI+ supports both JPEG and GIF natively. ... Regards, ...
    (microsoft.public.vc.mfc)
  • Re: What about JPEG, GIF, etc.!!
    ... The patent on GIF expired about two years ago. ... I think GDI+ supports both JPEG and GIF natively. ... Regards, ...
    (microsoft.public.vc.language)
  • Re: Cropping or extracting ellipse from image
    ... JPEG images only save with 24 bits per pixel. ... The only way out is to save as GIF which brings up the issue of palette ... Answer those GDI+ questions with the GDI+ FAQ ... It just seems like the transparency isn't working. ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Drawing Text onto Image
    ... I have chosen to use Jpeg with no compression. ... Public Shared Function GetCodecInfo(ByVal mimeType As String) As ... > the same file-size because GIF uses lossless compression whereas JPEG is ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Humans "unique" social
    ... > advantage of JPEG over GIF?". ... image into colored pixels before compressing the image." ...
    (sci.bio.evolution)