如何将TImage里的图片另存为BMP、JPG、PNG格式的文件

之前我写了如何从数据库里读取图像数据并用Image控件显示,现在讲讲如何另存为图像文件。

procedure TForm_ShowPic.N1Click(Sender: TObject);
var
  vPng:TPNGObject;
  vJpg:TJPEGImage;
  vBmp:TBitmap;
  vTmp:string;
  FType:Integer;
  vStream:TStream;
begin
  with SaveDialog1 do
  begin
    if not Execute then Exit;    
    FType:=-1;                  
    vTmp:=ExtractFileExt(FileName);   //分析用户保存的图像类型
    if vTmp='.png' then
      FType:=0
    else
    if vTmp='.jpg' then
      FType:=1
    else
    if vTmp='.bmp' then
      FType:=2;
    vTmp:=FileName;   //这是文件路径
  end;
  try
    if FType=-1 then  //默认是bmp
      FType:=2;
    vStream:=TMemoryStream.Create;   //创建流
    ImageShow.Picture.Graphic.SaveToStream(vStream); 将Image里的图像读到流里
    if vStream.Size=0 then  //判断有没有图像 
    begin
      FreeAndNil(vStream);
      ShowMessage('没有图像!');
      Exit;
    end;
    vStream.Position:=0;  //移动到流的开头,不要少了这步
    if FType=0 then  //保存成Png 文件
    begin
      vPng:=TPNGObject.Create;
      vPng.LoadFromStream(vStream); 
      vPng.SaveToFile(vTmp);
      FreeAndNil(vPng);
 
    end;
    if FType=1 then  //保存成jpg文件
    begin
      vJpg:=TJPEGImage.Create;
      vBmp:=TBitmap.Create;  //先创建一个bmp
      vBmp.Assign(ImageShow.Picture.Graphic);  //把Image里的图像读到bmp里
 
      vJpg.Assign(vBmp);  //在这里将bmp的图像转成jpg
      vJpg.SaveToFile(vTmp);
      FreeAndNil(vBmp);
      FreeAndNil(vJpg);
    end;
    if FType=2 then   //保存成bmp文件
    begin
      ImageShow.Picture.Graphic.SaveToFile(vTmp);  //bmp的最简单,直接另存为就好
    end;
    FreeAndNil(vStream);
  except
    on E:Exception do
    begin
      MessageDlg('图像保存失败!原因是:'+#13+E.Message,mtWarning,[mbOK],0);
      Exit;
    end;
  end;
end;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
procedure BitmapFileToPNG(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin Bitmap := TBitmap.Create; PNG := TPNGObject.Create; {In case something goes wrong, free booth Bitmap and PNG} try Bitmap.LoadFromFile(Source); PNG.Assign(Bitmap); //Convert data into png PNG.SaveToFile(Dest); finally Bitmap.Free; PNG.Free; end end; Converting from PNG file to Windows bitmap file The above inverse. Loads a png and saves into a bitmap procedure PNGFileToBitmap(const Source, Dest: String); var Bitmap: TBitmap; PNG: TPNGObject; begin PNG := TPNGObject.Create; Bitmap := TBitmap.Create; {In case something goes wrong, free booth PNG and Bitmap} try PNG.LoadFromFile(Source); Bitmap.Assign(PNG); //Convert data into bitmap Bitmap.SaveToFile(Dest); finally PNG.Free; Bitmap.Free; end end; Converting from TImage to PNG file This method converts from TImage to PNG. It has full exception handling and allows converting from file formats other than TBitmap (since they allow assigning to a TBitmap) procedure TImageToPNG(Source: TImage; const Dest: String); var PNG: TPNGObject; BMP: TBitmap; begin PNG := TPNGObject.Create; {In case something goes wrong, free PNG} try //If the TImage contains a TBitmap, just assign from it if Source.Picture.Graphic is TBitmap then PNG.Assign(TBitmap(Source.Picture.Graphic)) //Convert bitmap data into png else begin //Otherwise try to assign first to a TBimap BMP := TBitmap.Create; try BMP.Assign(Source.Picture.Graphic); PNG.Assign(BMP); finally BMP.Free; end; end; //Save to PNG format PNG.SaveToFile(Dest); finally PNG.Free; end end;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值