Converting a BMP Image to a PNG Image
To save an image to a disk file, call the
GdipSaveImageToFile function. The following console application loads a BMP image from a disk file, converts the image to the PNG format, and saves the converted image to a new disk file.
C++
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
INT GetEncoderClsid(const WCHAR* format, CLSID* pClsid); // helper function
INT main()
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID encoderClsid;
Status stat;
Image* image = new Image(L"Bird.bmp");
// Get the CLSID of the PNG encoder.
GetEncoderClsid(L"image/png", &encoderClsid);
stat = image->Save(L"Bird.png", &encoderClsid, NULL);
if(stat == Ok)
printf("Bird.png was saved successfully\n");
else
printf("Failure: stat = %d\n", stat);
delete image;
GdiplusShutdown(gdiplusToken);
return 0;
}
PowerBASICThe main function relies on the helper function
GdiPlusGetEncoderClsid to retrieve the guid of the PNG encoder.
' ========================================================================================
' Helper function to retrieve the encoder's clsid
' * strMimeType = Mime type (ANSI string)
' ========================================================================================
FUNCTION GdiPlusGetEncoderClsid (BYVAL strMimeType AS STRING) AS STRING
LOCAL hr AS LONG
LOCAL pImageCodecInfo AS ImageCodecInfo PTR
LOCAL numEncoders AS DWORD
LOCAL nSize AS DWORD
LOCAL i AS LONG
LOCAL wstrlen AS LONG
LOCAL sMimeType AS STRING
hr = GdipGetImageEncodersSize(numEncoders, nSize)
REDIM buffer(nSize - 1) AS BYTE
pImageCodecInfo = VARPTR(buffer(0))
hr = GdipGetImageEncoders(numEncoders, nSize, BYVAL pImageCodecInfo)
IF hr = 0 THEN
FOR i = 1 TO numEncoders
wstrlen = lstrlenW(BYVAL @pImageCodecInfo.MimeType)
IF wstrlen THEN sMimeType = ACODE$(PEEK$(@pImageCodecInfo.MimeType, wstrlen * 2))
IF INSTR(UCASE$(sMimeType), UCASE$(strMimeType)) THEN
FUNCTION = GUIDTXT$(@pImageCodecInfo.Clsid)
EXIT FOR
END IF
INCR pImageCodecInfo '// Increments pointer
NEXT
END IF
END FUNCTION
' ========================================================================================
#COMPILE EXE
#DIM ALL
#INCLUDE "GDIPLUS.INC"
#INCLUDE "GDIPUTILS.INC"
' ========================================================================================
' Main
' ========================================================================================
FUNCTION PBMAIN
LOCAL hStatus AS LONG
LOCAL token AS DWORD
LOCAL StartupInput AS GdiplusStartupInput
LOCAL EncoderClsid AS GUID
LOCAL pImage AS DWORD
LOCAL eps AS EncoderParameters
LOCAL ep AS EncoderParameter
LOCAL nWidth AS DWORD
LOCAL nHeight AS DWORD
LOCAL transformation AS DWORD
LOCAL strFileName AS STRING
' // Initialize GDI+
StartupInput.GdiplusVersion = 1
hStatus = GdiplusStartup(token, StartupInput, BYVAL %NULL)
IF hStatus THEN
PRINT "Error initializing GDI+"
EXIT FUNCTION
END IF
strFileName = UCODE$("Shapes.bmp")
hStatus = GdipLoadImageFromFile(STRPTR(strFileName), pImage)
' // Get the CLSID of the PNG encoder.
EncoderClsid = GUID$(GdiPlusGetEncoderClsid("image/png"))
' // Save the image.
strFileName = UCODE$("Shapes.png")
hStatus = GdipSaveImageToFile(pImage, STRPTR(strFileName), EncoderClsid, BYVAL %NULL)
IF hStatus = %StatusOk THEN
PRINT "Shapes.png saved successfully"
ELSE
PRINT "Attempt to save Shapes.png failed"
END IF
' // Cleanup
IF pImage THEN GdipDisposeImage(pImage)
' // Shutdown GDI+
GdiplusShutdown token
WAITKEY$
END FUNCTION
' ========================================================================================