1
0
Fork 0
mirror of https://gitlab.com/sbrl/GalleryShare.git synced 2018-06-12 22:45:16 +00:00

Rewrite thumbnail generator again.

Apparently some wiring is broken in the request router though, as the thumbnail requests aren't going through correctly anymore.
This commit is contained in:
Starbeamrainbowlabs 2016-07-09 21:34:09 +01:00
parent 620cf2f892
commit 548c4829c6
4 changed files with 50 additions and 12 deletions

View file

@ -9,7 +9,7 @@ using System.Threading;
using System.Xml;
using System.Reflection;
using ImageMagick;
using System.Drawing;
namespace GalleryShare
{
@ -27,8 +27,7 @@ namespace GalleryShare
int port;
string servingDirectory = Environment.CurrentDirectory;
int thumbnailWidth = 300;
int thumbnailHeight = 200;
Size thumbnailSize = new Size(300, 200);
HttpListener server = new HttpListener();
string prefix;
@ -227,11 +226,9 @@ namespace GalleryShare
if(cycle.Request.QueryString["type"] == "thumbnail")
{
// Send a thumbnail!
MagickImage img = new MagickImage(requestedPath);
img.Thumbnail(new MagickGeometry(thumbnailWidth, thumbnailHeight));
cycle.Response.ContentType = "image/webp";
img.Write(cycle.Response.OutputStream, MagickFormat.WebP);
img.Dispose();
Console.WriteLine("Sending thumbnail for '{0}'", requestedPath);
cycle.Response.ContentType = "image/png";
ThumbnailGenerator.GenerateThumbnailPng(requestedPath, thumbnailSize, cycle.Response.OutputStream);
return;
}
@ -241,7 +238,6 @@ namespace GalleryShare
Stream fileData = File.OpenRead(requestedPath);
await fileData.CopyToAsync(cycle.Response.OutputStream);
}
}
}

View file

@ -37,15 +37,13 @@
<HintPath>packages\MimeSharp.1.0.0\lib\MimeSharp.dll</HintPath>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="Magick.NET-Q16-AnyCPU">
<HintPath>..\packages\Magick.NET-Q16-AnyCPU.7.0.2.100\lib\net40-client\Magick.NET-Q16-AnyCPU.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="GalleryServer.cs" />
<Compile Include="Utilities.cs" />
<Compile Include="ThumbnailGenerator.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="packages\Magick.NET-Q8-x64.7.0.2.100\build\net40-client\Magick.NET-Q8-x64.targets" Condition="Exists('packages\Magick.NET-Q8-x64.7.0.2.100\build\net40-client\Magick.NET-Q8-x64.targets')" />

View file

@ -0,0 +1,40 @@
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);
}
}
}
}
}

View file

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="MimeSharp" version="1.0.0" targetFramework="net45" />
</packages>