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

Write initial directory preview generator implementation

This commit is contained in:
Starbeamrainbowlabs 2016-08-10 20:58:09 +01:00
parent 438cb63bc3
commit 39d04706ed
3 changed files with 60 additions and 2 deletions

View file

@ -0,0 +1,22 @@
using System;
using System.Drawing;
namespace GalleryShare
{
public static class Extensions
{
public static PointF Multiply(this PointF a, PointF b)
{
return new PointF(
a.X * b.X,
a.Y * b.Y
);
}
public static Point ToIntPoint(this PointF source)
{
return new Point((int)source.X, (int)source.Y);
}
}
}

View file

@ -51,6 +51,7 @@
<Compile Include="RequestRouter\RouteSpecialFile.cs" />
<Compile Include="RequestRouter\RouteDirectoryListing.cs" />
<Compile Include="RequestRouter\RouteDefault.cs" />
<Compile Include="Extensions.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup />

View file

@ -5,9 +5,8 @@ using System.IO;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Net;
using System.Reflection;
using System.Collections.Generic;
namespace GalleryShare
{
@ -26,6 +25,30 @@ namespace GalleryShare
imageToSend.Dispose();
}
public static Image GenerateDirectoryThumbnail(string dirPath, Size thumbnailSize)
{
List<string> dirFiles = new List<string>(Directory.GetFiles(dirPath));
dirFiles.Sort( (a, b) => File.GetLastWriteTime(a).CompareTo(File.GetLastWriteTime(b)) );
Bitmap resultImage = new Bitmap(thumbnailSize.Width, thumbnailSize.Height);
using(Graphics context = Graphics.FromImage(resultImage))
{
for(int i = 0; i < 4; i++)
{
using (Image fileThumbnail = GenerateFileThumbnail(
dirFiles[i],
new Size(thumbnailSize.Width, thumbnailSize.Height)
))
{
PointF drawingPos = getDirectoryImageDrawPosition(i).Multiply(new PointF(resultImage.Width, resultImage.Height));
context.DrawImage(fileThumbnail, drawingPos.ToIntPoint());
}
}
}
return resultImage;
}
public static Image GenerateFileThumbnail(string imagePath, Size thumbnailBounds)
{
try {
@ -103,6 +126,18 @@ namespace GalleryShare
return mStream.ToArray();
}
}
private static PointF getDirectoryImageDrawPosition(int id)
{
switch(id)
{
case 0: return new PointF(0, 0);
case 1: return new PointF(0.5f, 0);
case 2: return new PointF(0.5f, 0.5f);
case 3: return new PointF(0, 0.5f);
default: throw new InvalidDataException($"Invalid id #{id}.");
}
}
}
}