Browse Source

add code to create basic image using record types.

master
Craig Oates 6 years ago
parent
commit
b413819584
  1. 42
      SmoulderingBeachBall/ImageMaker.fs
  2. 21
      SmoulderingBeachBall/ScratchPad.fsx

42
SmoulderingBeachBall/ImageMaker.fs

@ -8,20 +8,18 @@ module ImageMaker =
type OverlayType = type OverlayType =
| Border | Border
| Complete | Full
type OverlaySpec = { type OverlaySpec =
colour: Brush; { colour: Brush;
overlayType: OverlayType overlayType: OverlayType }
}
type ImageSpec = { type ImageSpec =
width: int; { width: int;
height: int; height: int;
colour: Brush; colour: Brush;
filePath: string; filePath: string;
overlay: OverlaySpec overlay: OverlaySpec }
}
let private validateDimension dimension = let private validateDimension dimension =
match dimension with match dimension with
@ -49,3 +47,23 @@ module ImageMaker =
| :? ArgumentException as ex -> return ex.Message | :? ArgumentException as ex -> return ex.Message
| _ 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
}

21
SmoulderingBeachBall/ScratchPad.fsx

@ -3,6 +3,7 @@
open System.Drawing open System.Drawing
open System.Drawing.Imaging open System.Drawing.Imaging
open SmoulderingBeachBall open SmoulderingBeachBall
open SmoulderingBeachBall.ImageMaker
// INITIAL IDEA ======================================================================================================= // INITIAL IDEA =======================================================================================================
@ -25,4 +26,22 @@ let im_colour = Brushes.BurlyWood
let im_testPath = "C:/users/craig/desktop/test.png" let im_testPath = "C:/users/craig/desktop/test.png"
ImageMaker.makeImage im_width im_height im_colour im_testPath ImageMaker.makeImage im_width im_height im_colour im_testPath
|> Async.RunSynchronously |> 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

Loading…
Cancel
Save