What was the question again?
Regardless, you have some resource leaks from not disposing of the
Bitmap
s
bit
and
bit_2
or the
Graphics
object and the
IntPtr
is not being released as per the
docs[
^] either. If all that you want to do is draw a red rectangle then look at the
Graphics.FillRectangle
and
Graphics.DrawRectangle
methods. You can specify a
Brush
, size and location and do the drawing without any
Bitmap
s at all. If you're drawing this on your
Form
then it needs to be done from the
OnPaint
event handler to ensure that the rectangle is redrawn every time that the
Form
is repainted. The event args to the handler also give you the
Graphics
object so that you don't need to use the overhead to create and destroy a new one.
If you need the
Bitmap
s because you want to reference them later that's fine, just make sure to release all of the resource that you allocate.