From b413819584ee8bca2713da58d6b8f6e0c8ef32be Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Fri, 31 Aug 2018 23:51:17 +0100 Subject: [PATCH] add code to create basic image using record types. --- SmoulderingBeachBall/ImageMaker.fs | 42 ++++++++++++++++++++--------- SmoulderingBeachBall/ScratchPad.fsx | 21 ++++++++++++++- 2 files changed, 50 insertions(+), 13 deletions(-) diff --git a/SmoulderingBeachBall/ImageMaker.fs b/SmoulderingBeachBall/ImageMaker.fs index e7948ed..7393cfa 100644 --- a/SmoulderingBeachBall/ImageMaker.fs +++ b/SmoulderingBeachBall/ImageMaker.fs @@ -8,20 +8,18 @@ module ImageMaker = type OverlayType = | Border - | Complete + | Full - type OverlaySpec = { - colour: Brush; - overlayType: OverlayType - } + type OverlaySpec = + { colour: Brush; + overlayType: OverlayType } - type ImageSpec = { - width: int; - height: int; - colour: Brush; - filePath: string; - overlay: OverlaySpec - } + type ImageSpec = + { width: int; + height: int; + colour: Brush; + filePath: string; + overlay: OverlaySpec } let private validateDimension dimension = match dimension with @@ -49,3 +47,23 @@ module ImageMaker = | :? 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 + } diff --git a/SmoulderingBeachBall/ScratchPad.fsx b/SmoulderingBeachBall/ScratchPad.fsx index caef410..ae86926 100644 --- a/SmoulderingBeachBall/ScratchPad.fsx +++ b/SmoulderingBeachBall/ScratchPad.fsx @@ -3,6 +3,7 @@ open System.Drawing open System.Drawing.Imaging open SmoulderingBeachBall +open SmoulderingBeachBall.ImageMaker // INITIAL IDEA ======================================================================================================= @@ -25,4 +26,22 @@ let im_colour = Brushes.BurlyWood let im_testPath = "C:/users/craig/desktop/test.png" ImageMaker.makeImage im_width im_height im_colour im_testPath - |> Async.RunSynchronously \ No newline at end of file + |> Async.RunSynchronously + + +let borderOverlay = + { colour = Brushes.BlueViolet; + overlayType = Border } + +let fullOverlay = + { colour = Brushes.Fuchsia; + overlayType = Full } + +let imageSpec = + { width = 500; + height = 500; + colour = Brushes.Yellow; + filePath = im_testPath; + overlay = borderOverlay } // Change this to quickly change between a border overlay and a full overlay. + +makeImage2 imageSpec |> Async.RunSynchronously