From f0f9fa2ef1900c332a47b1414db8380f3c1c0487 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Wed, 27 Sep 2017 13:31:54 +0200 Subject: [PATCH] Added base64ing of thumbnailed logos --- Logic/Logic.csproj | 1 + Logic/Support/Document.cs | 10 +++ Logic/Support/DocumentImage.cs | 85 +++++++++++++++++++ .../v5/Security/ChangeOrganizations.aspx | 2 +- .../v5/Security/ChangeOrganizations.aspx.cs | 9 +- 5 files changed, 99 insertions(+), 8 deletions(-) create mode 100644 Logic/Support/DocumentImage.cs diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 7791cb78..250e9a0c 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -197,6 +197,7 @@ + diff --git a/Logic/Support/Document.cs b/Logic/Support/Document.cs index ea48667c..2d4ded22 100644 --- a/Logic/Support/Document.cs +++ b/Logic/Support/Document.cs @@ -178,6 +178,11 @@ namespace Swarmops.Logic.Support return GetReader (Encoding.GetEncoding (encodingCodePage)); } + public StreamReader GetReader() + { + return new StreamReader(StorageRoot + ServerFileName); + } + public Person UploadedByPerson { get @@ -190,5 +195,10 @@ namespace Swarmops.Logic.Support return null; } } + + public DocumentImage Image + { + get { return new DocumentImage(this); } + } } } \ No newline at end of file diff --git a/Logic/Support/DocumentImage.cs b/Logic/Support/DocumentImage.cs new file mode 100644 index 00000000..f725784c --- /dev/null +++ b/Logic/Support/DocumentImage.cs @@ -0,0 +1,85 @@ +using System; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Collections.Generic; +using System.Drawing.Imaging; +using System.IO; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Swarmops.Logic.Support +{ + public class DocumentImage + { + public DocumentImage(Document parent) + { + this.Parent = parent; + } + + private Image GetImage() + { + Image image = null; + using (StreamReader docReader = Parent.GetReader()) + { + image = Image.FromStream(docReader.BaseStream); + } + + return image; + } + + private static Image ScaleImage(Image image, int maxWidth, int maxHeight) + { + double ratio = (double)maxWidth / image.Width; + + if (maxHeight > 0) + { + double ratioY = (double) maxHeight/image.Height; + if (ratioY < ratio) + { + ratio = ratioY; + } + } + + int newWidth = (int) (image.Width*ratio); + int newHeight = (int) (image.Height*ratio); + + Image newImage = new Bitmap(newWidth, newHeight); + + using (Graphics g = Graphics.FromImage(newImage)) + { + g.DrawImage(image, 0, 0, newWidth, newHeight); + } + + return newImage; + + } + + public string GetBase64(int width, int minHeight = 0) + { + string cacheKey = String.Format("DocumentImage-Base64-{0}-{1}-{2}", Parent.Identity, width, minHeight); + + // Do we have the requested item in cache? + + string base64 = (string) Cache.GuidCache.Get(cacheKey); + if (!string.IsNullOrEmpty(base64)) + { + return base64; + } + + // No, so construct it + + using (Image image = ScaleImage(GetImage(), width, minHeight)) + { + MemoryStream stream = new MemoryStream(); + image.Save(stream, ImageFormat.Png); + base64 = Convert.ToBase64String(stream.ToArray()); + } + + Cache.GuidCache.Set(cacheKey, base64); + return base64; + } + + public Document Parent { get; private set; } + } +} diff --git a/Site/Pages/v5/Security/ChangeOrganizations.aspx b/Site/Pages/v5/Security/ChangeOrganizations.aspx index 619d1dc0..efb4b1ab 100644 --- a/Site/Pages/v5/Security/ChangeOrganizations.aspx +++ b/Site/Pages/v5/Security/ChangeOrganizations.aspx @@ -71,7 +71,7 @@
';"> - +
<%# Eval("OrganizationName") %>
diff --git a/Site/Pages/v5/Security/ChangeOrganizations.aspx.cs b/Site/Pages/v5/Security/ChangeOrganizations.aspx.cs index d209075b..bab10dae 100644 --- a/Site/Pages/v5/Security/ChangeOrganizations.aspx.cs +++ b/Site/Pages/v5/Security/ChangeOrganizations.aspx.cs @@ -50,13 +50,8 @@ namespace Swarmops.Frontend.Pages.v5.Security string logoUrl = "/Images/Other/blank-logo-640x360.png"; Document logoLandscape = organization.LogoLandscape; - if (logoLandscape != null) - { - logoUrl = "/Support/StreamUpload.aspx?DocId=" + - logoLandscape.Identity.ToString(CultureInfo.InvariantCulture); - } - newOrganizationParameters.LogoUrl = logoUrl; + newOrganizationParameters.LogoImage = logoLandscape.Image.GetBase64(70, 48); newOrganizationParameters.OrganizationId = membership.OrganizationId; newOrganizationParameters.OrganizationName = membership.Organization.Name; @@ -71,7 +66,7 @@ namespace Swarmops.Frontend.Pages.v5.Security private class OrganizationParameters { - public string LogoUrl { get; set; } + public string LogoImage { get; set; } public int OrganizationId { get; set; } public string OrganizationName { get; set; } }