在我的窗体上绘制一个位图 [英] Draw a Bitmap on my Form

查看:75
本文介绍了在我的窗体上绘制一个位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想在屏幕上绘制一个位图.我的想法是要这样

Hi,

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

'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))



我需要此Bitmap方法,因为稍后我想根据一系列具有不同颜色的AVI电影创建一个Bitmap.

谢谢



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

Thanks

推荐答案

又是什么问题?

无论如何,由于不处理Bitmap s bitbit_2Graphics对象,并且根据IntPtr msdn.microsoft.com/en-us/library/2k57eh1k%28VS.80%29.aspx>文档 [
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.


感谢您的快速解答.

是的,我在代码中犯了一些错误.

该代码只是一个片段,用以演示我想做的事情.

我知道我可以使用FillRectangle,但我想使用位图以另一种方式使用此位图.

有了您对API的提示(再次感谢),我现在创建类似这样的东西.

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.

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)



对我来说,这看起来很丑.但是,也许就是这样.

我需要一个彩色的位图来创建以后的AVI文件.这就是原因,我不能使用fillrectangle方法.



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.


这篇关于在我的窗体上绘制一个位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆