Jeg lavede et script i går, som scanner en mappe og opretter to thumbnails for hvert billede. I dette tilfælde er det godt nok samme størrelse, hvor en af dem har et overlay, men du kan bruge koden som inspiration.
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.HtmlControls;
- using System.Web.UI.WebControls;
-
- public partial class _Default : System.Web.UI.Page
- {
- private static string pathImg = @"images/";
- private static string pathThumbs = pathImg + @"thumbs/";
- private static string pathActive = pathImg + @"actives/";
- private static string pathOverlay = @"system/overlay.png";
- private static int thumbnailHeight = 66;
- private static int thumbnailMaxWidth = 100;
-
- /// <summary>
- /// Generates thumbnails for a specified image
- /// </summary>
- /// <param name="file">Image to be resized</param>
- private void GenerateThumb(FileInfo file)
- {
- //Opens the image and specifies the type/format
- Bitmap bmp = new Bitmap(file.FullName);
- ImageFormat format = bmp.RawFormat;
-
- //Variables for calculating new size
- decimal ratio;
- int newWidth = thumbnailMaxWidth;
- int newHeight = thumbnailHeight;
-
- //Calculating new size
- if (bmp.Width > bmp.Height)
- {
- ratio = (decimal)newWidth / bmp.Width;
- newHeight = Convert.ToInt32(bmp.Height * ratio);
- }
- else
- {
- ratio = (decimal) newHeight / bmp.Height;
- newWidth = Convert.ToInt32(bmp.Width * ratio);
- }
-
- //Preparing to draw thumbnail in a new bitmap!
- Bitmap tmb = new Bitmap(newWidth, newHeight);
- Graphics g = Graphics.FromImage(tmb);
-
- //Quality and method
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
-
- //Draw the thumbnail
- g.DrawImage(bmp, 0, 0, newWidth, newHeight);
-
- //Gets rid of garbage - nessarily before saving!
- g.Dispose();
- bmp.Dispose();
-
- //Saving the thumbnail
- tmb.Save(Page.MapPath(pathThumbs) + file.Name, format);
-
- //Opens the overlay, creates a new bitmap and prepares for drawing
- Bitmap bmpOverlay = new Bitmap(Server.MapPath(pathOverlay));
- Bitmap tmbActive = new Bitmap(newWidth, newHeight);
- g = Graphics.FromImage(tmbActive);
-
- //Same quality and method as above
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
-
- //Drawing twhice, first copy the thumbnail, then the overlay
- g.DrawImage(tmb, 0, 0, newWidth, newHeight);
- g.DrawImage(bmpOverlay, 0, 0, newWidth, newHeight);
-
- //Getting rid of garbage - Where is my butler?!
- g.Dispose();
- tmb.Dispose();
- bmpOverlay.Dispose();
-
- //Saves the thumbnail with overlay!
- tmbActive.Save(Page.MapPath(pathActive) + file.Name, format);
-
- //Cleaning up
- tmbActive.Dispose();
- }
- }
Jeg har klippet OnPageLoad og det der scanner mapperne ud, da det ikke er det der er relevant for dig.