Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the pixel color in a VB.net form?

How would I change the colour of individual pixels in a VB.NET form?

Thanks.

like image 984
SiliconCelery Avatar asked Dec 06 '25 03:12

SiliconCelery


1 Answers

A hard requirement for Winforms is that you should be able to redraw the form whenever Windows asks it to. Which will happen when you minimize and restore the window. Or on older versions of Windows when you move another window across yours.

So just setting pixels on the window isn't good enough, you are going to lose them all when the window redraws. Instead use a bitmap. An additional burden is that you are going to have to keep the user interface responsive so you need to do your calculations on a worker thread. The BackgroundWorker is handy to get that right.

One way to do this is to use two bitmaps, one you fill in the worker and another that you display. Every, say, one row of pixels make a copy of the in-work bitmap and pass that to ReportProgress(). Your ProgressChanged event then disposes the old bitmap and stores the new passed one and calls Invalidate to force a repaint.

like image 161
Hans Passant Avatar answered Dec 08 '25 19:12

Hans Passant