Solved

Thumbnails?

Posted on 2001-06-23
2
388 Views
Last Modified: 2010-04-06

My program works with own custom documents and sometimes user creates templates from them.
also there is a background picture into documents. I want to make 'new from template' dialog form that shows thumbnails of my document;s background. SO my question is :
-how to create thumbnail from my background pictures (jpegs)?

thanks in advance.
0
Comment
Question by:kzdown
2 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 45 total points
ID: 6221390
Form Delphi3000.com

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<1 then
    raise Exception.Create('Invalid Width');
  if Height<1 then
    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 Ratio>1 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.

Best Regards

Cesario
0
 
LVL 22

Expert Comment

by:Mohammed Nasman
ID: 6222475
Hello

  Here's procedure called MakeThumb, just pass the two images as source and target and the thumbnail size

//=====
procedure MakeThumb(src,dst:TImage; size:integer);
var tr:TRect;
newwidth,newheight,orgwidth,orgheight:integer;
begin
 orgwidth:=src.width;
 orgheight:=src.height;
 if (orgheight/orgwidth > 1) then begin
   newheight:=size;
   newwidth:=round(orgwidth*(newheight/orgheight));
  end
 else begin
   newwidth:=size;
   newheight:=round(orgheight*(newwidth/orgwidth));
  end;
 dst.autosize:=false;
 dst.stretch:=false;
 dst.width:=size;
 dst.height:=size;
 dst.canvas.pen.color:=clwhite;
 dst.canvas.brush.color:=clwhite;
 if (newwidth < size)then begin
   tr.left:=(size-newwidth)div 2;
   tr.right:=tr.left+newwidth;
   tr.top:=0;tr.bottom:=size;
   dst.canvas.rectangle(0,0,tr.left,size);    // fill white
   dst.canvas.rectangle(tr.right,0,size,size);// fill white
  end;
 if newheight > size then begin
   tr.left:=0;tr.right:=size;
   tr.top:=(size-newheight)div 2;
   tr.bottom:=tr.top+newheight;
   dst.canvas.rectangle(0,0,size,tr.top);      // fill white
   dst.canvas.rectangle(0,tr.bottom,size,size);// fill white
  end;
 dst.canvas.stretchdraw(tr,src.picture.graphic);
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  makethumb(image1,Image2,100);
end;

Best regards
Mohammed nasman
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Sending Gmail through Delphi 3 90
When i run adoquery my application freezes 26 150
Dynamically Created Query 3 48
Graphics32 under Delphi 10.1 Berlin 2 34
A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

947 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now