You can find all the details at: Using System.Drawing with ASP.NET Core or ASP.NET Service
Last active
May 22, 2021 20:55
-
-
Save aspose-com-gists/e5a714ced9148321c908cf1be53d034f to your computer and use it in GitHub Desktop.
Using System.Drawing with ASP.NET Core or ASP.NET Service | Using Aspose.Drawing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// The result of filtered thumbnail generation. | |
/// </summary> | |
public class FilterThumbnailResult | |
{ | |
/// <summary> | |
/// Initializes a new instance of the FilterThumbnailResult class. | |
/// </summary> | |
/// <param name="filterType">The type of filter applied to the thumbnail.</param> | |
/// <param name="pngData">The image data in PNG format.</param> | |
/// <param name="width">The width of the thumbnail.</param> | |
/// <param name="height">The height of the thumbnail.</param> | |
public FilterThumbnailResult(FilterType filterType, byte[] pngData, int width, int height) | |
{ | |
this.FilterType = filterType; | |
this.PngData = pngData; | |
this.Width = width; | |
this.Height = height; | |
} | |
/// <summary> | |
/// Gets the type of filter applied to the thumbnail. | |
/// </summary> | |
public FilterType FilterType { get; } | |
/// <summary> | |
/// Gets the image data in PNG format. | |
/// </summary> | |
public byte[] PngData { get; } | |
/// <summary> | |
/// Gets the width of the thumbnail. | |
/// </summary> | |
public int Width { get; } | |
/// <summary> | |
/// Gets the height of the thumbnail. | |
/// </summary> | |
public int Height { get; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Available types of the filters. | |
/// </summary> | |
public enum FilterType | |
{ | |
/// <summary> | |
/// Filter for generation of image saturated with red color. | |
/// </summary> | |
Red, | |
/// <summary> | |
/// Filter for generation of image saturated with green color. | |
/// </summary> | |
Green, | |
/// <summary> | |
/// Filter for generation of image saturated with blue color. | |
/// </summary> | |
Blue, | |
/// <summary> | |
/// Filter for generation of a grayscale image. | |
/// </summary> | |
Grayscale, | |
/// <summary> | |
/// Filter for generation of a negative image. | |
/// </summary> | |
Negative, | |
/// <summary> | |
/// Filter for generation of a sepia image. | |
/// </summary> | |
Sepia, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// Generates a set of thumbnails for all available filters. | |
/// </summary> | |
/// <param name="sourceImage">Bytes of the source image.</param> | |
/// <param name="width">Requested width of the preview images.</param> | |
/// <param name="height">Requested height of the preview images.</param> | |
/// <returns>A set of thumbnails.</returns> | |
public IEnumerable<FilterThumbnailResult> GenerateAllThumbnails(byte[] sourceImage, int width, int height) | |
{ | |
using (MemoryStream ms = new MemoryStream(sourceImage)) | |
using (Bitmap bitmap = new Bitmap(ms)) | |
{ | |
ConcurrentBag<FilterThumbnailResult> collection = new ConcurrentBag<FilterThumbnailResult>(); | |
Parallel.ForEach(Processors, (processor) => | |
{ | |
using (Bitmap previewBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) | |
using (Graphics graphics = Graphics.FromImage(previewBitmap)) | |
{ | |
graphics.DrawImage(bitmap, 0, 0, width, height); | |
collection.Add(processor(previewBitmap)); | |
} | |
}); | |
return collection; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
/// <summary> | |
/// The interface for thumbnails images generation. | |
/// </summary> | |
public interface IThumbnailGenerator | |
{ | |
/// <summary> | |
/// Generates a set of thumbnails for all available filters. | |
/// </summary> | |
/// <param name="sourceImage">Bytes of the source image.</param> | |
/// <param name="width">Requested width of the preview images.</param> | |
/// <param name="height">Requested height of the preview images.</param> | |
/// <returns>A set of thumbnails.</returns> | |
IEnumerable<FilterThumbnailResult> GenerateAllThumbnails(byte[] sourceImage, int width, int height); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// <summary> | |
/// IThumbnailGenerator interface implementation using System.Drawing. | |
/// </summary> | |
public class ThumbnailGenerator | |
: IThumbnailGenerator | |
{ | |
/// <summary> | |
/// A set of thumbnail processors. | |
/// </summary> | |
private static readonly FilterThumbnailProcessor[] Processors = new FilterThumbnailProcessor[] | |
{ | |
ApplyRedFilter, | |
ApplyGreenFilter, | |
ApplyBlueFilter, | |
ApplyGrayFilter, | |
ApplyNegativeFilter, | |
ApplySepiaFilter, | |
}; | |
/// <summary> | |
/// Delegate for thumbnail processing. | |
/// </summary> | |
/// <param name="thumbnail">Thumbnail of the original image.</param> | |
/// <returns>Result of filtered thumbnail generation.</returns> | |
private delegate FilterThumbnailResult FilterThumbnailProcessor(Bitmap thumbnail); | |
/// <summary> | |
/// Generates a set of thumbnails for all available filters. | |
/// </summary> | |
/// <param name="sourceImage">Bytes of the source image.</param> | |
/// <param name="width">Requested width of the preview images.</param> | |
/// <param name="height">Requested height of the preview images.</param> | |
/// <returns>A set of thumbnails.</returns> | |
public IEnumerable<FilterThumbnailResult> GenerateAllThumbnails(byte[] sourceImage, int width, int height) | |
{ | |
using (MemoryStream ms = new MemoryStream(sourceImage)) | |
using (Bitmap bitmap = new Bitmap(ms)) | |
{ | |
foreach (var processor in Processors) | |
{ | |
using (Bitmap previewBitmap = new Bitmap(width, height, PixelFormat.Format32bppArgb)) | |
using (Graphics graphics = Graphics.FromImage(previewBitmap)) | |
{ | |
graphics.DrawImage(bitmap, 0, 0, width, height); | |
yield return processor(previewBitmap); | |
} | |
} | |
} | |
} | |
/// <summary> | |
/// Applies a red filter to thumbnail. | |
/// </summary> | |
/// <param name="thumbnail">Thumbnail of the original image.</param> | |
/// <returns>Result of filtered thumbnail generation.</returns> | |
private static FilterThumbnailResult ApplyRedFilter(Bitmap thumbnail) | |
{ | |
... | |
} | |
/// <summary> | |
/// Applies a green filter to thumbnail. | |
/// </summary> | |
/// <param name="thumbnail">Thumbnail of the original image.</param> | |
/// <returns>Result of filtered thumbnail generation.</returns> | |
private static FilterThumbnailResult ApplyGreenFilter(Bitmap thumbnail) | |
{ | |
... | |
} | |
/// <summary> | |
/// Applies a blue filter to thumbnail. | |
/// </summary> | |
/// <param name="thumbnail">Thumbnail of the original image.</param> | |
/// <returns>Result of filtered thumbnail generation.</returns> | |
private static FilterThumbnailResult ApplyBlueFilter(Bitmap thumbnail) | |
{ | |
... | |
} | |
/// <summary> | |
/// Applies a gray filter to thumbnail. | |
/// </summary> | |
/// <param name="thumbnail">Thumbnail of the original image.</param> | |
/// <returns>Result of filtered thumbnail generation.</returns> | |
private static FilterThumbnailResult ApplyGrayFilter(Bitmap thumbnail) | |
{ | |
... | |
} | |
/// <summary> | |
/// Applies a negative filter to thumbnail. | |
/// </summary> | |
/// <param name="thumbnail">Thumbnail of the original image.</param> | |
/// <returns>Result of filtered thumbnail generation.</returns> | |
private static FilterThumbnailResult ApplyNegativeFilter(Bitmap thumbnail) | |
{ | |
... | |
} | |
/// <summary> | |
/// Applies a sepia filter to thumbnail. | |
/// </summary> | |
/// <param name="thumbnail">Thumbnail of the original image.</param> | |
/// <returns>Result of filtered thumbnail generation.</returns> | |
private static FilterThumbnailResult ApplySepiaFilter(Bitmap thumbnail) | |
{ | |
... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment