module internal ImageServices
open System.IO
open System.Drawing
open System.Drawing.Imaging
open DeathSocket
open ColourServices
open SkiaSharp
let createHorizontalLines width height rows =
let interval = height / rows
[| for point in 1 .. (rows - 1 ) ->
[|Point (0, (interval * point))
Point (width, (interval * point) )|]|]
let createVerticalLines width height columns =
let interval = width / columns
[| for point in 1 .. (columns - 1 ) ->
[| Point ((interval * point), 0 )
Point ((interval * point), height)|]|]
(* Note on Use of Temp File in Functions which Add A Grid Overlay
===========================================================================
The temp. file is used in the functions below are there to convert images
with indexed pixels. Instead of listing them all, just assume any function
with a "*Spec" type as a parameter will use this "temp" file. *)
let drawBrushSpecGrid (spec: BrushSpec) =
use original = Bitmap.FromFile spec.originalPath
use temp = new Bitmap(original)
use clone = temp.Clone(new Rectangle(0, 0 , temp.Width, temp.Height), PixelFormat.Format32bppArgb)
use graphics = Graphics.FromImage(clone)
use pen = new Pen (spec.colour, width = spec.penWidth)
graphics.DrawImage(original,new Rectangle(0, 0 , clone.Width, clone.Height))
let horizontalLines =
createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows)
let verticalLines =
createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns)
for line in horizontalLines do graphics.DrawLines (pen, line)
for line in verticalLines do graphics.DrawLines (pen, line)
clone.Save (spec.savePath)
let drawRGBAGrid (spec: RGBASpec) =
use original = Bitmap.FromFile spec.originalPath
use temp = new Bitmap (original)
use clone = temp.Clone (new Rectangle(0, 0 , temp.Width, temp.Height), PixelFormat.Format32bppArgb)
use graphics = Graphics.FromImage(clone)
use pen = new Pen ((makeBrushFromRGBASpec spec), width = spec.penWidth)
graphics.DrawImage (original,new Rectangle(0, 0 , clone.Width, clone.Height))
let horizontalLines =
createHorizontalLines (clone.Size.Width) (clone.Size.Height) (spec.rows)
let verticalLines =
createVerticalLines (clone.Size.Width) (clone.Size.Height) (spec.columns)
for line in horizontalLines do graphics.DrawLines (pen, line)
for line in verticalLines do graphics.DrawLines (pen, line)
clone.Save (spec.savePath)
// You are up to here. Need to draw lines and add image.
let drawSkiaGrid () =
// Set Canvas/Surface
let imageInfo = new SKImageInfo (2592, 1456 )
use surface = SKSurface.Create(imageInfo)
let canvas = surface.Canvas
canvas.Clear (SKColors.White)
// Set SKPaint Objects
use stroke = new SKPaint ()
stroke.Style <- SKPaintStyle.Stroke
stroke.StrokeWidth <- float32 3
//stroke.Color <- SKColors.BlueViolet
stroke.Color <- makeSkiaColour 123 123 123
// Note about generating colours in Skia Sharp.
//stroke.Color <- new SKColor (byte 1000 , byte 5111 , byte 4225 , byte 255 )
use imageFill = new SKPaint ()
imageFill.Style <- SKPaintStyle.Fill
// Add Bitmap
let fileStream = File.Open (@"C:\Users\craig\Desktop\test.jpg", FileMode.Open)
use skStream = new SKManagedStream (fileStream)
use bitmap = SKBitmap.Decode (skStream)
use shader = SKShader.CreateBitmap (bitmap, SKShaderTileMode.Mirror, SKShaderTileMode.Mirror)
imageFill.Shader <- shader
canvas.DrawPaint (imageFill)
// Draw Lines
let horizontalLines = createHorizontalLines 2592 1456 6
let verticalLines = createVerticalLines 2592 1456 9
for hLine in horizontalLines do
let x1 = float32 hLine.[0].X
let y1 = float32 hLine.[0].Y
let x2 = float32 hLine.[1].X
let y2 = float32 hLine.[1].Y
canvas.DrawLine (x1, y1, x2, y2, stroke)
for vLine in verticalLines do
let x1 = float32 vLine.[0].X
let y1 = float32 vLine.[0].Y
let x2 = float32 vLine.[1].X
let y2 = float32 vLine.[1].Y
canvas.DrawLine (x1, y1, x2, y2, stroke)
use snapshot = surface.Snapshot ()
use data = snapshot.Encode (SKEncodedImageFormat.Png, 100 )
use saveStream = File.OpenWrite (@"C:\Users\craig\Desktop\test-complete.jpg")
data.SaveTo (saveStream)
printfn "Skia Sharp image saved."