matty
Scripting Guru
Posts: 8328 Reputation: 109
39 / /
Joined: Dec 2002
Status: Away
|
RE: DynImage
Welcome to the wonderful world of Gdip
code: var sGpStatus = new Array('[Ok]', '[GenericError]', '[InvalidParameter]', '[OutOfMemory]', '[ObjectBusy]', '[InsufficientBuffer]', '[NotImplemented]', '[Win32Error]', '[WrongState]', '[Aborted]', '[FileNotFound]', '[ValueOverflow ]', '[AccessDenied]', '[UnknownImageFormat]', '[FontFamilyNotFound]', '[FontStyleNotFound]', '[NotTrueTypeFont]', '[UnsupportedGdiplusVersion]', '[GdiplusNotInitialized]', '[PropertyNotFound]', '[PropertyNotSupported]');
function OnEvent_Initialize(MessengerStart){
GdipConvertImage('c:\\myimage.jpg', 'c:\\myimage.png');
}
/*
Name: GdipConvertImage
Purpose: Converts an image of any format to PNG
Parameters: sImgIn - The path to the original image
sImgOut - The path to the file to output
Return: True if function succeeds / False if it fails
*/
function GdipConvertImage (sImgIn /* InputFile */, sImgOut /* Output File */) {
var ret;
var GdipToken = Interop.Allocate(4);
var nImgPtr = Interop.Allocate(4);
var GdipStartupInput = Interop.Allocate(16);
GdipStartupInput.WriteDWORD(0, 1);
Interop.Call('gdiplus', 'GdiplusStartup', GdipToken, GdipStartupInput, 0);
if (GdipToken.ReadDWORD(0) !== 0 /* Loaded Properly */) {
Interop.Call('gdiplus', 'GdipLoadImageFromFile', sImgIn, nImgPtr);
if (nImgPtr.ReadDWORD(0) !== 0 /* Check to see if the image was loaded by Gdip */) {
var ImageFormat = Interop.Allocate(16);
Interop.Call('ole32', 'CLSIDFromString', '{557CF406-1A04-11D3-9A73-0000F81EF32E}', ImageFormat);
var EncoderParameters = Interop.Allocate(32);
with (EncoderParameters){
WriteDWORD (0, 1);
WriteDWORD (20, 1);
WriteDWORD (24, 4);
}
Interop.Call('ole32', 'CLSIDFromString', '{1D5BE4B5-FA4A-452D-9CDD-5DB35105E7EB}', EncoderParameters.DataPtr+4);
ret = Interop.Call('gdiplus', 'GdipSaveImageToFile', nImgPtr.ReadDWORD(0), sImgOut, ImageFormat, EncoderParameters);
Debug.Trace('Convert of '+sImgIn+' to '+sImgOut+(ret === 0 ? ' successful.' : ' failed! (Error Code : '+sGpStatus[ret]+')'));
Interop.Call('gdiplus', 'GdipDisposeImage', nImgPtr.ReadDWORD(0));
}
}
Interop.Call('gdiplus', 'GdiplusShutdown', GdipToken.ReadDWORD(0));
return ret === 0;
}
This post was edited on 08-19-2007 at 01:27 PM by matty.
|
|