Death Socket consists of three projects. They are a .Net Standard 2.0 library, a console program and a Test Centre. The purpose of this repository is to provide a way for people to add grids to images.
https://www.craigoates.net/Software/project/13
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
4.7 KiB
106 lines
4.7 KiB
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 listig 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 |
|
use imageFill = new SKPaint () |
|
imageFill.Style <- SKPaintStyle.Fill |
|
|
|
// Add Bitmap |
|
//let fileStream = File.Open (@"C:\Users\craig\Desktop\test.jpg", FileMode.Open) |
|
|
|
let byteArray = File.ReadAllBytes(@"C:\Users\craig\Desktop\test.jpg") |
|
use memStream = new MemoryStream(byteArray) |
|
|
|
// this works... |
|
use skStream = new SKManagedStream (memStream) |
|
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." |