下面是使用GDI+的,如果你的计算机上没有相应的图像编辑软件,转换的jpg文件可能不是真正意义上jpg格式.uses GDIPAPI, GDIPOBJ, GDIPUTIL;var fBitmap: TGPBitmap; Clsid: TGUID; begin fBitmap := TGPBitmap.Create('c:\test.tif'); if GetEncoderClsid('image/jpeg', Clsid) <> -1 then //'image/bmp','image/png' fBitmap.Save('c:\test.jpg', Clsid); end;
把这个文件插到你的单元,就可以用Image组件进行装入和保存,这是GDI+的最小版本^_^ unit GdiplusBitmap;interfaceuses Windows, SysUtils, ActiveX, Classes, Graphics;type TGdipBitmap = class(TBitmap) public procedure LoadFromStream(Stream: TStream); override; procedure SaveToFile(const Filename: string); override; procedure SaveToStream(Stream: TStream); override; end;implementationconst gpdll = 'gdiplus.dll';type TStatus = Integer; PGdiplusStartupInput = ^TGdiplusStartupInput; TGdiplusStartupInput = packed record GdiplusVersion: Integer; DebugEventCallback: Pointer; SuppressBackgroundThread: BOOL; SuppressExternalCodecs: BOOL; end; TImageCodecInfo = packed record Clsid: TGUID; // 能识别特定编码解码器的 GUID。 FormatID: TGUID; // 识别编码解码器格式的 GUID CodecName: PWCHAR; // 编码解码器名称的字符串 DllName: PWCHAR; // 存放编码解码器的 DLL 的路径名字符串 FormatDescription: PWCHAR; // 描述编码解码器的文件格式的字符串。 FilenameExtension: PWCHAR; // 编码解码器中使用的文件扩展名的字符串 MimeType: PWCHAR; // 编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串 Flags: DWORD; // 有关编码解码器的其他信息(ImageCodecFlags若干标志的组合) Version: DWORD; // 编码解码器的版本号 SigCount: DWORD; SigSize: DWORD; SigPattern: PByte; // 表示编码解码器签名的二维字节数组 SigMask: PByte; // 用作筛选器的二维字节数组 end; PImageCodecInfo = ^TImageCodecInfo;function GdiplusStartup(var token: DWORD; const input: PGdiplusStartupInput; output: Pointer): TStatus; stdcall; external gpdll name 'GdiplusStartup'; procedure GdiplusShutdown(token: DWORD); stdcall; external gpdll name 'GdiplusShutdown'; function GdipCreateBitmapFromStream(stream: ISTREAM; var bitmap: Pointer): TStatus; stdcall; external gpdll name 'GdipCreateBitmapFromStream'; function GdipCreateBitmapFromHBITMAP(hbm: HBITMAP; hpal: HPALETTE; var bitmap: Pointer): TStatus; stdcall; external gpdll name 'GdipCreateBitmapFromHBITMAP'; function GdipSaveImageToStream(image: Pointer; stream: ISTREAM; const clsidEncoder: PGUID; const encoderParams: Pointer): TStatus; stdcall; external gpdll name 'GdipSaveImageToStream'; function GdipDisposeImage(image: Pointer): TStatus; stdcall; external gpdll name 'GdipDisposeImage'; function GdipCreateHBITMAPFromBitmap(bitmap: Pointer; var hbmReturn: HBITMAP; background: DWORD): TStatus; stdcall; external gpdll name 'GdipCreateHBITMAPFromBitmap'; function GdipGetImageEncodersSize(var numEncoders, size: Integer): TStatus; stdcall; external gpdll name 'GdipGetImageEncodersSize'; function GdipGetImageEncoders(numEncoders, size: Integer; encoders: PImageCodecInfo): TStatus; stdcall; external gpdll name 'GdipGetImageEncoders';type TGpBitmap = class public Native: Pointer; constructor Create(stream: IStream); overload; destructor Destroy; override; constructor Create(hbm: HBITMAP; hpal: HPALETTE); overload; function GetHBITMAP: HBITMAP; procedure Save(stream: IStream; const clsidEncoder: TCLSID); end;procedure CheckStatus(Status: TStatus); begin if Status <> 0 then raise Exception.Create('Gdiplus Error!'); end;{ TGpBitmap }constructor TGpBitmap.Create(stream: IStream); begin CheckStatus(GdipCreateBitmapFromStream(stream, Native)); end;constructor TGpBitmap.Create(hbm: HBITMAP; hpal: HPALETTE); begin CheckStatus(GdipCreateBitmapFromHBITMAP(hbm, hpal, Native)); end;destructor TGpBitmap.Destroy; begin GdipDisposeImage(Native); end;function TGpBitmap.GetHBITMAP: HBITMAP; begin CheckStatus(GdipCreateHBITMAPFromBitmap(Native, Result, 0)); end;procedure TGpBitmap.Save(stream: IStream; const clsidEncoder: TCLSID); begin CheckStatus(GdipSaveImageToStream(Native, stream, @clsidEncoder, nil)); end;var GdiplusStartupInput: TGdiplusStartupInput; gdipToken: DWord; ImageFormat: string = '';procedure SetImageFormat(const Filename: string); begin ImageFormat := ExtractFileExt(Filename); if ImageFormat <> '' then begin Delete(ImageFormat, 1, 1); if CompareText(ImageFormat, 'jpg') = 0 then ImageFormat := 'jpeg' else if CompareText(ImageFormat, 'tif') = 0 then ImageFormat := 'tiff'; end else ImageFormat := 'bmp'; ImageFormat := 'image/' + ImageFormat; end;function GetImageEncoderClsid(format: String; var Clsid: TGUID): integer; var num, size, i: Integer; ImageCodecInfo: PImageCodecInfo; type InfoArray = array of TImageCodecInfo; begin num := 0; size := 0; Result := -1; GdipGetImageEncodersSize(num, size); if (size = 0) then exit; GetMem(ImageCodecInfo, size); if(ImageCodecInfo = nil) then exit; try GdipGetImageEncoders(num, size, ImageCodecInfo); i := 0; while (i < num) and (CompareText(InfoArray(ImageCodecInfo)[i].MimeType, format) = 0) do Inc(i); if i < num then begin Clsid := InfoArray(ImageCodecInfo)[i].Clsid; Result := i; end; finally FreeMem(ImageCodecInfo, size); end; end;{ TGdipBitmap }procedure TGdipBitmap.LoadFromStream(Stream: TStream); var Adaper: TStreamAdapter; tmp: TGpBitmap; begin Adaper := TStreamAdapter.Create(Stream, soReference); tmp := TGpBitmap.Create(Adaper); try Handle := tmp.GetHBITMAP; finally tmp.Free; end; end;procedure TGdipBitmap.SaveToFile(const Filename: string); begin SetImageFormat(Filename); inherited SaveToFile(Filename); ImageFormat := ''; end;procedure TGdipBitmap.SaveToStream(Stream: TStream); var tmp: TGpBitmap; Adaper: TStreamAdapter; Clsid: TGUID; begin if (ImageFormat <> '') and (GetImageEncoderClsid(ImageFormat, Clsid) <> -1) then begin tmp := TGpBitmap.Create(Handle, Palette); try Adaper := TStreamAdapter.Create(Stream, soReference); tmp.Save(Adaper, Clsid); finally tmp.Free; end; end else inherited SaveToStream(Stream); end;initialization begin FillChar(GdiplusStartupInput, Sizeof(GdiplusStartupInput), 0); GdiplusStartupInput.GdiplusVersion := 1; GdiplusStartup(gdipToken, @GdiplusStartupInput, nil); TPicture.RegisterFileFormat('bmp', 'BMP File', TGdipBitmap); TPicture.RegisterFileFormat('jpg', 'JPG File', TGdipBitmap); TPicture.RegisterFileFormat('jpeg', 'JPEG File', TGdipBitmap); TPicture.RegisterFileFormat('gif', 'GIF File', TGdipBitmap); TPicture.RegisterFileFormat('tiff', 'TIFF File', TGdipBitmap); TPicture.RegisterFileFormat('tif', 'TIFF File', TGdipBitmap); TPicture.RegisterFileFormat('Exif', 'TIFF File', TGdipBitmap); TPicture.RegisterFileFormat('png', 'PNG File', TGdipBitmap); end; finalization begin TPicture.UnregisterGraphicClass(TGdipBitmap); GdiplusShutdown(gdipToken); end;end.
while (i < num) and (CompareText(InfoArray(ImageCodecInfo)[i].MimeType, format) = 0) do 这一句有错误,改为: while (i < num) and (CompareText(InfoArray(ImageCodecInfo)[i].MimeType, format) <> 0) do另外,需要编译一次
急用哈
GDIPAPI, GDIPOBJ, GDIPUTIL;var
fBitmap: TGPBitmap;
Clsid: TGUID;
begin
fBitmap := TGPBitmap.Create('c:\test.tif');
if GetEncoderClsid('image/jpeg', Clsid) <> -1 then //'image/bmp','image/png'
fBitmap.Save('c:\test.jpg', Clsid);
end;
unit GdiplusBitmap;interfaceuses
Windows, SysUtils, ActiveX, Classes, Graphics;type
TGdipBitmap = class(TBitmap)
public
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToFile(const Filename: string); override;
procedure SaveToStream(Stream: TStream); override;
end;implementationconst
gpdll = 'gdiplus.dll';type
TStatus = Integer; PGdiplusStartupInput = ^TGdiplusStartupInput;
TGdiplusStartupInput = packed record
GdiplusVersion: Integer;
DebugEventCallback: Pointer;
SuppressBackgroundThread: BOOL;
SuppressExternalCodecs: BOOL;
end; TImageCodecInfo = packed record
Clsid: TGUID; // 能识别特定编码解码器的 GUID。
FormatID: TGUID; // 识别编码解码器格式的 GUID
CodecName: PWCHAR; // 编码解码器名称的字符串
DllName: PWCHAR; // 存放编码解码器的 DLL 的路径名字符串
FormatDescription: PWCHAR; // 描述编码解码器的文件格式的字符串。
FilenameExtension: PWCHAR; // 编码解码器中使用的文件扩展名的字符串
MimeType: PWCHAR; // 编码解码器的多用途网际邮件扩充协议 (MIME) 类型的字符串
Flags: DWORD; // 有关编码解码器的其他信息(ImageCodecFlags若干标志的组合)
Version: DWORD; // 编码解码器的版本号
SigCount: DWORD;
SigSize: DWORD;
SigPattern: PByte; // 表示编码解码器签名的二维字节数组
SigMask: PByte; // 用作筛选器的二维字节数组
end;
PImageCodecInfo = ^TImageCodecInfo;function GdiplusStartup(var token: DWORD; const input: PGdiplusStartupInput; output: Pointer): TStatus;
stdcall; external gpdll name 'GdiplusStartup';
procedure GdiplusShutdown(token: DWORD);
stdcall; external gpdll name 'GdiplusShutdown';
function GdipCreateBitmapFromStream(stream: ISTREAM; var bitmap: Pointer): TStatus;
stdcall; external gpdll name 'GdipCreateBitmapFromStream';
function GdipCreateBitmapFromHBITMAP(hbm: HBITMAP; hpal: HPALETTE; var bitmap: Pointer): TStatus;
stdcall; external gpdll name 'GdipCreateBitmapFromHBITMAP';
function GdipSaveImageToStream(image: Pointer; stream: ISTREAM; const clsidEncoder: PGUID; const encoderParams: Pointer): TStatus;
stdcall; external gpdll name 'GdipSaveImageToStream';
function GdipDisposeImage(image: Pointer): TStatus;
stdcall; external gpdll name 'GdipDisposeImage';
function GdipCreateHBITMAPFromBitmap(bitmap: Pointer; var hbmReturn: HBITMAP; background: DWORD): TStatus;
stdcall; external gpdll name 'GdipCreateHBITMAPFromBitmap';
function GdipGetImageEncodersSize(var numEncoders, size: Integer): TStatus;
stdcall; external gpdll name 'GdipGetImageEncodersSize';
function GdipGetImageEncoders(numEncoders, size: Integer; encoders: PImageCodecInfo): TStatus;
stdcall; external gpdll name 'GdipGetImageEncoders';type
TGpBitmap = class
public
Native: Pointer;
constructor Create(stream: IStream); overload;
destructor Destroy; override;
constructor Create(hbm: HBITMAP; hpal: HPALETTE); overload;
function GetHBITMAP: HBITMAP;
procedure Save(stream: IStream; const clsidEncoder: TCLSID);
end;procedure CheckStatus(Status: TStatus);
begin
if Status <> 0 then
raise Exception.Create('Gdiplus Error!');
end;{ TGpBitmap }constructor TGpBitmap.Create(stream: IStream);
begin
CheckStatus(GdipCreateBitmapFromStream(stream, Native));
end;constructor TGpBitmap.Create(hbm: HBITMAP; hpal: HPALETTE);
begin
CheckStatus(GdipCreateBitmapFromHBITMAP(hbm, hpal, Native));
end;destructor TGpBitmap.Destroy;
begin
GdipDisposeImage(Native);
end;function TGpBitmap.GetHBITMAP: HBITMAP;
begin
CheckStatus(GdipCreateHBITMAPFromBitmap(Native, Result, 0));
end;procedure TGpBitmap.Save(stream: IStream; const clsidEncoder: TCLSID);
begin
CheckStatus(GdipSaveImageToStream(Native, stream, @clsidEncoder, nil));
end;var
GdiplusStartupInput: TGdiplusStartupInput;
gdipToken: DWord;
ImageFormat: string = '';procedure SetImageFormat(const Filename: string);
begin
ImageFormat := ExtractFileExt(Filename);
if ImageFormat <> '' then
begin
Delete(ImageFormat, 1, 1);
if CompareText(ImageFormat, 'jpg') = 0 then
ImageFormat := 'jpeg'
else if CompareText(ImageFormat, 'tif') = 0 then
ImageFormat := 'tiff';
end else ImageFormat := 'bmp';
ImageFormat := 'image/' + ImageFormat;
end;function GetImageEncoderClsid(format: String; var Clsid: TGUID): integer;
var
num, size, i: Integer;
ImageCodecInfo: PImageCodecInfo;
type
InfoArray = array of TImageCodecInfo;
begin
num := 0;
size := 0;
Result := -1;
GdipGetImageEncodersSize(num, size);
if (size = 0) then exit;
GetMem(ImageCodecInfo, size);
if(ImageCodecInfo = nil) then exit;
try
GdipGetImageEncoders(num, size, ImageCodecInfo);
i := 0;
while (i < num) and (CompareText(InfoArray(ImageCodecInfo)[i].MimeType, format) = 0) do
Inc(i);
if i < num then
begin
Clsid := InfoArray(ImageCodecInfo)[i].Clsid;
Result := i;
end;
finally
FreeMem(ImageCodecInfo, size);
end;
end;{ TGdipBitmap }procedure TGdipBitmap.LoadFromStream(Stream: TStream);
var
Adaper: TStreamAdapter;
tmp: TGpBitmap;
begin
Adaper := TStreamAdapter.Create(Stream, soReference);
tmp := TGpBitmap.Create(Adaper);
try
Handle := tmp.GetHBITMAP;
finally
tmp.Free;
end;
end;procedure TGdipBitmap.SaveToFile(const Filename: string);
begin
SetImageFormat(Filename);
inherited SaveToFile(Filename);
ImageFormat := '';
end;procedure TGdipBitmap.SaveToStream(Stream: TStream);
var
tmp: TGpBitmap;
Adaper: TStreamAdapter;
Clsid: TGUID;
begin
if (ImageFormat <> '') and (GetImageEncoderClsid(ImageFormat, Clsid) <> -1) then
begin
tmp := TGpBitmap.Create(Handle, Palette);
try
Adaper := TStreamAdapter.Create(Stream, soReference);
tmp.Save(Adaper, Clsid);
finally
tmp.Free;
end;
end else
inherited SaveToStream(Stream);
end;initialization
begin
FillChar(GdiplusStartupInput, Sizeof(GdiplusStartupInput), 0);
GdiplusStartupInput.GdiplusVersion := 1;
GdiplusStartup(gdipToken, @GdiplusStartupInput, nil);
TPicture.RegisterFileFormat('bmp', 'BMP File', TGdipBitmap);
TPicture.RegisterFileFormat('jpg', 'JPG File', TGdipBitmap);
TPicture.RegisterFileFormat('jpeg', 'JPEG File', TGdipBitmap);
TPicture.RegisterFileFormat('gif', 'GIF File', TGdipBitmap);
TPicture.RegisterFileFormat('tiff', 'TIFF File', TGdipBitmap);
TPicture.RegisterFileFormat('tif', 'TIFF File', TGdipBitmap);
TPicture.RegisterFileFormat('Exif', 'TIFF File', TGdipBitmap);
TPicture.RegisterFileFormat('png', 'PNG File', TGdipBitmap);
end;
finalization
begin
TPicture.UnregisterGraphicClass(TGdipBitmap);
GdiplusShutdown(gdipToken);
end;end.
这一句有错误,改为:
while (i < num) and (CompareText(InfoArray(ImageCodecInfo)[i].MimeType, format) <> 0) do另外,需要编译一次
相当好用
loadfromtif
savetobmp/jpg就行了(以前用过,现在机器上没装)
看一看示例源代码
什么都有了
明天给分