Re: Drawing Text onto Image
- From: "landers" <landley_darlington@xxxxxxxxxxx>
- Date: Sun, 8 Jan 2006 15:48:36 -0000
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}
>>
>>
>
>
.
- References:
- Drawing Text onto Image
- From: landers
- Re: Drawing Text onto Image
- From: Bob Powell [MVP]
- Drawing Text onto Image
- Prev by Date: Drawstring padding rendered text
- Next by Date: Re: A newbie problem with gdiplus.h...
- Previous by thread: Re: Drawing Text onto Image
- Next by thread: Print in Landscape mode
- Index(es):
Relevant Pages
|