Click here to Skip to main content
16,006,341 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I would like to draw an Bitmap on my screen. My idea was to go like this

VB
'Bitmap of size 100 x 100
      Dim bit As New Bitmap(100, 100)
      'Fill the Bitmap with red color
      Dim pointer As IntPtr = bit.GetHbitmap(Color.Red)
      'Create a new bitmap with this red color
      Dim bit_2 As Bitmap = Bitmap.FromHbitmap(pointer)
      'Drawing it
      g = Me.CreateGraphics
      g.DrawImage(bit_2, New PointF(0, 0))


I need this Bitmap method because I would like to create later from a series of Bitmap with different colors a AVI movie.

Thanks
Posted

What was the question again?

Regardless, you have some resource leaks from not disposing of the Bitmaps 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 Bitmaps 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 Bitmaps because you want to reference them later that's fine, just make sure to release all of the resource that you allocate.
 
Share this answer
 
Thanks for your quick answer.

Yes, I did some error in the code.

The code is only a snippset to demonstrate, what I would like to do.

I know that I can use FillRectangle, but I would like to work with bitmap to use this bitmaps in another way.

With your hint to the API (thanks, again) I create now something like this.

VB
Dim instance As Bitmap = New Bitmap(100, 100)
Dim background As Color = Color.Beige
Dim returnValue As IntPtr
returnValue = instance.GetHbitmap(background)
PictureBox2.Image = Image.FromHbitmap(returnValue)


For me this look very ugly. But maybe that is the way.

I need a coloured bitmap to create later a AVI-file. That is the reason, that I can't use the fillrectangle method.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900