如何使用图形绘制图像 [英] How to use graphics to draw image

查看:87
本文介绍了如何使用图形绘制图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用图形类在位图上绘制图像?然后可以将其还原到屏幕上,以使我在调用invalidate().时所绘制的内容都不会被擦除.请为我提供解决方案.

How can I use graphics class to draw images over a bitmap? Which then can be restored to the screen so whatever i draw does not get erased when I call invalidate().please provide me the solution.

推荐答案

为我提供 解决方案"似乎意味着必须有一个且只有一个.但问题是奇怪,因为它混淆了两个不同的方面.

在位图上绘图是发生在内存中的事,并且与invalidate()的动作无关,其目的是强制窗口管理器要求您的程序重绘窗口内容.

如果您不希望窗口内容消失",则必须重写OnPaint,重新绘制窗口.
您可以通过在其中复制图像来做到这一点(创建CPaintDC,用它初始化gdp::Graphics,广告将其用于绘制您已经存在的位图并绘制到其中).

要绘制位图,首先必须在某处创建一个gdp :: Bitmap,在其上初始化一个gdp :: Graphics,然后用它来绘制.

但是...这两个截然不同的步骤是否必要?您是否可以仅在需要时将其绘制到窗口中?
"Provide me the solution" seems implying there must be one and only. But the question is strange, since it mixed up two distinct aspects.

Drawing on a bitmap is something that happens in memory, and has no relation with the action of invalidate(), whose purpose is to force the window manager to ask your program to redraw the window contents.

If you don''t want the window contents to "vanish", you have to override OnPaint, repainting the window.
You can do that by copying an image in it (Create a CPaintDC, initialise a gdp::Graphics with it, ad use it to draw your already existent and drawn bitmap into it).

To draw your bitmap, you have first to create a gdp::Bitmap somewhere, initiaklize a gdp::Graphics on it, ad use it to draw.

But ... are these two distinct step necessary? Can''t you just draw the into the window when required?


窗口没有与其表面关联的永久性画布/位图.系统可以随时覆盖屏幕上的窗口的保护,并且可以随时使用WM_PAINT调用您,以要求您在屏幕上重绘窗口图像的被破坏"部分.
该图包括2个步骤:
The window does not have a persistent canvas/bitmap associated with its surface. The system can overwrite a protion of your window on the screen any time, and it can call you any time with a WM_PAINT to ask you to redraw the ''destroyed'' part of the window image on the screen.
The drawing consists of 2 steps:

  1. 系统将WM_ERASEBKGND发送到您的窗口/控件.
  2. WM_PAINT,您必须在其中绘制控件


如果要保留窗口/控件的图像,则必须将控件的实际外观绘制到内存中的位图,而在WM_PAINT消息中,只需将位图绘制到控件即可.这称为双缓冲.如果控件不是太复杂,那么为此就不值得在内存中保留位图.通常仅在控件的绘制很复杂且对性能至关重要的情况下,或者可以避免很多烦人的闪烁时才使用双缓冲.另两种避免闪烁的方法:


If you want to keep the image of your window/control then you have to draw the actual appearance of the control to an in-memory bitmap, and in the WM_PAINT message you just draw the bitmap to the control. This is called double buffering. If the control is not too complex then it does not worth keeping an in memory bitmap for this purpose. Doublebuffering is usually used only if the drawing of the control is complex and performance critical, or if you can avoid a lot of annoying flickering. Another two flicker avoidance methods:


  1. 从WM_ERASEBKGND返回1而不在绘制前清除贝克特.
  2. 将图形完全从WM_PAINT移动到WM_ERASEBKGND,在WM_PAINT中不执行任何操作,而是验证剪贴区域(例如,仅调用BeginPaint()和EndPaint()而不绘制实际图形).


调用invalidate()时,将发送WM_PAINT消息.
在您的 OnPaint [
When you call invalidate(), a WM_PAINT message is sent.
In your OnPaint[^]function, you can then do whatever is needed to redraw the picture (using the graphics class if you wish).


这篇关于如何使用图形绘制图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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