The purpose of this repository is to provide a way for people to create placeholder images quickly.
https://www.craigoates.net/Software/project/11
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
102 lines
3.6 KiB
102 lines
3.6 KiB
namespace SmoulderingBeachBall |
|
|
|
module ImageMaker = |
|
|
|
open System |
|
open System.IO |
|
open System.Drawing |
|
|
|
type OverlayType = |
|
| Border |
|
| Full |
|
|
|
type OverlaySpec = |
|
{ colour: Brush; |
|
overlayType: OverlayType } |
|
|
|
type ImageSpec = |
|
{ width: int; |
|
height: int; |
|
colour: Brush; |
|
filePath: string; |
|
overlay: OverlaySpec option } |
|
|
|
let private validateDimension dimension = |
|
match dimension with |
|
| dimension when dimension <= 0 -> invalidArg "dimension" "The width and height must be greater than 0." |
|
| _ -> () |
|
|
|
let private validateDirectory filePath = |
|
let path = Path.GetDirectoryName filePath |
|
match (Directory.Exists path) with |
|
| false -> invalidArg "filePath" "Unable to save to the specified location because it does not exist." |
|
| true -> () |
|
|
|
// This function is to be deleted. |
|
let makeImageBase width height colour filepath = |
|
async { |
|
try |
|
validateDimension width |
|
validateDimension height |
|
validateDirectory filepath |
|
use bitmap = new Bitmap(width, height) |
|
use graphics = Graphics.FromImage(bitmap) |
|
graphics.FillRectangle(colour, new Rectangle(0, 0, bitmap.Width, bitmap.Height)) |
|
bitmap.Save(filepath) |
|
return "Image saved." |
|
with |
|
| :? ArgumentException as ex -> return ex.Message |
|
| _ as ex -> return ex.Message |
|
} |
|
|
|
let private createBorderPath penWidth spec = |
|
let penOffset = int (penWidth / (float32 2)) |
|
[|Point (0, penOffset); // Essentially (0, 0) |
|
Point ((spec.width - penOffset), penOffset); |
|
Point ((spec.width - penOffset), (spec.height - penOffset)); |
|
Point (penOffset, (spec.height - penOffset)); |
|
Point (penOffset, 0)|] |
|
|
|
let private drawImageWithBorder (graphics: Graphics) (pen: Pen) spec = |
|
printfn "[INFO.] Adding border to image..." |
|
let penPath = createBorderPath pen.Width spec |
|
graphics.DrawLines (pen, penPath) |
|
|
|
|
|
let private drawImageWithFullOverlay graphics pen spec = |
|
printfn "[INFO.] Adding full overlay to image..." |
|
() |
|
|
|
let private addOverlayToImage graphics spec = |
|
let overlay = spec.overlay.Value |
|
let pen = new Pen (overlay.colour, Width = 10.0f) |
|
match overlay.overlayType with |
|
| Border -> drawImageWithBorder graphics pen spec |
|
| Full -> drawImageWithFullOverlay graphics pen spec |
|
|
|
let private drawImage spec = |
|
let bitmap = new Bitmap (spec.width, spec.height) |
|
let graphics = Graphics.FromImage (bitmap) |
|
let rectangle = Rectangle (0, 0, spec.width, spec.height) |
|
graphics.FillRectangle (spec.colour, rectangle) |
|
match spec.overlay.IsSome with |
|
| true -> addOverlayToImage graphics spec |
|
| false -> printfn "[INFO.] No overlay specified. Creating image without one." |
|
bitmap.Save (spec.filePath) |
|
bitmap.Dispose() |
|
graphics.Dispose() |
|
|
|
let makeImage spec = |
|
async { |
|
try |
|
printfn "[INFO.] Attempting to make image..." |
|
validateDimension spec.width |
|
validateDimension spec.height |
|
validateDirectory spec.filePath |
|
drawImage spec |
|
printfn "[SUCCESS] Image creation attempt complete." |
|
return () |
|
with |
|
| :? ArgumentException as ex -> printfn "%s" ex.Message |
|
| _ as ex -> printfn "%s" ex.Message |
|
} |