程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> Delphi常見圖象格式轉換技術

Delphi常見圖象格式轉換技術

編輯:Delphi

  給大家提供幾個常用的圖象格式轉換方法和其轉換函數
  希望可以對你有幫助
  1. ICO圖標轉換BMP格式
  2. 32x32 BMP格式圖象轉換為 ICO格式
  3.轉換BMP->JPEG文件格式
  4.JPEG 轉換為BMP函數
  5.Bmp轉換為JPEG文件格式函數
  -------------------------------------------------------------------------------------------------------------------------
  1.Chinese : ICO圖標轉換BMP格式
  English :(Conversion from ICO to BMP)
  --------------------------------------------------------

    var
      Icon   : TIcon;
      Bitmap : TBitmap;
    begin
       Icon   := TIcon.Create;
       Bitmap := TBitmap.Create;
       Icon.LoadFromFile('c:picture.ico');
       Bitmap.Width := Icon.Width;
       Bitmap.Height := Icon.Height;
       Bitmap.Canvas.Draw(0, 0, Icon );
       Bitmap.SaveToFile('c:picture.bmp');
       Icon.Free;
       Bitmap.Free;
  ===================================
  2.Chinese: 32x32 BMP格式圖象轉換為 ICO格式
  English :32x32 bit Bitmaps to ICO's
  -----------------------------------
  unit main;

  interface

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

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

  var
    Form1: TForm1;

  implementation

  {$R *.DFM}

  procedure TForm1.Button1Click(Sender: TObject);
  var winDC, srcdc, destdc : HDC;
      oldBitmap : HBitmap;
      iinfo : TICONINFO;
  begin
      GetIconInfo(Image1.Picture.Icon.Handle, iinfo);

      WinDC := getDC(handle);
      srcDC := CreateCompatibleDC(WinDC);
      destDC := CreateCompatibleDC(WinDC);
      oldBitmap := SelectObject(destDC, iinfo.hbmColor);
      oldBitmap := SelectObject(srcDC, iinfo.hbmMask);

      BitBlt(destdc, 0, 0, Image1.picture.icon.width,
       Image1.picture.icon.height,
             srcdc, 0, 0, SRCPAINT);
      Image2.picture.bitmap.handle := SelectObject(destDC, oldBitmap);
      DeleteDC(destDC);
      DeleteDC(srcDC);
      DeleteDC(WinDC);

   image2.Picture.Bitmap.savetofile(ExtractFilePath(Application.ExeName)
            + 'myfile.bmp');
  end;

  procedure TForm1.FormCreate(Sender: TObject);
  begin
    image1.picture.icon.loadfromfile('c:myicon.ico');
  end;

  end.
  ==================================================================
  3. Chinese:轉換BMP->JPEG文件格式
  Englsh:convert the bitmap into a JPEG file format
  ------------------------------------------------------------------
  var
    MyJpeg: TJpegImage;
    Image1: TImage;
  begin
    Image1:= TImage.Create;
    MyJpeg:= TJpegImage.Create;
    Image1.LoadFromFile('TestImage.BMP');  // 讀取Bitmap文件
    MyJpeg.Assign(Image1.Picture.Bitmap); 
  object
    MyJpeg.SaveToFile('MyJPEGImage.JPG'); //保存JPEG
  end;

  --------------------------------------------------------------------
  4.JPEG 轉換為BMP函數

  procedure Jpg2Bmp(const source,dest:string);
  var
    MyJpeg: TJpegImage;
    bmp: Tbitmap;
  begin
  bmp:=tbitmap.Create;
  MyJpeg:= TJpegImage.Create;
  try
    myjpeg.LoadFromFile(source);
    bmp.Assign(myjpeg);
    bmp.SaveToFile(dest);
  finally
    bmp.free;
    myjpeg.Free;
  end;
  end;

  ----------------------------------------------------------
  5.Bmp轉換為JPEG文件格式函數
  ----------------------------------------------------------
  procedure Bmp2Jpg(const source,dest:string;const scale:byte);
  var
    MyJpeg: TJpegImage;
    Image1: TImage;
  begin
  Image1:= TImage.Create(application);
  MyJpeg:= TJpegImage.Create;
  try
    Image1.Picture.Bitmap.LoadFromFile(source);
    MyJpeg.Assign(Image1.Picture.Bitmap);
    MyJpeg.CompressionQuality:=scale;
    MyJpeg.Compress;
    MyJpeg.SaveToFile(dest);
  finally
    image1.free;
    myjpeg.Free;
  end;
  end;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved