1
0
Fork 0
mirror of https://gitlab.com/sbrl/GalleryShare.git synced 2018-06-12 22:45:16 +00:00
GalleryShare/GalleryShare/ThumbnailGenerator.cs
Starbeamrainbowlabs 548c4829c6 Rewrite thumbnail generator again.
Apparently some wiring is broken in the request router though, as the thumbnail requests aren't going through correctly anymore.
2016-07-09 21:34:09 +01:00

41 lines
1.1 KiB
C#

using System;
using System.Threading.Tasks;
using System.Drawing;
using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace GalleryShare
{
public static class ThumbnailGenerator
{
public static void GenerateThumbnailPng(string imagePath, Size thumbnailBounds, Stream outputStream)
{
using (Bitmap rawImage = new Bitmap(imagePath)) {
float scaleFactor = Math.Min(
thumbnailBounds.Width / (float)rawImage.Width,
thumbnailBounds.Height / (float)rawImage.Height
);
Size thumbnailSize = new Size(
(int)(rawImage.Width * scaleFactor),
(int)(rawImage.Height * scaleFactor)
);
using (Bitmap smallImage = new Bitmap(thumbnailSize.Width, thumbnailSize.Height))
using (Graphics context = Graphics.FromImage(smallImage)) {
context.CompositingMode = CompositingMode.SourceCopy;
context.InterpolationMode = InterpolationMode.HighQualityBicubic;
context.DrawImage(rawImage, new Rectangle(
Point.Empty,
thumbnailSize
));
smallImage.Save(outputStream, ImageFormat.Png);
}
}
}
}
}