Class ImageUtilities
Inherited Members
Namespace: SuperOffice.Util
Assembly: SoCore.dll
Syntax
public static class ImageUtilities
Fields
ProductImageSize
Default max size for product images (largest edge)
Declaration
public const int ProductImageSize = 2000
Field Value
| Type | Description |
|---|---|
| int |
ThumbnailSize
Default thumbnail size
Declaration
public const int ThumbnailSize = 150
Field Value
| Type | Description |
|---|---|
| int |
Methods
FitTo(Image, int, int)
If needed, resize the image to make it fit the given dimensions, while keeping the aspect ratio.
If you are creating thumbnails, please use the GetThumbnail(Image, int, int), which is more efficient for radical scaling of that sort.Declaration
public static Image FitTo(this Image image, int width, int height)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | image | Source image |
| int | width | Maximum width |
| int | height | Maximum height |
Returns
| Type | Description |
|---|---|
| Image | Possibly rescaled image |
GetAspect(int, int)
Get the aspect of an image.
Declaration
public static double GetAspect(int width, int height)
Parameters
| Type | Name | Description |
|---|---|---|
| int | width | |
| int | height |
Returns
| Type | Description |
|---|---|
| double | Aspect for an image (width/height). 1 is a squere. |
GetBytes(Image)
Declaration
public static byte[] GetBytes(this Image input)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | input |
Returns
| Type | Description |
|---|---|
| byte[] |
GetClearPixel(Size)
Get an image containing a transperant background
Declaration
public static Image GetClearPixel(Size size)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | size |
Returns
| Type | Description |
|---|---|
| Image |
GetImageTypeFromBase64String(string, string)
Determine image type from the first few bytes of the image data. Note: not the media-type, but the image file extension (gif, png, jpeg, tiff).
Declaration
public static string GetImageTypeFromBase64String(string base64String, string defaultType)
Parameters
| Type | Name | Description |
|---|---|---|
| string | base64String | |
| string | defaultType | "gif", "png", "jpeg", or "tiff" |
Returns
| Type | Description |
|---|---|
| string | "gif", "png", "jpeg", or "tiff" |
GetImageTypeFromBytes(byte[], string)
Determine image type from the first few bytes of the image data. Note: not the media-type, but the image file extension (gif, png, jpeg, tiff).
Declaration
public static string GetImageTypeFromBytes(byte[] bytes, string defaultType)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | bytes | Image data |
| string | defaultType | "gif", "png", "jpeg", or "tiff" |
Returns
| Type | Description |
|---|---|
| string | "gif", "png", "jpeg", or "tiff" |
GetIntersectionRectangle(Size, Size, VerticalAlignment)
Get the Rectangle in the destination that is covered by the source.
The arguments represents the size of two rectangles that are centered on top of each other.
Declaration
public static Rectangle GetIntersectionRectangle(Size destination, Size source, ImageUtilities.VerticalAlignment align)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | destination | Size of the destination rectangle. |
| Size | source | Size of the source rectangle |
| ImageUtilities.VerticalAlignment | align |
Returns
| Type | Description |
|---|---|
| Rectangle | Rectangle in the destination that is covered by the source. |
GetScaledImageOrDefaultImage(Image, ImageSize, FallbackStrategy, Func<Image>, bool, Color)
Get image or default image provided by a callback function.
Declaration
public static Image GetScaledImageOrDefaultImage(Image image, ImageUtilities.ImageSize imageSize, ImageUtilities.FallbackStrategy fallbackStrategy, Func<Image> defaultImageFunc, bool borderOnSrNoPhoto, Color borderColor)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | image | The image - which may be null. |
| ImageUtilities.ImageSize | imageSize | Desired returned image size. 0 = no change |
| ImageUtilities.FallbackStrategy | fallbackStrategy | Strategy to use if there are no pictures available. Null = return NULL value instead of a default or blank image. |
| Func<Image> | defaultImageFunc | Returns a default image when called. NULL means no default image -- get a transparent bitmap instead. |
| bool | borderOnSrNoPhoto | Should there be a border on the image created if the SR_NO_PHOTO is used. |
| Color | borderColor |
Returns
| Type | Description |
|---|---|
| Image | Scaled Image or scaled default/fallback image, or blank transparent image. |
Remarks
This function does not access the database. Does not require a session. Entirely in-memory.
GetSrNoPhoto(Size, Color, Color, bool, string)
Get an image containing the string represented by the resource value of SR_NO_PHOTO.
Declaration
public static Image GetSrNoPhoto(Size size, Color bgColor, Color borderColor, bool hasBorder, string text = "NO PHOTO")
Parameters
| Type | Name | Description |
|---|---|---|
| Size | size | Size of the image. |
| Color | bgColor | Background color for the image. |
| Color | borderColor | |
| bool | hasBorder | |
| string | text |
Returns
| Type | Description |
|---|---|
| Image | Image containing the text represented by the resource value of SR_NO_PHOTO. |
GetTextImage(Size, string, Font, Color, Color, Color, bool)
Get an image containing a text center both horizontally and vertically.
Declaration
public static Image GetTextImage(Size size, string text, Font font, Color textColor, Color bgColor, Color borderColor, bool border)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | size | Size of the image |
| string | text | Text to write in the image. |
| Font | font | Font to use when writing the text. |
| Color | textColor | The color to use when writing the text. |
| Color | bgColor | The background color of the image. |
| Color | borderColor | |
| bool | border |
Returns
| Type | Description |
|---|---|
| Image | An image containing a text center both horizontally and vertically. |
GetThumbnail(Image, int, int)
Get a thumbnail of the incoming image; the quiality of the scaling is not quite as good as for FitTo(Image, int, int), but is more efficient. Aspect ratio is maintained.
Declaration
public static Image GetThumbnail(this Image image, int width = 150, int height = 150)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | image | Source image |
| int | width | Maximum width, default ThumbnailSize |
| int | height | Maximum height, default ThumbnailSize |
Returns
| Type | Description |
|---|---|
| Image | Thumbnail image |
GetThumbnail(string, int, int)
Get a thumbnail from a string-encoded image; output size is not guaranteed as short input strings will cause an immediate return in the interest of performance (a string of less than 1500 can't be a big image!)
Declaration
public static string GetThumbnail(string input, int width = 150, int height = 150)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | Source image as base-64 encoded string |
| int | width | Maximum width, default ThumbnailSize |
| int | height | Maximum height, default ThumbnailSize |
Returns
| Type | Description |
|---|---|
| string |
GetThumbnailImage(Stream, int, int)
Declaration
public static Image GetThumbnailImage(this Stream input, int width = 150, int height = 150)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | input | |
| int | width | |
| int | height |
Returns
| Type | Description |
|---|---|
| Image |
GetThumbnailImage(string, int, int)
Get a thumbnail from a string-encoded image; output size is not guaranteed as short input strings will cause an immediate return in the interest of performance (a string of less than 1500 can't be a big image!)
Declaration
public static Image GetThumbnailImage(string input, int width = 150, int height = 150)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | Source image as base-64 encoded string |
| int | width | Maximum width, default ThumbnailSize |
| int | height | Maximum height, default ThumbnailSize |
Returns
| Type | Description |
|---|---|
| Image |
Max(Size, Size)
Get the minimum value for height and width induvidually. Values of 0 are considered none-values.
Declaration
public static Size Max(Size s1, Size s2)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | s1 | First size element |
| Size | s2 | Second size element |
Returns
| Type | Description |
|---|---|
| Size | Resulting Size |
Min(Size, Size)
Get the minimum value for height and width induvidually. Values of 0 are considered none-values.
Declaration
public static Size Min(Size s1, Size s2)
Parameters
| Type | Name | Description |
|---|---|---|
| Size | s1 | First size element |
| Size | s2 | Second size element |
Returns
| Type | Description |
|---|---|
| Size | Resulting Size |
ResolveUrlOrBase64ImageAsync(string, int, CancellationToken)
If the input is a well-formed absolute URI, then assume that it leads to an image, and fetch that image; otherwise just pass the input straight out again. Images are scaled down to 1000/1000 if needed
Declaration
public static Task<string> ResolveUrlOrBase64ImageAsync(string input, int maxPixelSize = 2000, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The input, which may be a base64 encoded image or a URL |
| int | maxPixelSize | Maximum image size, of longest side. Images larger than this in any direction will be shrunk (keeping the aspect ratio intact). Stretching will never occur. |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string> |
Remarks
Eventually this should do some caching, have a cache-retention policy, flush etc... but in this prototype version we just fetch the data directly
ResolveUrlOrBase64ImageNoReductionAsync(string, CancellationToken)
If the input is a well-formed absolute URI, then assume that it leads to an image, and fetch that image; otherwise just pass the input straight out again.
Declaration
public static Task<string> ResolveUrlOrBase64ImageNoReductionAsync(string input, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| string | input | The input, which may be a base64 encoded image or a URL |
| CancellationToken | cancellationToken |
Returns
| Type | Description |
|---|---|
| Task<string> |
Remarks
Eventually this should do some caching, have a cache-retention policy, flush etc... but in this prototype version we just fetch the data directly
ScaleImage(Image, ImageSize, VerticalAlignment)
Scale an image to comply with the ImageDescription
Declaration
public static Image ScaleImage(Image image, ImageUtilities.ImageSize imageSize, ImageUtilities.VerticalAlignment align)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | image | The image to scale. |
| ImageUtilities.ImageSize | imageSize | Description of the scaling. |
| ImageUtilities.VerticalAlignment | align |
Returns
| Type | Description |
|---|---|
| Image | The scaled image. |
ScaleImage(Image, double, double, Size, Size, Color, VerticalAlignment)
Scale an image
Declaration
public static Image ScaleImage(Image image, double scaleX, double scaleY, Size minSize, Size maxSize, Color fillColor, ImageUtilities.VerticalAlignment align)
Parameters
| Type | Name | Description |
|---|---|---|
| Image | image | The Image to scale. |
| double | scaleX | Scaling vector for the x (width) axis. values below 1 means shrinking. |
| double | scaleY | Scaling vector for the y (height) axis. values below 1 means shrinking. |
| Size | minSize | Minimum size of the picture. |
| Size | maxSize | Maximum size of the picture. |
| Color | fillColor | The color to be used when filling out the minimum area not covered by the image. |
| ImageUtilities.VerticalAlignment | align |
Returns
| Type | Description |
|---|---|
| Image | The scaled image. |
TryFitImage(byte[], int, int, bool)
Declaration
public static byte[] TryFitImage(byte[] imageData, int maxWidth, int maxHeight, bool alwaysTryConvert = false)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | imageData | |
| int | maxWidth | |
| int | maxHeight | |
| bool | alwaysTryConvert |
Returns
| Type | Description |
|---|---|
| byte[] |