24 bits bitmap file to 256-color bitmap Conversion

Source: Internet
Author: User
Tags bmp image

{*************************************** *******************}
{}
{Image Conversion Processing Unit}
{Converting the 24 bits bitmap file to the 256-color bitmap file}
{*************************************** *******************}

// ================================================ ================================
// Program: Chen linmao
// Written on: 2002-06-28

// Contact Author: linmaochen@sohu.com
// ================================================ ================================

// ================================================ ================================
// Program function: converts an image in bitmap format to a JPEG image.
// Program conversion algorithm: use the default method in tbitmap and tjpeg for conversion
// First define a bitmap data source and import the image file to tbitmap;
// Define a JPEG data source and define its data source from tbitmap.
// ================================================ ==================================

Unit BMP jpg_unit;

Interface
Uses
Windows, messages, sysutils, classes, graphics, controls, forms, dialogs,
Stdctrls, buttons, JPEG;

// ================================================ ==================================
// Function: convert a bitmap file to a JPG file
// Function name: BMP tojpg
// Parameter description:
// BMP file: file in BMP format to be converted
// Jpgfile: the destination file to be converted to JPG format
// Compress: File compression ratio during conversion
// ================================================ ==================================
Function BMP tojpg (BMP file: string; jpgfile: string; compress: integer): Boolean;

Function jpgtobmp (jpgfile: string; BMP file: string; compress: integer): Boolean;

Implementation

// Convert BMP image to jpg image
Function BMP tojpg (BMP file: string; jpgfile: string; compress: integer): Boolean;
VaR
Bitmap: tbitmap;
Jpgimage: tsf-image;
Begin
Result: = false;
Try
Bitmap: = tbitmap. Create;
Jpgimage: = tsf-image. Create;
Bitmap. loadfromfile (BMP file );
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;

// Convert a jpg image to a 256-color BMP Image
Function jpgtobmp (jpgfile: string; BMP file: string; compress: integer): Boolean;
VaR
Bitmap: tbitmap;
Jpgimage: tsf-image;
Begin
Result: = false;
Try
Bitmap: = tbitmap. Create;
Jpgimage: = tsf-image. Create;
Jpgimage. loadfromfile (jpgfile );

Jpgimage. Performance: = jpbestquality;
Jpgimage. grayscale: = true;
Jpgimage. Smoothing: = true;

Bitmap. Assign (jpgimage );
Bitmap. dormant;

Bitmap. savetofile (BMP file );
Result: = true;
Except
End;
Bitmap. Free;
Jpgimage. Free;

 

End;

 

Begin

// Convert a 24-bit color image to a 256-color grayscale image

Jpgfilename: = _ apppath pai'temp.jpg ';
BMP filename: = _ apppath pai'temp.bmp ';
BMP tojpg (srcfilename, jpgfilename, 100 );
Jpgtobmp (jpgfilename, BMP filename, 100 );

// After the above two steps, the system automatically converts the 24 bits BMP to a 256-color bitmap and passes the test under Delphi5.0. Unfortunately, the color bitmap is converted to a grayscale image. Srcfilename: 24 bits BMP file.

End.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.