News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Help with GDI+ (Gdiplus)

Started by psyshadow, April 06, 2009, 09:07:34 AM

Previous topic - Next topic

psyshadow

Okay, so this isn't exactly a masm question, but I ported this code to C from masm. Similar code to this has been posted on these forums at least 5 times before, and that's where I got this code from.

I have a problem with the following code and need help

The code actually works exactly the way I want it to; but when the program exits it crashes (the Windows error report pops up.)
It opens a bitmap and basically just saves a copy of the bitmap and exits. Does anyone know what is wrong here?


#include <windows.h>
#include <stdio.h>
#include "mygdiplus2.h"

int main()
{
    GdiplusStartup(&token, &gdipsi, NULL);
    GdipLoadImageFromFile(L"sample.bmp", &image);
    GdipGetImageEncodersSize(&numEncoders, &sizeImageCodecInfo);
    pImageCodecInfo = malloc(sizeImageCodecInfo);

    GdipGetImageEncoders(numEncoders, sizeImageCodecInfo, pImageCodecInfo);

    GdipSaveImageToFile(image, L"copy.bmp", pImageCodecInfo, NULL);

    GdipDisposeImage(image);
    GdiplusShutdown(token);

    free(pImageCodecInfo);

    return(0);
}


The contents of the mygdiplus2.h header file:

// Mygdiplus Version 2

// GDIPLUSSTARTUPINPUT - Structure - Initialize Values
struct GdiplusStartupInput
{
DWORD GdiplusVersion;
DWORD DebugEventCallback;
DWORD SuppressBackgroundThread;
DWORD SuppressExternalCodecs;
};

static struct GdiplusStartupInput gdipsi = {1, 0, 0, 0};

// END GDIPLUSSTARTUPINPUT

// IMAGECODECINFO - Structure
struct _ImageCodecInfo
{
    CLSID ClassID;
    GUID FormatID;
    DWORD CodecName;
    DWORD DllName;
    DWORD FormatDescription;
    DWORD FilenameExtension;
    DWORD MimeType;
    DWORD Flags;
    DWORD Version;
    DWORD SigCount;
    DWORD SigSize;
    DWORD SigPattern;
    DWORD SigMask;
};
static struct _ImageCodecInfo ImageCodecInfo;
// END IMAGECODECINFO

static DWORD token = 0;
static DWORD image = 0;
static DWORD numEncoders = 0;
static DWORD sizeImageCodecInfo = 0;
static DWORD pImageCodecInfo = 0;



Just in case it helps, which I'm sure it does, I've took the time to find the origional code; here it is in MASM:


; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    include \masm32\include\masm32rt.inc
    include \masm32\include\gdiplus.inc
    includelib \masm32\lib\gdiplus.lib
    include \masm32\macros\ucmacros.asm

    GdiplusStartupInput STRUCT
      GdiplusVersion           DWORD ?
      DebugEventCallback       DWORD ?
      SuppressBackgroundThread DWORD ?
      SuppressExternalCodecs   DWORD ?
    GdiplusStartupInput ENDS

    ImageCodecInfo STRUCT
      ClassID           CLSID <>
      FormatID          GUID <>
      CodecName         DWORD ?
      DllName           DWORD ?
      FormatDescription DWORD ?
      FilenameExtension DWORD ?
      MimeType          DWORD ?
      Flags             DWORD ?
      Version           DWORD ?
      SigCount          DWORD ?
      SigSize           DWORD ?
      SigPattern        DWORD ?
      SigMask           DWORD ?
    ImageCodecInfo ENDS

; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
      token               dd 0
      image               dd 0
      numEncoders         dd 0
      sizeImageCodecInfo  dd 0
      pImageCodecInfo     dd 0
      gdipsi              GdiplusStartupInput <1>  ; version must be 1
    .code
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««

    print "GdiplusStartup rv "
    invoke GdiplusStartup, ADDR token, ADDR gdipsi, NULL
    print ustr$(eax),13,10

    print "GdipLoadImageFromFile rv "
    invoke GdipLoadImageFromFile, uni$("sample.bmp"), ADDR image
    print ustr$(eax),13,10

    print "GdipGetImageEncodersSize rv "
    invoke GdipGetImageEncodersSize, ADDR numEncoders,
                                     ADDR sizeImageCodecInfo
    print ustr$(eax),13,10

    print "numEncoders "
    print ustr$(numEncoders),13,10
    print "sizeImageCodecInfo "
    print ustr$(sizeImageCodecInfo)," bytes",13,10

    mov pImageCodecInfo, alloc(sizeImageCodecInfo)

    print "GdipGetImageEncoders rv "
    invoke GdipGetImageEncoders, numEncoders, sizeImageCodecInfo,
                                 pImageCodecInfo
    print ustr$(eax),13,10

    xor ebx, ebx
    mov esi, pImageCodecInfo
    .WHILE ebx < numEncoders
      invoke crt_printf, chr$("%S%c"), [esi].ImageCodecInfo.MimeType, 10
      .IF ucmp$([esi].ImageCodecInfo.MimeType, uni$("image/jpeg") )
        print "GdipSaveImageToFile rv "
        invoke GdipSaveImageToFile, image, uni$("sample.jpg"),
                                    esi, NULL
        print ustr$(eax),13,10
      .ENDIF
      add esi, SIZEOF ImageCodecInfo
      inc ebx
    .ENDW

    print "GdipDisposeImage rv "
    invoke GdipDisposeImage, image
    print ustr$(eax),13,10

    print "GdiplusShutdown rv "
    invoke GdiplusShutdown, token
    print ustr$(eax),13,10

    free pImageCodecInfo

    inkey "Press any key to exit..."
    exit
; «««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start

%StatusOk = 0
%StatusGenericError = 1
%StatusInvalidParameter = 2
%StatusOutOfMemory = 3
%StatusObjectBusy = 4
%StatusInsufficientBuffer = 5
%StatusNotImplemented = 6
%StatusWin32Error = 7
%StatusWrongState = 8
%StatusAborted = 9
%StatusFileNotFound = 10
%StatusValueOverflow = 11
%StatusAccessDenied = 12
%StatusUnknownImageFormat = 13
%StatusFontFamilyNotFound = 14
%StatusFontStyleNotFound = 15
%StatusNotTrueTypeFont = 16
%StatusUnsupportedGdiplusVersion = 17
%StatusGdiplusNotInitialized = 18
%StatusPropertyNotFound = 19
%StatusPropertyNotSupported = 20


Thanks so much if anyone can help
,

James


Rainstorm

hi,
if accessing GDI+ through the flat API is there some chance that windows would change something down the line & the code would possibly not work (since they are shaky.. about reccommending this way of using GDI+)

t  y