Solved

Thumbnails?

Posted on 2001-06-23
2
411 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

696 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