24bits bitmap 檔案到256色bitmap 的轉換

來源:互聯網
上載者:User

{**********************************************************}
{                                                                                                      }
{                      圖象轉換處理單元                                                    }
{                    完成24bits bitmap 檔案到256色bitmap 的轉換               }
{**********************************************************}

//==========================================================
//  程式編寫:陳林茂
//  編寫日期:2002-06-28

// 聯絡作者:linmaochen@sohu.com
//==========================================================

//==========================================================
// 程式的功能:  完成BITMAP格式的圖象到JPEG 格式圖象的轉換
// 程式轉換的演算法:利用TBITMAP,Tjpeg中的預設的方法進行轉換
// 首先定義一BITMAP資料來源,將圖象檔案調入到TBITMAP中;
// 然後定義一JPEG 資料來源,將它的資料來源定義來源於TBITMAP。
//===========================================================

unit bmpJpg_unit;

interface
Uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons,Jpeg;

//===========================================================
//函數的功能:將BITMAP格式的檔案轉換到JPG格式的檔案
//函數名稱:  BMPTOJPG
//參數的說明:
// BMPFILE  :  需要轉換的BMP格式的檔案
// JPGFILE  : 轉換為JPG格式的目的檔案
// COMPRESS : 轉換過程中檔案的壓縮比
//===========================================================
  Function BmpToJpg(BmpFile :String; JpgFile :String;Compress :Integer):Boolean;

  Function JpgToBmp(JpgFile :String; BmpFile :String;Compress :Integer):Boolean;

implementation

//將BMP圖象轉化為JPG圖象
Function  BmpToJpg(BmpFile :String; JpgFile :String;Compress :Integer):Boolean;
Var
  Bitmap  :TBitmap;
  Jpgimage :TjpegImage;
Begin
  Result :=False;
  Try
    bitmap :=TBitmap.Create ;
    JpgImage :=TjpegImage.Create ;
    bitmap.LoadFromFile(BmpFile);
    jpgImage.CompressionQuality :=Compress;
    JpgImage.Performance :=jpBestQuality;
    jpgImage.Grayscale :=True;
    jpgImage.Smoothing :=True;
    JpgImage.Assign(Bitmap);
    JpgImage.Compress ;
    JpgImage.SaveToFile(JpgFile);
    Result :=True;
  Except
  End;
  Bitmap.Free;
  JpgImage.Free ;

End;

//將JPG圖象轉化為256色的BMP圖象
Function JpgtoBmp(JpgFile :String; BmpFile :String;Compress :Integer):Boolean;
Var
  Bitmap  :TBitmap;
  Jpgimage :TjpegImage;
Begin
  Result :=False;
  Try
    bitmap :=TBitmap.Create ;
    JpgImage :=TjpegImage.Create ;
    jpgImage.LoadFromFile(JpgFile);

    JpgImage.Performance :=jpBestQuality;
    jpgImage.Grayscale :=True;
    jpgImage.Smoothing :=True;

    bitmap.Assign(jpgImage);
    bitmap.Dormant ;

    bitmap.SaveToFile(bmpfile);
    Result :=True;
  Except
  End;
  Bitmap.Free;
  JpgImage.Free ;

 

End;

 

begin

       //將24位彩色圖象轉化為256色灰階圖象

        jpgFilename := _appPath +'temp.jpg';
        bmpFilename := _appPath +'temp.bmp';
        bmptojpg(srcfileName,jpgFilename,100);
        jpgtoBmp(jpgfilename,bmpFilename,100);

    //經過上面兩個步驟,系統自動將24bits bmp 轉換為256色bitmap,在delphi5.0下通過測試,只是有點遺憾的是,彩色位元影像會轉化為灰階圖象。srcfileName :為24bits BMP檔案。

end. 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.