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.
69 lines
2.2 KiB
69 lines
2.2 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 } |
|
|
|
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 -> () |
|
|
|
let makeImage 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 drawMainImage spec = |
|
use bitmap = new Bitmap(spec.width, spec.height) |
|
use graphics = Graphics.FromImage(bitmap) |
|
graphics.FillRectangle(spec.colour, new Rectangle(0, 0, bitmap.Width, bitmap.Height)) |
|
bitmap.Save(spec.filePath) |
|
|
|
let makeImage2 spec = |
|
async { |
|
try |
|
validateDimension spec.width |
|
validateDimension spec.height |
|
validateDirectory spec.filePath |
|
// add option code for overlay here |
|
drawMainImage spec |
|
return "Image saved." |
|
with |
|
| :? ArgumentException as ex -> return ex.Message |
|
| _ as ex -> return ex.Message |
|
}
|
|
|