feature: document template QR Code image extensions

This commit is contained in:
Gary Sharp
2023-10-12 15:51:33 +11:00
parent 7353405b16
commit a3fb09440d
17 changed files with 299 additions and 65 deletions
@@ -1,4 +1,4 @@
using Disco.Models.BI.Expressions;
using Disco.Models.Services.Expressions.Extensions;
using System;
using System.Drawing;
using System.IO;
@@ -8,33 +8,35 @@ namespace Disco.Services.Expressions.Extensions.ImageResultImplementations
public abstract class BaseImageExpressionResult : IImageExpressionResult
{
public byte Quality { get; set; }
public bool LosslessFormat { get; set; }
public ImageExpressionFormat Format { get; set; }
public bool ShowField { get; set; }
public string BackgroundColour { get; set; }
public bool BackgroundPreferTransparent { get; set; }
public BaseImageExpressionResult()
{
LosslessFormat = false;
Format = ImageExpressionFormat.Jpeg;
Quality = 90;
ShowField = false;
BackgroundPreferTransparent = true;
}
public abstract Stream GetImage(int Width, int Height);
public abstract Stream GetImage();
public abstract MemoryStream GetImage(int width, int height);
public abstract MemoryStream GetImage(out int width, out int height);
protected Stream RenderImage(Image SourceImage, int Width, int Height)
protected MemoryStream RenderBitmapImage(Image SourceImage, int Width, int Height)
{
if (SourceImage == null)
throw new ArgumentNullException("SourceImage");
throw new ArgumentNullException(nameof(SourceImage));
if (Width <= 0)
throw new ArgumentOutOfRangeException("Width", "Width must be > 0");
throw new ArgumentOutOfRangeException(nameof(Width), "Width must be > 0");
if (Height <= 0)
throw new ArgumentOutOfRangeException("Height", "Height must be > 0");
throw new ArgumentOutOfRangeException(nameof(Height), "Height must be > 0");
if (Format != ImageExpressionFormat.Jpeg && Format != ImageExpressionFormat.Png)
throw new NotSupportedException($"The format {Format} is not supported by this method");
Brush backgroundBrush = null;
if (!LosslessFormat || !BackgroundPreferTransparent)
if (Format == ImageExpressionFormat.Jpeg || !BackgroundPreferTransparent)
{
if (string.IsNullOrEmpty(BackgroundColour))
backgroundBrush = Brushes.White;
@@ -44,22 +46,28 @@ namespace Disco.Services.Expressions.Extensions.ImageResultImplementations
using (Image resizedImage = SourceImage.ResizeImage(Width, Height, backgroundBrush))
{
return OutputImage(resizedImage);
return OutputBitmapImage(resizedImage);
}
}
protected Stream OutputImage(Image SourceImage)
protected MemoryStream OutputBitmapImage(Image SourceImage)
{
if (Format != ImageExpressionFormat.Jpeg && Format != ImageExpressionFormat.Png)
throw new NotSupportedException($"The format {Format} is not supported by this method");
MemoryStream imageStream = new MemoryStream();
if (LosslessFormat)
if (Format == ImageExpressionFormat.Png)
{ // Lossless Format - PNG
SourceImage.SavePng(imageStream);
}
else
else if (Format == ImageExpressionFormat.Jpeg)
{ // Lossy Format - JPG
var quality = Math.Min(100, Math.Max(1, (int)Quality));
SourceImage.SaveJpg(quality, imageStream);
}
else
throw new NotSupportedException($"Unexpected format {Format}");
imageStream.Position = 0;
return imageStream;
}