From 970b13899c5bdb429cab926a77f65d87133d07af Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Sat, 1 Sep 2018 16:05:43 +0100 Subject: [PATCH] remove MakeImage2 code. Reduce the amount of possible duplicate code in logic branches. And, update Scratchpad to reflect the changes. Draw border logic will, also, be added but it will be incomplete and produce incorrect results. --- SmoulderingBeachBall/ImageMaker.fs | 84 +++++++++++++---------------- SmoulderingBeachBall/ScratchPad.fsx | 6 +-- 2 files changed, 41 insertions(+), 49 deletions(-) diff --git a/SmoulderingBeachBall/ImageMaker.fs b/SmoulderingBeachBall/ImageMaker.fs index 4d6cb96..f100f02 100644 --- a/SmoulderingBeachBall/ImageMaker.fs +++ b/SmoulderingBeachBall/ImageMaker.fs @@ -33,7 +33,7 @@ module ImageMaker = | true -> () // This function is to be deleted. - let makeImage width height colour filepath = + let makeImageBase width height colour filepath = async { try validateDimension width @@ -49,61 +49,53 @@ module ImageMaker = | _ as ex -> return ex.Message } - let private 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) - printfn "[SUCCESS] Image saved." + let private createBorderPath spec = + [|Point (10, 10); + Point (spec.width, 10); + Point (spec.width, spec.height); + Point (spec.height, 10); + Point (10, 10)|] - (* - Need to call dispose manually because of how the image is/can be built-up. - need to reduce the amount of branching code... it's getting unwieldy. - create bitmap - create graphics - draw base - check overlay - add border - add full overlay - write image to disk - dispose resources - write output message*) + let private drawImageWithBorder (graphics: Graphics) (pen: Pen) spec = + printfn "[INFO.] Adding border to image..." + let penPath = createBorderPath spec + graphics.DrawLines (pen, penPath) - let private createBorderPositions spec = - [|Point (0, 0); - Point (spec.width, 0); - Point (spec.width, spec.height); - Point (spec.height, 0); - Point (0, 0)|] - let private drawImageWithBorder spec = - let overlay = spec.overlay.Value - use bitmap = new Bitmap (spec.width, spec.height) - use graphics = Graphics.FromImage (bitmap) - use pen = new Pen (overlay.colour) - let border = createBorderPositions spec - graphics.DrawLines (pen, border) - printfn "Overlay image function not finished." + let private drawImageWithFullOverlay graphics pen spec = + printfn "[INFO.] Adding full overlay to image..." + () - let private drawImageWithFullOverlay spec = () + 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 drawImageWithOverlay spec = - match spec.overlay.Value.overlayType with - | Border -> drawImageWithBorder spec - | Full -> drawImageWithFullOverlay 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 makeImage2 spec = + let makeImage spec = async { try printfn "[INFO.] Attempting to make image..." validateDimension spec.width validateDimension spec.height validateDirectory spec.filePath - match Option.isSome spec.overlay with - | true -> drawImageWithOverlay spec - | false -> drawMainImage spec - return "[SUCCESS] Image creation attempt complete." + drawImage spec + printfn "[SUCCESS] Image creation attempt complete." + return () with - | :? ArgumentException as ex -> return ex.Message - | _ as ex -> return ex.Message - } + | :? ArgumentException as ex -> printfn "%s" ex.Message + | _ as ex -> printfn "%s" ex.Message + } \ No newline at end of file diff --git a/SmoulderingBeachBall/ScratchPad.fsx b/SmoulderingBeachBall/ScratchPad.fsx index 8cd3d28..1da7979 100644 --- a/SmoulderingBeachBall/ScratchPad.fsx +++ b/SmoulderingBeachBall/ScratchPad.fsx @@ -25,7 +25,7 @@ let im_height = 500 let im_colour = Brushes.BurlyWood let im_testPath = "C:/users/craig/desktop/test.png" -ImageMaker.makeImage im_width im_height im_colour im_testPath +ImageMaker.makeImageBase im_width im_height im_colour im_testPath |> Async.RunSynchronously @@ -42,6 +42,6 @@ let imageSpec = height = 500; colour = Brushes.Yellow; filePath = "C:/users/craig/desktop/test.png"; - overlay = None } // Change this to quickly change between border/full overlay or None. + overlay = Some borderOverlay } // Change this to quickly change between border/full overlay or None. -makeImage2 imageSpec |> Async.RunSynchronously +ImageMaker.makeImage imageSpec |> Async.RunSynchronously \ No newline at end of file