Groups | Blog | Home
all groups > dotnet drawing api > february 2004 >

dotnet drawing api : Resize Transparent GIF?


lori NO[at]SPAM bwedd.com
2/12/2004 10:12:37 AM
I have code that resizes JPG well... actually I developed it from a
newsgroup post here:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=eB7LHhuODHA.4024%40tk2msftngp13.phx.gbl&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%26oe%3DUTF-8%26selm%3DeB7LHhuODHA.4024%2540tk2msftngp13.phx.gbl

However, when I try to resize a transparent GIF (and save it as a JPG)
I end up with a file that shows black. I expect it to show the
transparent color (which is purple in this case). That is what
happens if I save the GIF as a JPG without resizing it.

Any suggestions?

Here is my code:

Shared Function ResizeImage(ByVal imgPhoto As Image, ByVal intWidth As
Integer, ByVal intHeight As Integer) As Image
Dim newImg As Image
Dim ms As New MemoryStream()
Dim graphic As Graphics
Dim rect As New Rectangle(0, 0, intWidth, intHeight)
Dim iformat As System.Drawing.Imaging.ImageFormat

If imgPhoto.PixelFormat =
System.Drawing.Imaging.PixelFormat.Format1bppIndexed _
Or imgPhoto.PixelFormat =
System.Drawing.Imaging.PixelFormat.Format4bppIndexed _
Or imgPhoto.PixelFormat =
System.Drawing.Imaging.PixelFormat.Format8bppIndexed _
Or imgPhoto.PixelFormat =
System.Drawing.Imaging.PixelFormat.Undefined _
Or imgPhoto.PixelFormat =
System.Drawing.Imaging.PixelFormat.DontCare _
Or imgPhoto.PixelFormat =
System.Drawing.Imaging.PixelFormat.Format16bppArgb1555 _
Or imgPhoto.PixelFormat =
System.Drawing.Imaging.PixelFormat.Format16bppGrayScale Then
'Because a graphics object cannot be created from an
Indexed Pixel Format (GIF)
'we will use a different method to resize it
newImg = New Bitmap(intWidth, intHeight,
PixelFormat.Format32bppArgb)
iformat = ImageFormat.Jpeg
Else
newImg = New Bitmap(intWidth, intHeight,
imgPhoto.PixelFormat)
iformat = imgPhoto.RawFormat
End If

'Scale image using system.drawing
graphic = Graphics.FromImage(newImg)
graphic.CompositingQuality() =
System.Drawing.Drawing2D.CompositingQuality.HighQuality
graphic.SmoothingMode =
Drawing.Drawing2D.SmoothingMode.HighQuality
graphic.InterpolationMode =
System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic
graphic.DrawImage(imgPhoto, rect)
'Save new image
newImg.Save(ms, iformat)
graphic.Dispose()

Return newImg

End Function

Thanks,
lori NO[at]SPAM bwedd.com
2/13/2004 9:25:17 AM
Thanks Bob.

I looked at your FAQ. But I don't see how to determine what my
current color palette is. Since I am resizing a GIF, I am guessing
that I will need to grab my original color palette and then set the
new color palette to be the same.

Do you have any code that shows how to do this?

Thanks,
Bob Powell [MVP]
2/13/2004 12:39:32 PM
When you resize any GIF the copy you drew to has a new non-transparent
spread palette. You need to go in and re-set the transparent colour
explicitly.

The GDI+ FAQ has an article on transparent GIF's and explains how to adjust
the colour palette when you save a GIF file.

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

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

Read my Blog at http://bobpowelldotnet.blogspot.com

[quoted text, click to view]

Bob Powell [MVP]
2/13/2004 11:35:22 PM
This is a nasty can of worms.

When a GIF is read it is stored internally as a 24 or 32 bit raster with no
palette so the original palette info is lost. Writing the GIF out again uses
a spread-palette which can cause degradation of the colours in an image,
especially if the original palette was carefully chosen to match the image.

There is a good article on MSDN that shows how to use an octtree
implementation to do colour quantization.

http://www.microsoft.com/msj/1097/wicked1097.aspx

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

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

Read my Blog at http://bobpowelldotnet.blogspot.com

[quoted text, click to view]

AddThis Social Bookmark Button