Re: Drawing Text onto Image



Thanks Bob.

I have chosen to use Jpeg with no compression. I've created a method to
return the required ImageCodecInfo,

Public Shared Function GetCodecInfo(ByVal mimeType As String) As
Imaging.ImageCodecInfo
Dim result As Imaging.ImageCodecInfo
Dim encoder As Imaging.Encoder = Imaging.Encoder.SaveFlag

Try
For Each ice As Imaging.ImageCodecInfo In
Imaging.ImageCodecInfo.GetImageEncoders
If ice.MimeType.ToLower = mimeType.ToLower Then
result = ice
Exit For
End If
Next

Return result

Catch ex As Exception
Throw ex

End Try
End Function

, and then in my save method ...

Dim codec As Imaging.ImageCodecInfo
Dim eps As Imaging.EncoderParameters = New Imaging.EncoderParameters

codec = GetCodecInfo("image/jpeg")
eps.Param(0) = New Imaging.EncoderParameter(Imaging.Encoder.Quality, 100)

myImage.Save (myStream, codec, eps)

It works a treat.

L.


"Bob Powell [MVP]" <bob@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:OTkt116EGHA.1396@xxxxxxxxxxxxxxxxxxxxxxx
> 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: Zip files
    ... GIF and TIFF files are larger, ... 'natural' images; GIFs are best suited to simple ... Also, the degree of compression of a JPEG is variable, ...
    (comp.sys.acorn.misc)
  • Re: Memory Map query
    ... Jpeg is a 24 bit format, ideal for photographs, which can have literally ... It uses a lossy compression to keep the file ... Gif is an 8 bit format, ...
    (uk.rec.walking)
  • Re: Re: Re: Memory Map query
    ... Jpeg is a 24 bit format, ideal for photographs, which can have literally ... It uses a lossy compression to keep the file ... Gif is an 8 bit format, ...
    (uk.rec.walking)
  • Re: Who could tell me what difference between the gif and jpg file?
    ... the difference between GIF and JPEG is mainly on the compression ... JPEG is optimized for complex images, while GIF is used for simpler, 256 ...
    (microsoft.public.windowsmedia)
  • Re: OT: Early Fall
    ... Since you are not processing the jpg and then saving the file, the compression and decompression will not introduce noise into the photo. ... TIFF pictures store a single raster image at any color depth. ... Not all TIFF files are compatible with all programs that support the baseline TIFF standard. ... JPEG pictures store a single raster image in 24-bit color. ...
    (rec.boats)