0

I want to update a bitmap on the form depending on the keys pressed by the user. I am still new to WinForms, do I need to constantly exit that form and make a new one with the updated bitmap?

open System.Drawing
open System.Windows.Forms

let bitmap = new Bitmap(100, 100)

for x in 0 .. 99 do
    for y in 0 .. 99 do
        bitmap.SetPixel(x, y, Color.Red)

let form = new Form()
let pictureBox = new PictureBox()

pictureBox.Image <- bitmap
form.Controls.Add(pictureBox)

Application.Run(form)

The key listener part will look something like this

match Console.ReadKey().Key with
| ConsoleKey.UpArrow -> // do something
| _ -> ()
  • Definitely you don't need to close a Form to update a Bitmap. But, update how? What are you planning to change? It's not really clear what you are trying to do. – Jimi Oct 16 '18 at 16:19
  • Say it's an empty bitmap of 100 by 100 and there is a black pixel in the top left. If the right key is pushed I want it to move to the right 1 pixel. –  Oct 16 '18 at 16:38
  • I would build a Class of Points with properties that store the `(X,Y)` coordinates of a `Point` and a bool property that identifies the `Point` as Selected. Have a `List`. In the `Paint()` event of a `PictureBox` (or `Panel`), iterate the `List`. When you find the `Point.Selected`, set the `Pixel.Color(Point.X, Point.Y)` Stop the search. On key Left-Right increase/descrese `List[Index]` by 1. On key Up/Down, increase/descrese `List[Index]` by `Row-Count`, watching for the edge-cases. – Jimi Oct 16 '18 at 17:23
  • so... you're creating a `canvas`? – Technivorous Oct 16 '18 at 17:23

0 Answers0