BMPJPG zoom to your designated size and quality JPG format

xiaoxiao2021-03-06  41

These are me later, and I also changed in the package. In addition, I didn't think of ASSIGN, but it would happen in some cases. If I have not encountered this problem, I can't respond casually. Because it is rushing, the results have been resolved after Assign, so I haven't discussed what kind of situation, please forgive me. Finally, I am very grateful to you share this function.

Unit jpgctrl;

Interface

Uses

Graphics, JPEG, MATH, SYSUTILS, TYPES

Type

DK1 = Record

HEADER: Array [1..10] of char; //

END;

Function Stretchimage (FileName: TFileName; Width, Height, Quality: Integer: tjpegimage;

Function StretchimagePercent (FileName: TFileName; PERCENT, QUALITY: Integer: tjpegimage;

Function Stretchimageratio (FileName: TFileName; Width, Height, Quality: Integer; Center: Boolean: tjpegimage

IMPLEMENTATION

/ / This feature can scalable the BMP / JPG file to your designated size and quality JPG format, you can save it, exist database, and even put into the TSTREAM directly to the user Browser as a display.

Function Stretchimage (FileName: TFileName; Width, Height, Quality: Integer: tjpegimage;

VAR

BMP, TEMPBMP: TBITMAP;

RT: TRECT;

F1: file of dk1;

DKIND1: DK1;

Sheader: String;

Begin

Result: = tjpegimage.create;

BMP: = Tbitmap.create;

Tempbmp: = tbitmap.create;

Bmp.Width: = width;

bmp.height: = height;

Rt.Left: = 0;

Rt.top: = 0;

Rt.right: = Width - 1;

Rt.bottom: = height - 1;

Try

AssignFile (F1, FileName);

RESET (F1);

Seek (F1, 0);

Read (F1, Dkind1);

Sheader: = DKIND1.HEADER;

Closefile (f1);

// if Uppercase (extractfileext (filename)) = '.jpg' Then

if Sheader = # $ FF # $ D8 # $ FF # $ E0 # $ 00 # $ 10 # $ 4a # $ 46 # $ 49 # $ 46 of

Begin

Result.LoadFromfile (filename);

Bmp.canvas.stretchDraw (RT, Result);

end

// Else if Uppercase (EXTRACTFILEEXT (FileName)) = '.bmp' Then

Else if sheader = # $ 42 # $ 4d # $ a6 # $ 42 # $ 00 # $ 00 # $ 00 # $ 00 # $ 00 # $ 00 TENEN

Begin

TempBmp.LoadFromFile (filename);

Bmp.canvas.stretchDraw (RT, TEMPBMP); END

Else

Begin

Result.Assign (BMP);

EXIT;

END;

Result.compressionquality: = QUALITY

Result.Assign (BMP);

Finally

Tempbmp.free;

Bmp.free;

END;

END;

// Use basically the same, just entering the width and high, more than one hundred points, with the original size by 100 parts to zoom

Function StretchimagePercent (FileName: TFileName; PERCENT, QUALITY: Integer: tjpegimage;

VAR

BMP, TEMPBMP: TBITMAP;

RT: TRECT;

F1: file of dk1;

DKIND1: DK1;

Sheader, Stype: String;

Begin

Result: = tjpegimage.create;

BMP: = Tbitmap.create;

Tempbmp: = tbitmap.create;

Try

AssignFile (F1, FileName);

RESET (F1);

Seek (F1, 0);

Read (F1, Dkind1);

Sheader: = DKIND1.HEADER;

Closefile (f1);

// if Uppercase (extractfileext (filename)) = '.jpg' Then

if Sheader = # $ FF # $ D8 # $ FF # $ E0 # $ 00 # $ 10 # $ 4a # $ 46 # $ 49 # $ 46 of

Begin

STYPE: = '. JPG';

Result.LoadFromfile (filename);

Bmp.Width: = round (result.width * percent / 100);

bmp.height: = round (result. Height * percent / 100);

end

// Else if Uppercase (EXTRACTFILEEXT (FileName)) = '.bmp' Then

Else if sheader = # $ 42 # $ 4d # $ a6 # $ 42 # $ 00 # $ 00 # $ 00 # $ 00 # $ 00 # $ 00 TENEN

Begin

STYPE: = '. BMP';

TempBmp.LoadFromFile (filename);

bmp.width: = round (tempbmp.width * percent / 100);

Bmp.height: = round (Tempbmp.Height * Percent / 100);

end

Else

Begin

Result.Assign (BMP);

EXIT;

END;

Rt.Left: = 0;

Rt.top: = 0;

Rt.right: = bmp.width - 1;

Rt.bottom: = bmp.height - 1;

if stype = '.jpg' Then

Bmp.canvas.stretchDraw (RT, Result)

Else

Bmp.canvas.stretchDraw (RT, TEMPBMP);

Result.compressionquality: = QUALITY

Result.Assign (BMP);

Finally

Tempbmp.free;

Bmp.free;

END;

END;

// This version will retain the length of the original image, zoom to the size frame you specify.

// For example, the original picture is 600 x 600, and your preset image size is 300 x 200 The image will be long after the quadrupan. However, if this function is used, the image will shrink into 200 x 200, and the reservation is just to enter your preset image framework. // This function also has a parameter to determine the output location of the image and the size.

// Center: Decided that if the image ratio does not match the specified size frame, the size is placed in the upper left corner or in the upper left corner. If the parameter is TRUE, the image is automatically added, and the output image size must be filled in the width, height, other vacant you specified. If false, the output image is based on the size after the calculation, does not leave the white.

Function Stretchimageratio (FileName: TFileName; Width, Height, Quality: Integer; Center: Boolean: tjpegimage

VAR

BMP, TEMPBMP: TBITMAP;

RT: TRECT;

Ratio: double;

Neww, newh: integer;

F1: file of dk1;

DKIND1: DK1;

Sheader, Stype: String;

Begin

Result: = tjpegimage.create;

BMP: = Tbitmap.create;

Tempbmp: = tbitmap.create;

Try

AssignFile (F1, FileName);

RESET (F1);

Seek (F1, 0);

Read (F1, Dkind1);

Sheader: = DKIND1.HEADER;

Closefile (f1);

// if Uppercase (extractfileext (filename)) = '.jpg' Then

if Sheader = # $ FF # $ D8 # $ FF # $ E0 # $ 00 # $ 10 # $ 4a # $ 46 # $ 49 # $ 46 of

Begin

STYPE: = '. JPG';

Result.LoadFromfile (filename);

Ratio: = min (width / result.height);

NEWW: = Round (Result.width * Ratio);

NEWH: = Round (Result.height * Ratio);

end

// Else if Uppercase (EXTRACTFILEEXT (FileName)) = '.bmp' Then

Else if sheader = # $ 42 # $ 4d # $ a6 # $ 42 # $ 00 # $ 00 # $ 00 # $ 00 # $ 00 # $ 00 TENEN

Begin

STYPE: = '. BMP';

TempBmp.LoadFromFile (filename);

Ratio: = min (width / tempbmp.width, height / tempbmp.height);

NEWW: = Round (Tempbmp.width * Ratio);

NEWH: = Round (Tempbmp.Height * Ratio);

end

Else

Begin

Result.Assign (BMP);

EXIT;

END;

IF center kil

Begin

Bmp.Width: = width;

bmp.height: = height;

Rt.left: = floor (abs (width-newwW) / 2);

RT.TOP: = floor (height-new) / 2);

Rt.right: = rt.Left neww; rt.bottom: = rt.top new;

end

Else

Begin

Bmp.Width: = neww;

bmp.height: = new;

Rt.Left: = 0;

Rt.top: = 0;

Rt.right: = bmp.width - 1;

Rt.bottom: = bmp.height - 1;

END;

if stype = '.jpg' Then

Bmp.canvas.stretchDraw (RT, Result)

Else

Bmp.canvas.stretchDraw (RT, TEMPBMP);

Result.compressionquality: = QUALITY

Result.Assign (BMP);

Finally

Tempbmp.free;

Bmp.free;

END;

END;

End.

转载请注明原文地址:https://www.9cbs.com/read-77287.html

New Post(0)