• 0

[VB.NET] Generic error occurred in GDI+


Question

I've got some code to make thumbnails by monitoring a folder for new jpeg images. However, while running against a folder with 25,000 images I am getting the following error, sometimes. (The code is running now, so I don't have a full count of how often it happens, but give it a few hours and I will have that number)

A generic error occurred in GDI+. Line 63

 Public Sub ResizeImage(ByVal filFile As FileInfo)

        Try
            Dim sThumbnail As String = filFile.FullName


            sThumbnail = sThumbnail.Replace(filFile.Extension, "") & "_Thumbnail.jpg"

            If File.Exists(sThumbnail) = False Then
                Dim dMath As Double
                Dim iHeight As Int32 = 0
                Dim bmpSource As New Bitmap(filFile.FullName)

                'To insure that the image isn't already smaller than the thumbnail will be
                If bmpSource.Width > Me.ImageWidth Then
                    dMath = bmpSource.Width / Me.ImageWidth
                    iHeight = Math.Round(bmpSource.Height / dMath)

                    Dim bmpThumbnail As New Bitmap(Me.ImageWidth, iHeight)
                    Dim g As Graphics = Graphics.FromImage(bmpThumbnail)

                    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

                    'THE ERROR HAPPENS ON THE FOLLOWING LINE
                    g.DrawImage(bmpSource, New Rectangle(0, 0, Me.ImageWidth, iHeight), New Rectangle(0, 0, bmpSource.Width, bmpSource.Height), GraphicsUnit.Pixel)

                    bmpThumbnail.Save(sThumbnail, System.Drawing.Imaging.ImageFormat.Jpeg)

                    'Cleanup
                    g.Dispose()
                    g = Nothing
                    bmpThumbnail.Dispose()
                    bmpThumbnail = Nothing
                End If


                'Cleanup
                bmpSource.Dispose()
                bmpSource = Nothing
            End If


        Catch ex As Exception
            LogException(ex)
        Finally
            GC.Collect()
        End Try

    End Sub

Any thoughts?

Link to comment
https://www.neowin.net/forum/topic/910898-vbnet-generic-error-occurred-in-gdi/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Rohdkill,

Thanks. When I'm back in the office I will check that out.

kad1r,

It's a Windows app so I can use a mapped path. It's working for 10's of thousands of files and failing for dozens. I there is a decent possibility the decimal point is the issue. Thanks though.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.