Class ImageUtilities
Namespace: SuperOffice.Util
Assembly: SoCore.dll
Syntax
public static class ImageUtilities : Object
Fields
ProductImageSize
Default max size for product images (largest edge)
Declaration
public const int ProductImageSize = 2000
Field Value
Type | Description |
---|---|
Int32 |
ThumbnailSize
Default thumbnail size
Declaration
public const int ThumbnailSize = 150
Field Value
Type | Description |
---|---|
Int32 |
Methods
FitTo(Image, Int32, Int32)
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, Int32, Int32), 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 |
Int32 | width | Maximum width |
Int32 | height | Maximum height |
Returns
Type | Description |
---|---|
Image | Possibly rescaled image |
GetAspect(Int32, Int32)
Get the aspect of an image.
Declaration
public static double GetAspect(int width, int height)
Parameters
Type | Name | Description |
---|---|---|
Int32 | width | |
Int32 | 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)
Declaration
public static string GetImageTypeFromBase64String(string base64String, string defaultType)
Parameters
Type | Name | Description |
---|---|---|
String | base64String | |
String | defaultType |
Returns
Type | Description |
---|---|
String |
GetIntersectionRectangle(Size, Size, ImageUtilities.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, ImageUtilities.ImageSize, ImageUtilities.FallbackStrategy, Func<Image>, Boolean, 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. |
Boolean | 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, Boolean, 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 | |
Boolean | 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, Boolean)
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 | |
Boolean | border |
Returns
Type | Description |
---|---|
Image | An image containing a text center both horizontally and vertically. |
GetThumbnail(Image, Int32, Int32)
Get a thumbnail of the incoming image; the quiality of the scaling is not quite as good as for FitTo(Image, Int32, Int32), 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 |
Int32 | width | Maximum width, default ThumbnailSize |
Int32 | height | Maximum height, default ThumbnailSize |
Returns
Type | Description |
---|---|
Image | Thumbnail image |
GetThumbnail(String, Int32, Int32)
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 |
Int32 | width | Maximum width, default ThumbnailSize |
Int32 | height | Maximum height, default ThumbnailSize |
Returns
Type | Description |
---|---|
String |
GetThumbnailImage(Stream, Int32, Int32)
Declaration
public static Image GetThumbnailImage(this Stream input, int width = 150, int height = 150)
Parameters
Type | Name | Description |
---|---|---|
Stream | input | |
Int32 | width | |
Int32 | height |
Returns
Type | Description |
---|---|
Image |
GetThumbnailImage(String, Int32, Int32)
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 |
Int32 | width | Maximum width, default ThumbnailSize |
Int32 | 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 |
ResolveUrlOrBase64Image(String, Int32)
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 string ResolveUrlOrBase64Image(string input, int maxPixelSize = 2000)
Parameters
Type | Name | Description |
---|---|---|
String | input | The input, which may be a base64 encoded image or a URL |
Int32 | 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. |
Returns
Type | Description |
---|---|
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
ResolveUrlOrBase64ImageNoReduction(String)
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 string ResolveUrlOrBase64ImageNoReduction(string input)
Parameters
Type | Name | Description |
---|---|---|
String | input | The input, which may be a base64 encoded image or a URL |
Returns
Type | Description |
---|---|
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, ImageUtilities.ImageSize, ImageUtilities.VerticalAlignment)
Scale an image to comply with the
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, ImageUtilities.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[], Int32, Int32, Boolean)
Declaration
public static byte[] TryFitImage(byte[] imageData, int maxWidth, int maxHeight, bool alwaysTryConvert = false)
Parameters
Type | Name | Description |
---|---|---|
Byte[] | imageData | |
Int32 | maxWidth | |
Int32 | maxHeight | |
Boolean | alwaysTryConvert |
Returns
Type | Description |
---|---|
Byte[] |