Question Generic GDI+ error

ALX

Well-known member
Joined
Nov 16, 2005
Messages
253
Location
Columbia, SC
Programming Experience
10+
I run into an ocassional error in Sub MakeBMPCopy() when I save a bimap to a file.
VB.NET:
Private Function CopyRect(ByVal src As Form) As Bitmap

        Dim srcPic As Graphics = src.CreateGraphics
        Dim srcBmp As New Bitmap(Rect.Width, Rect.Height, srcPic)
        Dim srcMem As Graphics = Graphics.FromImage(srcBmp)
        Dim HDC1 As IntPtr = srcPic.GetHdc
        Dim HDC2 As IntPtr = srcMem.GetHdc
        BitBlt(HDC2, 0, 0, Rect.Width, Rect.Height, HDC1, Rect.X, Rect.Y, 13369376)
        CopyRect = CType(srcBmp.Clone(), Bitmap)
        srcPic.ReleaseHdc(HDC1)
        srcMem.ReleaseHdc(HDC2)
        srcPic.Dispose()
        srcMem.Dispose()
    End Function

Private Sub MakeBMPCopy()
       [COLOR="SeaGreen"] '   This produces a smaller version of screenshot [/COLOR]
        Dim x As Integer
        Dim y As Integer

        Rect = New Rectangle(0, 0, Frm.Width, Frm.Height)
        ShrinkFactor = 0.75
        Dim p As New PictureBox
        ImagePath = "data1\bmps\snapshots\"
        Dim bmp As Bitmap = CopyRect(Frm)

        p.Size = bmp.Size
        x = CInt(p.Width * ShrinkFactor)
        y = CInt(p.Height * ShrinkFactor)
        p.Image = bmp
        p.Image = p.Image.GetThumbnailImage(x, y, AddressOf Dummy, System.IntPtr.Zero)

        Try
          [COLOR="red"] '  Here's where my error occurs...
             I can run the process maybe three or four times before the 
             error occurs...[/COLOR]      
       p.Image.Save(BaseDir & ImagePath & Filename1 & ".bmp")
        Catch ex As Exception

        End Try

        p.Dispose()
    End Sub

The Error code and a portion of the stack trace:

System.Runtime.InteropServices.ExternalException was unhandled
ErrorCode=-2147467259
Message="A generic error occurred in GDI+."
Source="System.Drawing"
StackTrace:
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at System.Drawing.Image.Save(String filename)
at ...etc.


This is a single user machine and the file is not being used by another process. Can anyone deciper this error code: -2147467259 -or- (&h80004005) ?
 
Last edited:
My guess would be a problem with the Thumbnail generation.

Your code seems fairly long-winded though. Here's the code I use :-

VB.NET:
        Using FullSizeBitmap As Bitmap = New Bitmap(Me.Width, Me.Height)

            'normal size
            Using g As Graphics = Graphics.FromImage(FullSizeBitmap)
                g.CopyFromScreen(Me.Location, Point.Empty, Me.Size)
                FullSizeBitmap.Save("c:\fullsize.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            End Using

            'reduced size
            Using HalfSizeBitmap As Bitmap = New Bitmap(Convert.ToInt32(Me.Width / 2), Convert.ToInt32(Me.Height / 2))
                Using g As Graphics = Graphics.FromImage(HalfSizeBitmap)
                    g.DrawImage(FullSizeBitmap, 0, 0, Convert.ToInt32(Me.Width / 2), Convert.ToInt32(Me.Height / 2))
                End Using
                HalfSizeBitmap.Save("c:\halfsize.jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
            End Using

        End Using
 
Long winded is an understatement! My code above doesn't even include the declarations to set up the API call. It appears that within a few lines, you're doing same thing and not having to call an API function directly. How is it that with all the research I did to accomplish this task, this simple code was never even mentioned before? So many sources all said that the API function "BitBlt Lib 'GDI32.DLL'" was the way to do this. Maybe this will solve my generic error as well. It would be nice if there were more specific information returned for these "generic errors".
>>> This is good! ... Thank you!
 
Last edited:
There appears to be a noticable difference in quality between the old "BitBlt" and the shorter version above when dealing with reduced bitmaps or jpeg's. Apparently the two different techniques are not using the same API function. The full size copy is good tho...
 
Back
Top