第二个参数不能为nil供参考: function GetEncoderClsid(format: WideString; var Clsid: TGUID): Boolean; var num, size, i: Integer; ImageCodecInfo: PImageCodecInfo; type InfoArray = array of TImageCodecInfo; begin num := 0; size := 0; Result := False; GetImageEncodersSize(num, size); if (size = 0) then exit; GetMem(ImageCodecInfo, size); try GetImageEncoders(num, size, ImageCodecInfo); i := 0; while (i < num) and (CompareText(InfoArray(ImageCodecInfo)[i].MimeType, format) <> 0) do Inc(i); Result := i < num; if Result then Clsid := InfoArray(ImageCodecInfo)[i].Clsid; finally FreeMem(ImageCodecInfo, size); end; end;var Clsid: TGUID; begin if GetEncoderClsid('image/bmp', Clsid) then GdipSaveImageToFile(gdip_Graphics,StrToWideChar( 'c:\1.bmp '),Clsid,nil); end;
function GetEncoderClsid(format: WideString; var Clsid: TGUID): Boolean;
var
num, size, i: Integer;
ImageCodecInfo: PImageCodecInfo;
type
InfoArray = array of TImageCodecInfo;
begin
num := 0;
size := 0;
Result := False; GetImageEncodersSize(num, size);
if (size = 0) then exit; GetMem(ImageCodecInfo, size);
try
GetImageEncoders(num, size, ImageCodecInfo);
i := 0;
while (i < num) and (CompareText(InfoArray(ImageCodecInfo)[i].MimeType, format) <> 0) do
Inc(i);
Result := i < num;
if Result then
Clsid := InfoArray(ImageCodecInfo)[i].Clsid;
finally
FreeMem(ImageCodecInfo, size);
end;
end;var
Clsid: TGUID;
begin
if GetEncoderClsid('image/bmp', Clsid) then
GdipSaveImageToFile(gdip_Graphics,StrToWideChar( 'c:\1.bmp '),Clsid,nil);
end;
GDIImageFormatBMP: TGUID = '{557CF400-1A04-11D3-9A73-0000F81EF32E}';
GDIImageFormatJPG: TGUID = '{557CF401-1A04-11D3-9A73-0000F81EF32E}';
GDIImageFormatGIF: TGUID = '{557CF402-1A04-11D3-9A73-0000F81EF32E}';
GDIImageFormatEMF: TGUID = '{557CF403-1A04-11D3-9A73-0000F81EF32E}';
GDIImageFormatWMF: TGUID = '{557CF404-1A04-11D3-9A73-0000F81EF32E}';
GDIImageFormatTIFF: TGUID = '{557CF405-1A04-11D3-9A73-0000F81EF32E}';
GDIImageFormatPNG: TGUID = '{557CF406-1A04-11D3-9A73-0000F81EF32E}';
GDIImageFormatICO: TGUID = '{557CF407-1A04-11D3-9A73-0000F81EF32E}';TGPImage.Save(filename: WideString; const clsidEncoder: TGUID);例子:
procedure TBossPubServerWindow.actSaveToFileExecute(Sender: TObject);
begin
inherited; dlgSaveToFile.FileName := FrmStencil.StencilName;
if not Assigned(FrmGraphics) or not dlgSaveToFile.Execute then Exit; case dlgSaveToFile.FilterIndex of
1: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatBMP);
2: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatJPG);
3: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatGIF);
4: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatEMF);
5: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatWMF);
6: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatTIFF);
7: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatPNG);
8: FrmGraphics.SaveToFile(dlgSaveToFile.FileName, GDIImageFormatICO);
end;
end;如果是位图,GDIImageFormatEMF、GDIImageFormatWMF和GDIImageFormatWMF不支持!