I can not understand how correctly in the MouseDoubleClick event to take a Bitmap drawn image and MouseDoubleClick get pixelated and change color. Please tell me a link to the GitHub project . The difficulty lies in the fact that I cannot change the color of each pixel (that is, when I double-click nothing happens), and when I try to work directly with the Bitmap image, an exception appears (I add a line to the beginning of the event: bmap = new Bitmap(panel.Image); ): System.NullReferenceException: 'Object reference not set to an instance of an object.'
Thank you in advance))
using System; using System.Collections.Generic; using System.Drawing; using System.Windows.Forms; namespace NNHopfild { public partial class NNHT : Form { Point CurrentPoint; Graphics g; Bitmap bmap; public NNHT() { InitializeComponent(); bmap = new Bitmap(panel.Width, panel.Height); panel.MouseDown += panel_MouseDown; panel.Paint += panel_Paint; g = panel.CreateGraphics(); } private void exit_Click(object sender, EventArgs e) { Application.Exit(); } private void panel_MouseMove(object sender, MouseEventArgs e) { if (e.Button == MouseButtons.Left) { using (Graphics g = Graphics.FromImage(bmap)) { g.FillEllipse(Brushes.Black, eX, eY, 10, 10); } panel.Invalidate(); } } private void panel_MouseDown(object sender, MouseEventArgs e) { CurrentPoint = e.Location; } private void panel_Paint(object sender, PaintEventArgs e) { e.Graphics.DrawImage(bmap, Point.Empty); } private void panel_MouseDoubleClick(object sender, MouseEventArgs e) { int height = panel.Height; int width = panel.Width; int[,] mat = new int[height, width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { Color col; col = bmap.GetPixel(i, j); if (col == Color.White) { mat[i, j] = -1; bmap.SetPixel(i, j, Color.Blue); } if (col == Color.Black) { mat[i, j] = 1; bmap.SetPixel(i, j, Color.Red); } } } } } } 
Bitmap.SetPixel(,,)way, I don’t think that you are critical, but the regularBitmap.SetPixel(,,)method is slow, look in the direction ofBitmap.LockBits()andFastBitmap. - Alias