Mega Code Archive

 
Categories / Delphi / Graphic
 

How to create JPEG image thumbnails programmatically

Title: How to create JPEG image thumbnails programmatically Question: This article shows how to create thumbnails of JPEG images programmatically. Scaling is not good, because it just displays stretched image, leaving actual image intact. Answer: Sometimes you need to create thumbnails of JPEG images. Using Scale property of TJpegImage class is not good, because it actually does not change the image itself, it just displays scaled image. Following unit can be used to create thumbnails programmatically. it does not do any drawing, so it can be used as in GUI, as in console, CGI and ISAPI applications. It can be very useful in web applications. If you have images in the database, you can create CGI or ISAPI app, which seds to the browser thumbnail, not actual image. Below is a source code of the unit called JpegConv.pas. Full unit, and a demo application, can be downloaded by using following direct link: http://www.argosoft.com/files/misc/thumbs.zip ------------------ unit JpegConv; interface uses Windows, Graphics, SysUtils, Classes; procedure CreateThumbnail(InStream, OutStream: TStream; Width, Height: Integer; FillColor: TColor=clWhite); overload; procedure CreateThumbnail(const InFileName, OutFileName: string; Width, Height: Integer; FillColor: TColor=clWhite); overload; implementation uses Jpeg; procedure CreateThumbnail(InStream, OutStream: TStream; Width, Height: Integer; FillColor: TColor=clWhite); var JpegImage: TJpegImage; Bitmap: TBitmap; Ratio: Double; ARect: TRect; AHeight, AHeightOffset: Integer; AWidth, AWidthOffset: Integer; begin // Check for invalid parameters if Width raise Exception.Create('Invalid Width'); if Height raise Exception.Create('Invalid Height'); JpegImage:=TJpegImage.Create; try // Load the image JpegImage.LoadFromStream(InStream); // Create bitmap, and calculate parameters Bitmap:=TBitmap.Create; try Ratio:=JpegImage.Width/JpegImage.Height; if Ratio1 then begin AHeight:=Round(Width/Ratio); AHeightOffset:=(Height-AHeight) div 2; AWidth:=Width; AWidthOffset:=0; end else begin AWidth:=Round(Height*Ratio); AWidthOffset:=(Width-AWidth) div 2; AHeight:=Height; AHeightOffset:=0; end; Bitmap.Width:=Width; Bitmap.Height:=Height; Bitmap.Canvas.Brush.Color:=FillColor; Bitmap.Canvas.FillRect(Rect(0,0,Width,Height)); // StretchDraw original image ARect:=Rect(AWidthOffset,AHeightOffset,AWidth+AWidthOffset,AHeight+AHeightOffset); Bitmap.Canvas.StretchDraw(ARect,JpegImage); // Assign back to the Jpeg, and save to the file JpegImage.Assign(Bitmap); JpegImage.SaveToStream(OutStream); finally Bitmap.Free; end; finally JpegImage.Free; end; end; procedure CreateThumbnail(const InFileName, OutFileName: string; Width, Height: Integer; FillColor: TColor=clWhite); overload; var InStream, OutStream: TFileStream; begin InStream:=TFileStream.Create(InFileName,fmOpenRead); try OutStream:=TFileStream.Create(OutFileName,fmOpenWrite or fmCreate); try CreateThumbnail(InStream,OutStream,Width,Height,FillColor); finally OutStream.Free; end; finally InStream.Free; end; end; end. ---------------------------