Creating thumbnail images

CK CK 3788 
Created at
Updated at  
867 0 0

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;
Tags BitConverter Generate Thumbnail from Image Stream Thumbnail creation from image stream create a thumbnail thumbnail Facebook X
Comments 0
Similar posts
  1. How to encrypt a string
    469
  2. Delphi for .Net Code Folding keyboard shortcuts
    530
  3. Implementing C#'s foreach loop in Delphi 8
    463
  4. Sending email messages in .Net
    1,041
  5. Technology sharing related with software development. Specially content will be focused to programming language, and embedded systems.
    855
  6. How to resize image ?
    1,314
  7. What is Twitter Cards?
    405
  1. Delphi Pointer Types
    22,115
  2. How to read/wrtie INI file in Delphi ?
    3,126
  3. How to get file created time, modified time, and last accessed time ?
    3,352
  4. Delphi string conversion functions - AnsiToUTF-8, UTF8Encode, ...
    646
  5. When subject is crashing in TIdSMTP VCL
    622
  6. How to send text/html based email in UTF-8 with TIdSMTP, TIdMessage VCL
    1,084
  7. How to encode subject content when sending mail with TIdMessage ?
    1,077
  8. Base64 Encoding/Decoding function for Delphi
    1,176
  9. How to send email by TIdSMTP VCL ?
    1,023
  10. How to return exit code such as exit() function in C/C++ ?
    670
  11. How to get parameter string ?
    1,251
  12. Save TBitmap image to Jpeg format image in Delphi
    717
  13. How to get screen resolution in case of using multiple monitors ?
    454
  14. How to turn off monitor ?
    435
  15. Find File at certain directory in Delphi
    441
  16. How to speed up access to the RichTextBox Lines property
    833
  17. Window flashing
    380
  18. How to use TTF Font Without Installing
    1,124
  19. How to show window contents while dragging
    461
  20. How to save text from Clipboard to a file
    573
  21. How to send char as message to another application
    549
  22. How to restrict mouse movement
    415
  23. List Devices (LPT, COM ports, ...)
    478
  24. How to know whether the font is TrueTypeFont or not
    380
  25. How to track a user's idle time
    593
  26. How to remove your application from TaskBar
    659
  27. How to play sounds on the PC Speaker ?
    533
  28. How to install an INF file using Delphi ?
    469
  29. How to determine the output of a console application ?
    457
  30. Get text from the control at pos x,y on screen
    476
Recently updated
  1. Google OAuth Token Expiration: Causes and Solutions
    31
  2. Why 100% SEO Guarantees Better User Engagement and How to Achieve It in Your Service
    57
  3. Why SPF and DKIM Are Essential for Linux Email Servers?
    51
  4. How do I create animated background for Google Meet?
    23,221
  5. MaxMind GeoLite2 vs. DB-IP Lite vs. ip-api.com: Choosing the Right Free IP Geolocation Solution
    273
  6. Meet Valkey: Why Redis Changed Its License, Why Valkey is Better, and How to Use It as a Drop-in Replacement
    104
  7. How Rogue Web Scrapers Are Stealing My Content and Destroying My Server
    170
  8. Array of arguments passed to script in PHP
    99
  9. Invalid Auction Storms in Ad Tech
    107
  10. Building MRC-Compliant Instrumentation in Ad Tech: Implementation Steps and Key Challenges
    208
  11. The Two Clocks of Ad Server Architecture
    78
  12. Frequency Cap Architecture Overview
    527
  13. Collecting Proof-of-Play from 100,000 Screens
    153
  14. Vector Search in Relational Databases (RDBs): Trends & Usage Environments
    303
  15. What is RedisVL?
    116
  16. Designing a Skill Registry Architecture: Minimize Token Usage and Maximize AI Performance
    247
  17. TPU & GPU Cluster Management Tools
    258
  18. The Concept of Scaffolding in AI Agents
    245
  19. Beyond Static Pipelines: Architecting LLM Multi-Agent Systems for Autonomous Workflow Orchestration
    598
  20. AI-DLC vs. AI-SDLC: Model-Centric vs. System-Centric AI Development
    268
  21. Fine-Tuning Open-Weight LLMs: Training, Licensing, and Deploying Custom Models with Ollama
    354
  22. Inside the Next-Generation Streaming Architecture: Ultra-Low Latency, SGAI, and AI Codecs
    207
  23. Machine Learning: Training vs. Ranking
    230
  24. The Continuous Learning Machine: Designing a Loop That Actually Turns
    228
  25. The Next-Gen Low-Latency Standards: LL-HLS & MoQ
    203
  26. Designing a Data Curation System Around a Vector Database
    230
  27. Extracting Context from Live Video: The Architecture Got Simpler When We Stopped Building Features
    263
  28. The New Ad Stack Behind Live Sports Streaming: Inside SGAI, EABN, and the Standards Race to Fix Live Monetization
    674
  29. Designing MLOps Architectures to Handle Seasonal Data Drift in EV Fleet Models
    201
  30. From 15 to 40+ FPS: Accelerating PyTorch Object Detection on Automotive ECUs
    233