delphi2010 图片格式转换Bmp, Png,Jpeg, Gif, Tiff , WMPhoto

作者:admin 来源: 日期:2013/1/20 22:58:44 人气:1107 标签:


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls;

type
TForm1 = class(TForm)
    Button1: TButton;
    Image1: TImage;
    procedure Button1Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

//   JXSF_PIC_Format_Convert
//   图片格式转换
//   [in]   pic_stream     原图片的数据流
//   [in]   toPicForMat    要转换的图片格式
//           0      1       2        3       4         5
//           Bmp, Png,   Jpeg,     Gif,    Tiff       WMPhoto
//   [out] targ_stream
//          转换后的数据流
// 返回:
//          转换是否成功
//          0 = 失败
//          1 = 成功
function JXSF_PIC_Format_Convert (
              const pic_stream :TMemoryStream;
              const toPicForMat : INT32;
              const targ_stream : TMemoryStream
            ) : INT32;
var   wi:TWICImage;
var   flag        : INT32;
begin

    if not ( toPicForMat in [0..5] ) then
    begin
          try
               targ_stream.Clear;
          except

          end;
          Result:=0;exit;
    end;

    wi:=TWICImage.Create;
    try
        pic_stream.Position:=0;
        targ_stream.Clear;

       // 下面是格式转换核心代码
        wi.LoadFromStream(pic_stream);
        wi.ImageFormat := TWICImageFormat( toPicForMat);
        wi.SaveToStream( targ_stream);
        targ_stream.Position:=0;
        flag:=1;
    except
        flag:=0;
    end;
    wi.Free;
    Result:=flag;
end;

procedure TForm1.Button1Click(Sender: TObject);
var sm1,sm2:TMemoryStream;
   var wi:TWICImage;
begin
    sm1:= TMemoryStream.Create;
    sm2:= TMemoryStream.Create;
    sm1.LoadFromFile( \'c:\tt\0002.jpg\');
     sm1.Position :=0;
     JXSF_PIC_Format_Convert( sm1,0,sm2) ;
    wi:=TWICImage.Create;
    wi.LoadFromStream(sm2);
     Image1.Picture.Assign( wi);
    wi.Free;
    sm1.Free;
    sm2.Free;

end;

end.