Jump to content

GDI+ Error description by code


InunoTaishou
 Share

Recommended Posts

Was getting some errors in my GDI+ project and wanted to know what they actually were, since I don't know what Error code 4 is.... So I looked it up on MSDN and put together a function that would give return a description of the error based on the code.

UDF:

; #FUNCTION# ====================================================================================================================
; Name...........:  _GDIPlus_GetErrorByCode
; Description....:  Get the error message for the @Extended error when a _GDIPlus_* function fails.
; Syntax.........:  _GDIPlus_GetErrorByCode ( [$iGDIError = @Extended [, Const $iReturnType = 0] ])
; Parameters.....:  $iGDIError   - The error to retrieve. Default is the last @Extended code.
;                   $iReturnType - What type the return should be. 0 will return a string, any other value returns an array.
;                                       - [0] Holds what type of error.
;                                       - [1] Holds the description.
; Return values..:  Success     - The error based on the code given in the formate requested.
;                   Failure     - The Error code given was out of range, not a valid code.
; Author.........:  InunoTaishou
; Remarks........:  Regardless of the error code a return in the format requested will be returned. If the code is out of range
;                   it will return an OutOfRange error.
; Related........:  GDIPlus
; Link...........:  https://msdn.microsoft.com/en-us/library/windows/desktop/ms534175%28v=vs.85%29.aspx
; Example........:  Yes
; ===============================================================================================================================
Func _GDIPlus_GetErrorByCode($iGDIError = @extended, Const $iReturnType = 0)
    Local Static $aErrors[22][2] = [["OK", "Indicates that the method call was successful."], _
            ["GenericError", "Indicates that there was an error on the method call, which is identified as something other than those defined by the other elements of this enumeration."], _
            ["InvalidParameter", "Indicates that one of the arguments passed to the method was not valid."], _
            ["OutOfMemory", "Indicates that the operating system is out of memory and could not allocate memory to process the method call. For an explanation of how constructors use the OutOfMemory status, see the Remarks section at the end of this topic."], _
            ["ObjectBusy", "Indicates that one of the arguments specified in the API call is already in use in another thread."], _
            ["InsufficientBuffer", "Indicates that a buffer specified as an argument in the API call is not large enough to hold the data to be received."], _
            ["NotImplemented", "Indicates that the method is not implemented."], _
            ["Win32Error", "Indicates that the method generated a Win32 error."], _
            ["WrongState", "Indicates that the object is in an invalid state to satisfy the API call. For example, calling Pen::GetColor from a pen that is not a single, solid color results in a WrongState status."], _
            ["Aborted", "Indicates that the method was aborted."], _
            ["FileNotFound", "Indicates that the specified image file or metafile cannot be found."], _
            ["ValueOverflow", "Indicates that the method performed an arithmetic operation that produced a numeric overflow."], _
            ["AccessDenied", "Indicates that a write operation is not allowed on the specified file."], _
            ["UnknownImageFormat", "Indicates that the specified image file format is not known."], _
            ["FontFamilyNotFound", "Indicates that the specified font family cannot be found. Either the font family name is incorrect or the font family is not installed."], _
            ["FontStyleNotFound", "Indicates that the specified style is not available for the specified font family."], _
            ["NotTrueTypeFont", "Indicates that the font retrieved from an HDC or LOGFONT is not a TrueType font and cannot be used with GDI+."], _
            ["UnsupportedGdiplusVersion", "Indicates that the version of GDI+ that is installed on the system is incompatible with the version with which the application was compiled."], _
            ["GdiplusNotInitialized", "Indicates that the GDI+API is not in an initialized state. To function, all GDI+ objects require that GDI+ be in an initialized state. Initialize GDI+ by calling GdiplusStartup."], _
            ["PropertyNotFound", "Indicates that the specified property does not exist in the image."], _
            ["PropertyNotSupported", "Indicates that the specified property is not supported by the format of the image and, therefore, cannot be set."], _
            ["ProfileNotFound", "Indicates that the color profile required to save an image in CMYK format was not found."]]
    Return (($iGDIError <= 21 And $iGDIError >= 0) ? _
            ($iReturnType = 0 ? $aErrors[$iGDIError][0] & ": " & $aErrors[$iGDIError][1] : StringSplit($aErrors[$iGDIError][0] & "|" & $aErrors[$iGDIError][1], "|", 2)) : _
            ($iReturnType = 0 ? SetError(10, 0, "OutOfRange: Error code " & $iGDIError & " is out of range") : SetError(10, $iGDIError, StringSplit("OutOfRange" & "|" & "Error code " & $iGDIError & " is out of range", "|", 2))))
EndFunc   ;==>_GDIPlus_GetErrorByCode

Example:

#include <Array.au3>
#include <GDIPlus.au3>
#include <GUIEdit.au3>

_GDIPlus_Startup()
Global Const $GUI_EVENT_CLOSE = -3
Global $hGui = GUICreate("GDI+ Error by code", 800, 500)
Global $hEdit = _GUICtrlEdit_Create($hGui, "", 10, 300, 780, 190)
Global $hPic = GUICtrlGetHandle(GUICtrlCreatePic("", 0, 0, 800, 290))
Global $hDc = _WinAPI_GetWindowDC($hPic)
Global $hGraphics = _GDIPlus_GraphicsCreateFromHDC($hDc)
Global $hPen = _GDIPlus_PenCreate(0xFF00FFFF, 6)
Global $hFormat = _GDIPlus_StringFormatCreate()
Global $hFontFamily = _GDIPlus_FontFamilyCreate("Segoe UI")
Global $hFont = _GDIPlus_FontCreate($hFontFamily, 24)
Global $tLaytout = _GDIPlus_RectFCreate(0, 0, 800, 290)
Global $hBrush = _GDIPlus_BrushCreateSolid(0xFF000000)
Global $hImage = _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Image1.jpg")
Global $hFile = FileOpen(@ScriptDir & "\Image2.jpg", 16 + 1)

_GDIPlus_StringFormatSetAlign($hFormat, 1)
_GDIPlus_StringFormatSetLineAlign($hFormat, 1)

GUISetState(@SW_SHOW, $hGui)

Debug("Error generating calls" & @CRLF)
Debug("Fake Font Family for _GDIPlus_FontFamilyCreate:" & _GDIPlus_FontFamilyCreate("AutoIt Rulez") & @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF)
Debug("Wrong Parameter for _GDIPlus_GraphicsClear: " & _GDIPlus_GraphicsClear(0, 0xFFF0F0F0) & @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF)
Debug("Load image for a file that's already open _GDIPlus_ImageLoadFromFile: " & _GDIPlus_ImageLoadFromFile(@ScriptDir & "\Image2.jpg") & @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF)
Debug("Save image over a file that's already open _GDIPlus_ImageSaveToFile: " & _GDIPlus_ImageSaveToFile($hImage, @ScriptDir & "\Image2.jpg") & @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF)
_GDIPlus_ImageDispose($hImage)
Debug("Dispose image that's already been disposed _GDIPlus_ImageDispose: " & _GDIPlus_ImageDispose($hImage) & @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF)

Debug(@CRLF & "Non error generating calls" & @CRLF)
Debug("_GDIPlus_GraphicsClear: " & _GDIPlus_GraphicsClear($hGraphics, 0xFFF0F0F0) & @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF)
Debug("_GDIPlus_GraphicsDrawStringEx: " & _
      _GDIPlus_GraphicsDrawStringEx($hGraphics, "This is a GDI+ UDF for getting the error message by @Extended code." & @CRLF & "Author: InunoTaishou", $hFont, $tLaytout, $hFormat, $hBrush) & _
      @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF)
Debug("_GDIPlus_GraphicsDrawRect: " & _GDIPlus_GraphicsDrawRect($hGraphics, 2, 2, 796, 286, $hPen) & @CRLF & @TAB & _GDIPlus_GetErrorByCode() & @CRLF & @CRLF)

Debug("All available errors:" & @CRLF & @TAB)
For $i = 0 to 21
    Debug($i & ": " & _GDIPlus_GetErrorByCode($i) & @CRLF & @TAB)
Next
_ArrayDisplay(_GDIPlus_GetErrorByCode(22, 1), "Error 22")

Do

Until (GUIGetMsg() = $GUI_EVENT_CLOSE)

_WinAPI_ReleaseDC($hPic, $hDc)
_GDIPlus_GraphicsDispose($hGraphics)
_GDIPlus_PenDispose($hPen)
_GDIPlus_StringFormatDispose($hFormat)
_GDIPlus_FontFamilyDispose($hFontFamily)
_GDIPlus_BrushDispose($hBrush)
FileClose($hFile)
GUIDelete($hGui)
Exit 0

Func Debug(Const $sMsg = "")
    _GUICtrlEdit_AppendText($hEdit, $sMsg)
EndFunc   ;==>Debug

; #FUNCTION# ====================================================================================================================
; Name...........:  _GDIPlus_GetErrorByCode
; Description....:  Get the error message for the @Extended error when a _GDIPlus_* function fails.
; Syntax.........:  _GDIPlus_GetErrorByCode ( [$iGDIError = @Extended [, Const $iReturnType = 0] ])
; Parameters.....:  $iGDIError   - The error to retrieve. Default is the last @Extended code.
;                   $iReturnType - What type the return should be. 0 will return a string, any other value returns an array.
;                                       - [0] Holds what type of error.
;                                       - [1] Holds the description.
; Return values..:  Success     - The error based on the code given in the formate requested.
;                   Failure     - The Error code given was out of range, not a valid code.
; Author.........:  InunoTaishou
; Remarks........:  Regardless of the error code a return in the format requested will be returned. If the code is out of range
;                   it will return an OutOfRange error.
; Related........:  GDIPlus
; Link...........:  https://msdn.microsoft.com/en-us/library/windows/desktop/ms534175%28v=vs.85%29.aspx
; Example........:  Yes
; ===============================================================================================================================
Func _GDIPlus_GetErrorByCode($iGDIError = @extended, Const $iReturnType = 0)
    Local Static $aErrors[22][2] = [["OK", "Indicates that the method call was successful."], _
            ["GenericError", "Indicates that there was an error on the method call, which is identified as something other than those defined by the other elements of this enumeration."], _
            ["InvalidParameter", "Indicates that one of the arguments passed to the method was not valid."], _
            ["OutOfMemory", "Indicates that the operating system is out of memory and could not allocate memory to process the method call. For an explanation of how constructors use the OutOfMemory status, see the Remarks section at the end of this topic."], _
            ["ObjectBusy", "Indicates that one of the arguments specified in the API call is already in use in another thread."], _
            ["InsufficientBuffer", "Indicates that a buffer specified as an argument in the API call is not large enough to hold the data to be received."], _
            ["NotImplemented", "Indicates that the method is not implemented."], _
            ["Win32Error", "Indicates that the method generated a Win32 error."], _
            ["WrongState", "Indicates that the object is in an invalid state to satisfy the API call. For example, calling Pen::GetColor from a pen that is not a single, solid color results in a WrongState status."], _
            ["Aborted", "Indicates that the method was aborted."], _
            ["FileNotFound", "Indicates that the specified image file or metafile cannot be found."], _
            ["ValueOverflow", "Indicates that the method performed an arithmetic operation that produced a numeric overflow."], _
            ["AccessDenied", "Indicates that a write operation is not allowed on the specified file."], _
            ["UnknownImageFormat", "Indicates that the specified image file format is not known."], _
            ["FontFamilyNotFound", "Indicates that the specified font family cannot be found. Either the font family name is incorrect or the font family is not installed."], _
            ["FontStyleNotFound", "Indicates that the specified style is not available for the specified font family."], _
            ["NotTrueTypeFont", "Indicates that the font retrieved from an HDC or LOGFONT is not a TrueType font and cannot be used with GDI+."], _
            ["UnsupportedGdiplusVersion", "Indicates that the version of GDI+ that is installed on the system is incompatible with the version with which the application was compiled."], _
            ["GdiplusNotInitialized", "Indicates that the GDI+API is not in an initialized state. To function, all GDI+ objects require that GDI+ be in an initialized state. Initialize GDI+ by calling GdiplusStartup."], _
            ["PropertyNotFound", "Indicates that the specified property does not exist in the image."], _
            ["PropertyNotSupported", "Indicates that the specified property is not supported by the format of the image and, therefore, cannot be set."], _
            ["ProfileNotFound", "Indicates that the color profile required to save an image in CMYK format was not found."]]
    Return (($iGDIError <= 21 And $iGDIError >= 0) ? _
            ($iReturnType = 0 ? $aErrors[$iGDIError][0] & ": " & $aErrors[$iGDIError][1] : StringSplit($aErrors[$iGDIError][0] & "|" & $aErrors[$iGDIError][1], "|", 2)) : _
            ($iReturnType = 0 ? SetError(10, 0, "OutOfRange: Error code " & $iGDIError & " is out of range") : SetError(10, $iGDIError, StringSplit("OutOfRange" & "|" & "Error code " & $iGDIError & " is out of range", "|", 2))))
EndFunc   ;==>_GDIPlus_GetErrorByCode

 

Image1.jpg

Image2.jpg

Edited by InunoTaishou
Forgot to include the Images I used in the example.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...