{********************************************************** ********} {} {Image Conversion Processing Unit} {Complete 24BITS Bitmap file to 256 color Bitmap conversion} {*************************** *************************************************
/ / =========================================================================================================================================================================================== ========== // Program: Chen Linmao // Writing Date: 2002-06-28
// Contact the author: linmaochen@sohu.com // ====================================== ====================
/ / =========================================================================================================================================================================================== ========== // The function of the program: Complete the image of the image to the JPEG format of the JPEG Format // Program Conversion: Use the default method in Tbitmap, TJPEG to convert // First Define a BitMap data source to transfer the image file into the Tbitmap; // then define a JPEG data source to define its data source from Tbitmap. / / =========================================================================================================================================================================================== =========== Unit bmpjpg_unit;
InterfaceUses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Buttons, JPEG
/ / =========================================================================================================================================================================================== ========== // Function: Convert BitMap format files to the JPG format file // function name: BMPTOJPG // Parameter Description: // BMPFile: The BMP format that needs to be converted File // jpgfile: Convert to JPG formatted destination file // compress: Compression ratio of files during conversion process // ========================== ============================================ Function BMPTOJPG (BMPFILE: STRING; JPGFILE: STRING; Compress: Integer): Boolean; function jpgtobmp (jpgfile: string; boolean): boolean;
IMPLEMENTATION
// Translate BMP image to JPG image 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;
// Translate the JPG image to 256 colors of BMP image 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
// Translate 24 color images to 256 color grayscale image
JPGFileName: = _Apppath 'Temp.jpg'; bmpFileName: = _AppPath 'Temp.bmp'; BMPTOJPG (SrcFileName, JPGFileName, 100); JPGTOBMP (JPGFileName, BMPFileName, 100);
// After two steps, the system automatically converts 24bits BMP to 256 color Bitmap, through testing under delphi5.0, just a bit unfortunately, the color bitmap will be converted to grayscale image. SRCFileName: The 24bits BMP file.
End.