Added base64ing of thumbnailed logos

This commit is contained in:
Rick Falkvinge
2017-09-27 13:32:24 +02:00
parent e63f3e50c6
commit f0f9fa2ef1
5 changed files with 99 additions and 8 deletions
+1
View File
@@ -197,6 +197,7 @@
<Compile Include="Security\Impersonation.cs" />
<Compile Include="SocketMessage.cs" />
<Compile Include="Support\BlockchainTransaction.cs" />
<Compile Include="Support\DocumentImage.cs" />
<Compile Include="Support\ErrorMessages.cs" />
<Compile Include="Support\LogEntries\ImpersonationLogEntry.cs" />
<Compile Include="Support\LogEntries\PersonalDataChangedLogEntry.cs" />
+10
View File
@@ -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); }
}
}
}
+85
View File
@@ -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; }
}
}
@@ -71,7 +71,7 @@
<asp:Repeater runat="server" ID="RepeaterOrganizations">
<ItemTemplate>
<div class="divOrganizationEncapsulation" onclick="document.location='/Pages/v5/Security/SetCurrentOrganization.aspx?OrganizationId=<%# Eval("OrganizationId") %>';">
<div class="divOrganizationLogo" style="background-image:url('<%# Eval("LogoUrl")%>')" ></div>
<div class="divOrganizationLogo" style="background-image:url(data:image/png;base64,<%# Eval("LogoImage")%>')" ></div>
<div class="spanOrganizationName"><%# Eval("OrganizationName") %></div>
</div>
</ItemTemplate>
@@ -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; }
}