3,881
社区成员
发帖
与我相关
我的任务
分享
Private Type GdiplusStartupInput
GdiplusVersion As Long
DebugEventCallback As Long
SuppressBackgroundThread As Long
SuppressExternalCodecs As Long
End Type
Private Declare Function GdiplusStartup Lib "gdiplus" (token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
Private Declare Function GdipCreateBitmapFromScan0 Lib "gdiplus" (ByVal Width As Long, ByVal Height As Long, ByVal stride As Long, ByVal PixelFormat As Long, scan0 As Any, bitmap As Long) As Long
Private Declare Function GdipCreatePen1 Lib "gdiplus" (ByVal color As Long, ByVal Width As Single, ByVal unit As Long, pen As Long) As Long
Private Declare Function GdipGetImageGraphicsContext Lib "gdiplus" (ByVal Image As Long, graphics As Long) As Long
Private Declare Function GdipDrawLine Lib "gdiplus" (ByVal graphics As Long, ByVal pen As Long, ByVal x1 As Single, ByVal y1 As Single, ByVal x2 As Single, ByVal y2 As Single) As Long
Private Declare Function CLSIDFromString Lib "ole32" (ByVal lpsz As Long, pclsid As Any) As Long
Private Declare Function GdipSaveImageToFile Lib "gdiplus" (ByVal Image As Long, ByVal filename As Long, clsidEncoder As Any, encoderParams As Any) As Long
Private Declare Function GdipDeletePen Lib "gdiplus" (ByVal pen As Long) As Long
Private Declare Function GdipDeleteGraphics Lib "gdiplus" (ByVal graphics As Long) As Long
Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
Private Declare Function GdiplusShutdown Lib "gdiplus" (ByVal token As Long) As Long
Private Sub Command1_Click()
Const PNGFile As String = "D:\1.png"
Const PixelFormat32bppARGB As Long = &H26200A
Const CLSID_PNG As String = "{557CF406-1A04-11D3-9A73-0000F81EF32E}"
Dim token As Long
Dim GpInput As GdiplusStartupInput
Dim ReturnValue As Long
Dim bitmap As Long
Dim graphics As Long
Dim pen As Long
Dim PngClsid(15) As Byte
Dim Params(7) As Long
'初始化GDI
GpInput.GdiplusVersion = 1
ReturnValue = GdiplusStartup(token, GpInput)
If ReturnValue <> 0 Then MsgBox "GDI初始化失败": Exit Sub
'新建Bitmap
GdipCreateBitmapFromScan0 100, 100, 0, PixelFormat32bppARGB, ByVal 0, bitmap
'新建pen
GdipCreatePen1 &H80FF0000, 10, 0, pen '半透明的红色
'画线
GdipGetImageGraphicsContext bitmap, graphics
GdipDrawLine graphics, pen, 10, 20, 60, 70
'保存为PNG
CLSIDFromString StrPtr(CLSID_PNG), PngClsid(0)
GdipSaveImageToFile bitmap, StrPtr(PNGFile), PngClsid(0), Params(0)
'扫地工作
GdipDeletePen pen
GdipDeleteGraphics graphics
GdipDisposeImage bitmap
GdiplusShutdown token
End Sub
Platform SDK: GDI+
Retrieving the Class Identifier for an Encoder
The function GetEncoderClsid in the following example receives the MIME type of an encoder and returns the class identifier (CLSID) of that encoder. The MIME types of the encoders built into GDI+ are as follows:
image/bmp
image/jpeg
image/gif
image/tiff
image/png
The function calls GetImageEncoders to get an array of ImageCodecInfo objects. If one of the ImageCodecInfo objects in that array represents the requested encoder, the function returns the index of the ImageCodecInfo object and copies the CLSID into the variable pointed to by pClsid. If the function fails, it returns –1.
int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
UINT num = 0; // number of image encoders
UINT size = 0; // size of the image encoder array in bytes
ImageCodecInfo* pImageCodecInfo = NULL;
GetImageEncodersSize(&num, &size);
if(size == 0)
return -1; // Failure
pImageCodecInfo = (ImageCodecInfo*)(malloc(size));
if(pImageCodecInfo == NULL)
return -1; // Failure
GetImageEncoders(num, size, pImageCodecInfo);
for(UINT j = 0; j < num; ++j)
{
if( wcscmp(pImageCodecInfo[j].MimeType, format) == 0 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}
free(pImageCodecInfo);
return -1; // Failure
}
The following console application calls the GetEncoderClsid function to determine the CLSID of the PNG encoder:
#include <windows.h>
#include <gdiplus.h>
#include <stdio.h>
using namespace Gdiplus;
#include "GdiplusHelperFunctions.h"
INT main()
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
CLSID encoderClsid;
INT result;
WCHAR strGuid[39];
result = GetEncoderClsid(L"image/png", &encoderClsid);
if(result < 0)
{
printf("The PNG encoder is not installed.\n");
}
else
{
StringFromGUID2(encoderClsid, strGuid, 39);
printf("An ImageCodecInfo object representing the PNG encoder\n");
printf("was found at position %d in the array.\n", result);
wprintf(L"The CLSID of the PNG encoder is %s.\n", strGuid);
}
GdiplusShutdown(gdiplusToken);
return 0;
}
When you run the preceding console application, you get an output similar to the following:
An ImageCodecInfo object representing the PNG encoder
was found at position 4 in the array.
The CLSID of the PNG encoder is {557CF406-1A04-11D3-9A73-0000F81EF32E}.
Platform SDK Release: August 2001 What did you think of this topic?
Let us know. Order a Platform SDK CD Online
(U.S/Canada) (International)