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

