Mega Code Archive

 
Categories / Delphi / Examples
 

Bmptojpeg

basically it'sa a piece of piss to convert from bitmap to JPEG: Here's a teeny snippet that does it for me, loading a JPEG into a TImage. The same principle will work for other JPEG -> BMP ops: VAR TempJPEG : TJPEGImage; Begin ... TempJPEG := TJPEGImage.Create; TempJPEG.LoadFromFile(Filename); Image.Picture.Bitmap.Assign(TempJPEG); TempJPEG.Free; ... but... [re the complexities] ************************************************************************************* Code to load an image file such that if the image is in bitmap format then convert it to a JPEG (if it's already a JPEG don't convert it, obviously). Furthermore, we can change both the quality and the size of the image as required. The third parameter to the CopyToJpeg function determines the image quality, while all images bigger than 216 by 185 are scaled to 216 by 185. Note that the actual conversion from .bmp to .jpg format is actually just a single line of code. Richard Ebbs and Kristian Whittick Jan 2000 unit BMP2JPEG; interface uses Graphics, JPEG; type TWhatever = class(TObject) function CopyToJpeg(const Source, Dest: String; Quality: Integer): Boolean; private public end; example call: CopyToJpeg(EstateData.DataDir + FsourceName, WorkDir + FDestName, 0); function CopyToJpeg(const Source, Dest: String; Quality: Integer): Boolean; var FileLoadImg : TImage; ScaledImage : TImage; JPEGimg : TJPEGImage; scale : single; Myrect : Trect; NewWGap : integer; NewHGap : integer; begin FileLoadImg := TImage.Create(nil); ScaledImage := TImage.Create(nil); JPEGimg := TJPEGImage.Create; case quality of 0: begin JPEGimg.CompressionQuality := 100; ScaledImage.AutoSize := False; ScaledImage.Width := 216; ScaledImage.Height := 185; end; 1: begin JPEGimg.CompressionQuality := 50; ScaledImage.AutoSize := False; ScaledImage.Width := 432; ScaledImage.Height := 305; end; else JPEGimg.CompressionQuality := 100; ScaledImage.AutoSize := true; end; try JPEGimg.ProgressiveEncoding := True; FileLoadImg.Picture.LoadFromFile(Source); if (FileLoadImg.Width = 0) or (FileLoadImg.Height = 0) then begin result := False; exit; end; case quality of 0, 1: begin if (ScaledImage.Width / FileLoadImg.Width) > (ScaledImage.Height / FileLoadImg.Height) then scale := (ScaledImage.Height / FileLoadImg.Height) else scale := (ScaledImage.Width / FileLoadImg.Width); Myrect.Left := 0; // Empty Image Myrect.Top := 0; Myrect.Right := ScaledImage.Width; Myrect.Bottom := ScaledImage.Height; ScaledImage.Canvas.Brush.Color := clWhite; ScaledImage.Canvas.FillRect(MyRect); NewWGap := Round((ScaledImage.Width - (FileLoadImg.Width * Scale)) / 2); NewHGap := Round((ScaledImage.Height - (FileLoadImg.Height * Scale)) / 2); Myrect.Left := NewWGap; Myrect.Top := NewHGap; Myrect.Right := ScaledImage.Width - NewWGap; Myrect.Bottom := ScaledImage.height - NewHGap; ScaledImage.Canvas.StretchDraw(Myrect, FileLoadImg.Picture.Graphic); JPEGimg.Assign(ScaledImage.Picture.Bitmap); end; else JPEGimg.Assign(FileLoadImg.Picture.Bitmap); end; JPEGimg.JPEGNeeded; JPEGimg.SaveToFile(Dest); result := True; except result := False; end; FileLoadImg.Free; ScaledImage.Free; JPEGimg.Free; end; ************************************************************************************* Hi Nick, > Does anyone know how can I convert a BitMap (Bmp) to JPEG image? Delphi comes with a nice new component called TJPEGImage, which can be found in the JPEG unit. The TJPEGImage component is derived from TGraphic, and hence probably Assign-compatible with a TBitmap (also derived form TGraphic). The following code shows that this trick indeed works: we can "Assign" an instance of a TBitmap to an instance of a TJPEGImage. And using that information, we can write a simple CONSOLE application to convert a given .BMP file to a .JPG file as follows: {$APPTYPE CONSOLE} uses Classes, Graphics, JPEG, SysUtils; var Bitmap: TBitmap; Stream: TFileStream; begin writeln('BMP2JPG 1.0 by Bob Swart (aka Dr.Bob - www.drbob42.com)'); if ParamCount = 0 then begin writeln('Usage: BMP2JPG file.bmp [%%]'); writeln; writeln('Where "file.bmp" is the BMP file to convert to JPG,'); writeln('and %% is the compression quality (100% by default)') end else begin Bitmap := TBitmap.Create; try Bitmap.LoadFromFile(ParamStr(1)); with TJPEGImage.Create do try Assign(Bitmap); if ParamCount > 1 then try CompressionQuality := StrToInt(ParamStr(2)) except CompressionQuality := 100 end; Stream := TFileStream.Create(ChangeFileExt(ParamStr(1),'.JPG'),fmCreate); try SaveToStream(Stream) finally Stream.Free end finally Free end finally Bitmap.Free end end end. Note that the TBitmap has a LoadFromFile method that we can use to load a file, say DRBOB.BMP, into the bitmap, while we must use the SaveToStream method from the TJPEGImage to save the JPEG image to a file stream again (for some reason the latter doesn't appear to have a SaveToFile). Note also that the second command-line argument is used as the compression quality percentage, which is 100% by default (for a small raster with no loss of information). As an example, I converted the 44,280 bytes HANDSHAK.BMP file (from the IMAGES\SPLASH\256COLOR directory) to seven JPEG files with compression qualities 100%, 90%, 89%, 80%, 70%, 60% and 50%. With 50% the images was no longer good in quality. In practice, I use anything above 70% to get a good filesize reduction but with a good picture left as well. Experiment with your own BMP files to get a feeling... Groetjes, Bob Swart (aka Dr.Bob - www.drbob42.com) -- drs. Robert E. (Bob) Swart, @-Consultant, Delphi Trainer & Author Delphi OplossingsCentrum (DOC) & UK-BUG Borland User Group member *************************************************************************************** ENCRYPTING IMAGES I use the free ware THKStreams. I use this to compress my images and embed them right into my EXE. As well as compressing, THKStream gives an encryption option. http://kakoulidis.homepage.com Here is how I do this in my apps. 1. I take an image. 2. I compress the image to a HKStream compressed file. I can put more than one image in a file. Matter of fact, I put all 15 of my images in the same file with a tag for each image. 3. I wrote a simple tool that reads the file and builds a large CONST Byte Array in a Delphi Unit file. 4. I include the Unit in my program. 5. I stream the Byte array into THKStream. 6. I use HKStream to uncompress my images. 7. My images are available in memory for the life of the app with everything all in one EXE. Makes for easy install/deliver.