When I ran the console application targeting a folder containing only jpg, gif or jpg files, the console application throw exceptions for gif files. The png files were not resized and fortunately the jpg were ok.
Here is the log error from the event viewer:
"A Graphics object cannot be created from an image that has an indexed pixel format. "

After a few readings, I found out that the problem was at the pixel format level. In the code sample I provided earlier, I used the pixel format provided by the System.Drawing.Image object instance loaded from the file name as you can see on line 26.
I changed the pixel format to System.Drawing.Image.PixelFormat.Format24bppRgb which provides 24 bits per pixel, 8 bits for Red, 8 for Green and and 8 for Blue. This change stopped the errors. The GIF files were resized and their weight were lighter. Unfortunately, when I tried to view the image applications I used sent an error on opening.
I search a little further and I added another filter on the file,
graph.PixelOffsetMode = PixelOffsetMode.HighQuality
that I inserted between line 34 and 35. Also, I looked for the ".gif" extension at the end of the file name. If the file name ended with ".gif", I then specified that specific format:
smallImg.Save(targetFilename, Imaging.ImageFormat.Gif)
Also, I changed the InterpolationMode to HighQualityByCubic which provides a higher quality after resizing the GIF file.
Let's have a look at the images. On the next image, on the left, is the original GIF image and on the right is the resized GIF image.
As you can see, the image on the right, after resizing has lost some quality.
Well... I have looked for quite some time now on how I could solve the quality issue and I must admit it, I am now clueless. To complete the application, however, I decided to reformat the GIF to JPEG. doing this solves completely the quality issue but does not answer how I could resize GIF images.
If someone has an answer, I am curious to know how you did it.
But for now, here is the method I use to resize images:
1: ''' <summary>
2: ''' Resize images to 128x128px
3: ''' </summary>
4: ''' <param name="sourceFilename">Complete input file name</param>
5: ''' <param name="targetFilename">Complete output file name</param>
6: ''' <remarks></remarks>
7: Public Shared Sub ResizeImage( _
ByVal sourceFilename As String, _
ByVal targetFilename As String)
8:
9: Dim img As System.Drawing.Image = Nothing
10: Dim graph As Graphics = Nothing
11: Dim smallImg As System.Drawing.Image
12: Dim rectangle As Rectangle
13: Dim smlSize As Size = New Size(128, 128)
14:
15: Try
16: img = System.Drawing.Image.FromFile(sourceFilename)
17: smallImg = New Bitmap(smlSize.Width, smlSize.Height, PixelFormat.Format24bppRgb)
18: rectangle = New Rectangle(0, 0, smlSize.Width, smlSize.Height)
19:
20: graph = Graphics.FromImage(smallImg)
21: graph.CompositingQuality = CompositingQuality.HighQuality
22: graph.SmoothingMode = SmoothingMode.HighQuality
23: graph.PixelOffsetMode = PixelOffsetMode.HighQuality
24: graph.InterpolationMode = InterpolationMode.HighQualityBicubic
25: graph.DrawImage(img, rectangle)
26:
27:
28: If targetFilename.LastIndexOf(".gif") > 0 AndAlso targetFilename.EndsWith(".gif") Then
29: targetFilename = targetFilename.Replace(".gif", ".jpg")
30: smallImg.Save(targetFilename)
31: Else
32: smallImg.Save(targetFilename)
33: End If
34:
35: Catch ex As Exception
36: cLogEventViewer.LogError( _
37: System.Reflection.MethodBase.GetCurrentMethod.DeclaringType.Name & _
38: " - Exception Resize Image - " & _
39: ex.Message & " " & _
40: ex.StackTrace)
41: Finally 'Clean up!
42: If Not img Is Nothing Then
43: img.Dispose()
44: img = Nothing
45: End If
46:
47: If Not graph Is Nothing Then
48: graph.Dispose()
49: graph = Nothing
50: End If
51: smlSize = Nothing
52: rectangle = Nothing
53: End Try
54: End Sub
Have a good one!
Cheers!
Patrice