Browse Source

add input validation to makeImage.

Add async and try-catch blocks to makeImage.
Refactor the scratch pad. script
master
Craig Oates 6 years ago
parent
commit
351b7c76a9
  1. 34
      SmoulderingBeachBall/SmoulderingBeachBall/ImageMaker.fs
  2. 17
      SmoulderingBeachBall/SmoulderingBeachBall/ScratchPad.fsx
  3. 12
      SmoulderingBeachBall/SmoulderingBeachBall/SmoulderingBeachBall.fsproj

34
SmoulderingBeachBall/SmoulderingBeachBall/ImageMaker.fs

@ -2,19 +2,33 @@ namespace SmoulderingBeachBall
module ImageMaker = module ImageMaker =
open System
open System.Drawing open System.Drawing
//open System.Drawing.Imaging open System.IO
let addOverlay colour image = let private validateDimension dimension =
null match dimension with
| dimension when dimension <= 0 -> invalidArg "dimension" "The width and height must be greater than 0."
| _ -> ()
let saveImage filePath = let private validateDirectory filePath =
null 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: int) (height: int) (colour: Brush) = let makeImage width height colour filepath =
async { async {
use bitmap = new Bitmap(width, height) try
use graphics = Graphics.FromImage(bitmap) validateDimension width
graphics.FillRectangle(Brushes.Coral, new Rectangle(0, 0, bitmap.Width, bitmap.Height)) validateDimension height
return "we" 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
} }

17
SmoulderingBeachBall/SmoulderingBeachBall/ScratchPad.fsx

@ -2,14 +2,27 @@
open System.Drawing open System.Drawing
open System.Drawing.Imaging open System.Drawing.Imaging
open SmoulderingBeachBall
// INITIAL IDEA =======================================================================================================
let width = 500 let width = 500
let height = 500 let height = 500
let colour = Brushes.BurlyWood let colour = Brushes.BurlyWood
let testPath = "C:/users/craig/desktop/test.png"
let draw () = let draw () =
use bmp = new Bitmap(width, height) use bmp = new Bitmap(width, height)
use gr = Graphics.FromImage(bmp) use gr = Graphics.FromImage(bmp)
gr.FillRectangle(colour, new Rectangle(0, 0, bmp.Width, bmp.Height)) gr.FillRectangle(colour, new Rectangle(0, 0, bmp.Width, bmp.Height))
gr.DrawImage(bmp, 0, 0) gr.DrawImage(bmp, 0, 0)
bmp.Save("C:/users/craig/desktop/test.png", ImageFormat.Png) bmp.Save(testPath, ImageFormat.Png)
let result = draw ()
// IMAGE MAKER ========================================================================================================
let im_width = 500
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
|> Async.RunSynchronously

12
SmoulderingBeachBall/SmoulderingBeachBall/SmoulderingBeachBall.fsproj

@ -6,18 +6,6 @@
<ItemGroup> <ItemGroup>
<Compile Include="ImageMaker.fs" /> <Compile Include="ImageMaker.fs" />
<COMReference Include="{215d64d2-031c-33c7-96e3-61794cd1ee61}">
<Guid>215d64d2-031c-33c7-96e3-61794cd1ee61</Guid>
<VersionMajor>2</VersionMajor>
<VersionMinor>4</VersionMinor>
<WrapperTool>tlbimp</WrapperTool>
</COMReference>
<COMReference Include="{d37e2a3e-8545-3a39-9f4f-31827c9124ab}">
<Guid>d37e2a3e-8545-3a39-9f4f-31827c9124ab</Guid>
<VersionMajor>2</VersionMajor>
<VersionMinor>4</VersionMinor>
<WrapperTool>tlbimp</WrapperTool>
</COMReference>
<None Include="ScratchPad.fsx" /> <None Include="ScratchPad.fsx" />
</ItemGroup> </ItemGroup>

Loading…
Cancel
Save