How to resize a transparent GIF?

  • Thread starter Thread starter Mike Bridge
  • Start date Start date
M

Mike Bridge

I have a method, taken from the web somewhere, for resizing a jpeg
which works well, using the System.Drawing.Graphics class. But
Graphics won't accept PixelFormats that are indexed---in my case, Gif
images---so I have to convert these to a non-indexed PixelFormat. The
problem is that when I convert these from a gif to a non-indexed
Image, then back to a Gif again, I not only lose the transparency, but
the resulting quality is poor.

Is there a correct way to do this in dotnet? My goal is to be able to
resize any .jpg or .gif (transparent or not).

Here is what I'm doing now :

Bitmap bmPhoto = new Bitmap(newWidth, newHeight,
PixelFormat.Format24bppRgb);
bmPhoto.SetResolution(origimage.HorizontalResolution,
origimage.VerticalResolution);
Graphics grPhoto = Graphics.FromImage(bmPhoto);
grPhoto.DrawImage(origimage,
new Rectangle(0,0,newWidth,newHeight),
new Rectangle(0,0,origimage.Width,origimage.Height),
GraphicsUnit.Pixel);
grPhoto.Dispose();


Thanks,

-Mike
 
Hi Mike,

Herfried. K. Wagner did supply today this piece of sample in the dotnet.vb
newsgroup.
I did not try it yet. But when Herfried makes this kind of samples the
almost always are right.
But when not, ask it in the newsgroup microsoft.public.dotnet.languages.vb
and refer to this sample and tell my name and from Herfried.

Cor

\\\By Herfried K. Wagner
Dim bmp As New Bitmap( _
"C:\Lagoon1024x768.bmp" _
)
Dim bmp2 As New Bitmap( _
bmp.Width * 0.1, _
bmp.Height * 0.1, _
Imaging.PixelFormat.Format24bppRgb _
)
Dim g As Graphics = Graphics.FromImage(bmp2)

' Select/change interpolation mode here.
g.InterpolationMode = _
Drawing.Drawing2D.InterpolationMode.HighQualityBicubic

' Draw image using specified interpolation mode.
g.DrawImage(bmp, 0, 0, bmp2.Width, bmp2.Height)
g.Dispose()

Me.PictureBox1.Image = bmp2
///

You can call 'bmp2''s 'Save' method to write the bitmap to a file.
There you can specify the destination image format too.

I hope this helps a little bit?

Cor
 
Hi Mike,

I did mean when not good for a "transparant" GIF for the rest I trust on
Herfried.

Cor
 
Back
Top