From 980d9d7ea78ec3d93cced0713ef88428bf27fe80 Mon Sep 17 00:00:00 2001 From: Craig Oates Date: Tue, 11 Sep 2018 02:33:19 +0100 Subject: [PATCH] make public function in library asynchronous. Adds initial workings of a function to save image to stream in library. --- DeathSocket/Domain.fs | 9 +++++++++ DeathSocket/GridPainter.fs | 12 +++++++----- DeathSocket/ImageServices.fs | 16 +++++++++++++++- DeathSocket/ScratchPad.fsx | 2 +- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/DeathSocket/Domain.fs b/DeathSocket/Domain.fs index e181d0c..04be768 100644 --- a/DeathSocket/Domain.fs +++ b/DeathSocket/Domain.fs @@ -4,6 +4,7 @@ module Domain = open System.Drawing + open System.Drawing.Imaging type ImageSpec = { originalPath: string; @@ -11,4 +12,12 @@ colour: Brush; penWidth: float32 rows: int; + columns: int } + + type StreamSpec = + { imagePath: string; + format: ImageFormat; + colour: Brush; + penWidth: float32 + rows: int; columns: int } \ No newline at end of file diff --git a/DeathSocket/GridPainter.fs b/DeathSocket/GridPainter.fs index 643ba5b..f24c2ef 100644 --- a/DeathSocket/GridPainter.fs +++ b/DeathSocket/GridPainter.fs @@ -8,9 +8,11 @@ namespace DeathSocket open ImageServices let applyGrid spec = - try - validateFilePath |> ignore - drawGrid spec |> ignore - with - | :? FileNotFoundException as ex -> printfn"%s" ex.Message + async { + try + validateFilePath |> ignore + drawGrid spec |> ignore + with + | :? FileNotFoundException as ex -> printfn"%s" ex.Message + } \ No newline at end of file diff --git a/DeathSocket/ImageServices.fs b/DeathSocket/ImageServices.fs index 05815bf..9b621fc 100644 --- a/DeathSocket/ImageServices.fs +++ b/DeathSocket/ImageServices.fs @@ -3,6 +3,7 @@ open System.Drawing open System.Drawing.Imaging open DeathSocket + open System.IO let createHorizontalLines width height rows = let interval = width / rows @@ -29,4 +30,17 @@ img.Dispose () graphics.Dispose () pen.Dispose () - () \ No newline at end of file + + let drawGridToStream (imgStream: Stream) spec = + let img = Bitmap.FromFile (spec.imagePath) + let graphics = Graphics.FromImage img + let pen = new Pen (spec.colour, width = spec.penWidth) + let horizontalLines = + createHorizontalLines (img.Size.Width) (img.Size.Height) (spec.columns) + let verticalLines = createVerticalLines (img.Size.Height) (spec.columns) + for line in horizontalLines do graphics.DrawLines (pen, line) + for line in verticalLines do graphics.DrawLines (pen, line) + graphics.Dispose () + pen.Dispose () + img.Save (imgStream, spec.format) + img.Dispose () \ No newline at end of file diff --git a/DeathSocket/ScratchPad.fsx b/DeathSocket/ScratchPad.fsx index ed5ad1a..fa5b9cd 100644 --- a/DeathSocket/ScratchPad.fsx +++ b/DeathSocket/ScratchPad.fsx @@ -59,4 +59,4 @@ let spec = penWidth = float32 (10) rows = 10 columns = 10 } -GridPainter.applyGrid spec \ No newline at end of file +GridPainter.applyGrid spec |> Async.RunSynchronously