Let's take a look at this image conversion function. Why is it normal when BMP is converted to JPG? When JPG is converted to JPG, Delphi doesn't know it. Delphi/Windows SDK/API
Http://www.delphi2007.net/DelphiMultimedia/html/delphi_20061024164425208.html
Function pictureresiz (afilename {source image}, dfilename {converted image}: string): string;
VaR
Image: tpicture;
BMP, tmpbmp: tbitmap;
Adesiredfilename: string;
Begin
Image: = tpicture. Create;
BMP: = tbitmap. Create;
Tmpbmp: = tbitmap. Create;
Try
Image. loadfromfile (afilename );
BMP. Assign (image. Graphic );
Tmpbmp. Width: = liwidth; // specify the image width.
Tmpbmp. Height: = liheight; // specify the Image Height
Setstretchbltmode (tmpbmp. Canvas. Handle, stretch_deletescans); // Smooth Processing
Stretchblt (tmpbmp. Canvas. Handle, 0, 0, tmpbmp. Width, tmpbmp. height,
BMP. Canvas. Handle, 0, 0, BMP. Width, BMP. Height, srccopy );
Image. Graphic. Assign (tmpbmp );
Adesiredfilename: = dfilename;
If fileexists (adesiredfilename) Then deletefile (adesiredfilename );
Image. savetofile (adesiredfilename );
Result: = adesiredfilename;
Finally
BMP. Free;
Tmpbmp. Free;
Image. Free;
End;
End;
First, determine whether the file is BMP or JPG.
Then process them separately.
Uses
JPEG;
//--------------------
VaR
Myjpeg: tsf-image;
Mybitmap: tbitmap;
Begin
Mybitmap: = tbitmap. Create ();
Myjpeg: = tsf-image. Create ();
Mybitmap. loadfromfile ('C: \ windows \ Desktop \ AA. BMP '); // load the bitmap from a file
Myjpeg. Assign (mybitmap); // assign the bitmap to myjpeg object
// Myjpeg. Canvas. stretchdraw (trect (0, 0, nimagewidth, nimageheight), mybitmap );
Myjpeg. compressionquality: = strtoint ('75 ');
Myjpeg. Compress;
Myjpeg. savetofile ('C: \ windows \ Desktop \ test. jpg '); // Save the JPEG to disk
End;
Convert JPG to BMP and write it in turn.
The image size also needs to be changed during conversion. happyggy (tobject says: I am a god ~~~), YourCodeYou cannot set the image size. For example, to convert a jpg or BMP image of 500*500 to a jpg image of 100*100.
Add a variable
// ==================================
VaR
Myjpeg: tsf-image;
Mybitmap: tbitmap;
Desbitmap: tbitmap;
Begin
Myjpeg: = tsf-image. Create;
Mybitmap: = tbitmap. Create;
Desbitmap: = tbitmap. Create;
Myjpeg. loadfromfile ('C: \ 123.jpg ');
Mybitmap. Assign (myjpeg );
Desbitmap. Height: 100;
Desbitmap. Width: = 100;
Desbitmap. Canvas. copyrect (desbitmap. Canvas. cliprect, mybitmap. Canvas, mybitmap. Canvas. cliprect );
Image1.picture. Bitmap: = desbitmap;
Mybitmap. Free;
Myjpeg. Free;
Desbitmap. Free;
End;
Add group 32179979 or 26929954