Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming

Creating thumbnail images (Delphi for .Net)

By Zarko Gajic, About.com


Suppose you have a Web Form where uploading of files is enabled, using the HtmlInputFile HTML control, and a user is uploading an image. If you accept only images of a specified size, you might need to shrink the uploaded image / create a thumnail.
Here's how to create thumbnail images in ASP.NET Delphi web applications.

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure GenerateThumbNail(const FileName : string; const ImageStream : Stream; const tWidth, tHeight : Double) ;
var
   g : System.Drawing.Image;
   thumbSize : Size;
   imgOutput : Bitmap;
   imgStream : MemoryStream;
begin
//This function creates the Thumbnail image and returns the
//image created in Byte() format

   g := System.Drawing.Image.FromStream(ImageStream) ;
   thumbSize := NewThumbSize(g.Width, g.Height, tWidth, tHeight) ;

   imgOutput := Bitmap.Create(g, thumbSize.Width, thumbSize.Height) ;
   imgStream := MemoryStream.Create;
   imgOutput.Save(imgStream, g.RawFormat) ;
   imgOutput.Save(Path.Combine('c:\LocationOnServer',FileName)) ;
   g.Dispose;
   imgOutput.Dispose;
end;

function NewThumbSize(const currentwidth, currentheight, newWidth, newHeight : Double) : Size;
var
   tempMultiplier : Double;
   NewSize : Size;
begin
   if currentheight > currentwidth Then
    tempMultiplier := newHeight / currentheight
   else
    tempMultiplier := newWidth / currentwidth;

   NewSize := Size.Create(Convert.ToInt32(currentwidth * tempMultiplier), Convert.ToInt32(currentheight * tempMultiplier)) ;
   Result := NewSize;
end;

//Usage:
GenerateThumbNail('ThumbnameOnServer', ImageFile.PostedFile.InputStream, thumbWidth, thumbHeight) ;

//Where
//ImageFile : System.Web.UI.HtmlControls.HtmlInputFile;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to Minimize ALL the windows on the Desktop
« How to extract the URL from an Internet Shortcut (.url) file

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming

More from About.com

Delphi Programming

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2004 Delphi Tips
  7. How to create a thumbnail image from a stream

©2008 About.com, a part of The New York Times Company.

All rights reserved.