Solved

Thumbnails?

Posted on 2001-06-23
2
404 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
QRReport  TQrmemo vertical stretching 1 57
Multiple image collision 13 89
RESTRequest Parameter 4 54
Delphi Firemonkey : user drawing in window 3 27
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…
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…

726 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