我正在为交易软件(C#、winforms、.NET 3.5)编写一个插件,我想在包含数据的面板(比如 ChartPanel
)上绘制一个十字光标画画可能很贵.到目前为止,我所做的是:
I'm writing a plugin for a trading software (C#, winforms, .NET 3.5) and I'd like to draw a crosshair cursor over a panel (let's say ChartPanel
) which contains data that might be expensive to paint. What I've done so far is:
CursorControl
CursorControl
位于主绘图面板上方,以便覆盖整个区域Enabled = false
以便所有输入事件都传递给父级图表面板
Paint
方法,以便它在当前鼠标位置从上到下和从左到右绘制线条CursorControl
to the panel
CursorControl
is positioned over the main drawing panel so that it covers it's entire areaEnabled = false
so that all input events are passed to the parent
ChartPanel
Paint
method is implemented so that it draws lines from top to bottom and from left to right at current mouse positionChartPanel.Invalidate()
,但正如我所说,底层数据的绘制可能很昂贵,这会导致每次移动鼠标时都重新绘制所有内容,这是错误的(但是这是我现在可以完成这项工作的唯一方法)CursorControl.Invalidate()
并且在绘制光标之前我会拍摄当前绘制数据的快照并将其作为光标的背景,每次光标都会恢复需要重新粉刷……问题是……我不知道该怎么做.ChartPanel.Invalidate()
, but as I said, the underlying data may be expensive to paint and this would cause everything to redraw everytime I move a mouse, which is wrong (but it is the only way I can make this work now)CursorControl.Invalidate()
and before the cursor is drawn I would take a snapshot of currently drawn data and keep it as a background for the cursor that would be just restored everytime the cursor needs to be repainted ... the problem with this is ... I don't know how to do that.2.B.意思是:
Graphics
对象转换为 Bitmap
(它(图形)是通过 Paint 方法提供给我的,我必须在它上面作画,所以我只是无法创建新的 Graphics 对象......也许我理解错了,但我是这么理解的)Graphics
object into Bitmap
(it (the Graphics) is given to me through Paint method and I have to paint at it, so I just can't create a new Graphics object ... maybe I get it wrong, but that's the way I understand it)我无法控制绘制昂贵数据的过程.我只能访问我的 CursorControl
以及通过 API 调用的方法.
I can't control the process of painting the expensive data. I can just access my CursorControl
and it's methods that are called through the API.
那么有没有什么办法可以把已有的Graphics内容存入Bitmap中,以后再恢复呢?或者有什么更好的方法可以解决这个问题?
So is there any way to store existing Graphics contents into Bitmap and restore it later? Or is there any better way to solve this problem?
已解决:经过数小时的反复试验,我想出了一个可行的解决方案.我用的软件有很多问题不能一概而论,但主要的原则是明确的:
RESOLVED: So after many hours of trial and error I came up with a working solution. There are many issues with the software I use that can't be discussed generally, but the main principles are clear:
panel.DrawToBitmap
方法.我知道,我想避免它,但最终我不得不接受,因为它似乎是唯一的方法ChartPanel
.在鼠标移动而不更改图表图像后,我通过 DrawToBitmap
进行快照并按照所选答案中的描述进行操作.panel.DrawToBitmap
method first mentioned in @Gusman's answer. I knew about it, I wanted to avoid it, but in the end I had to accept, because it seems to be the only wayChartPanel
. After the mouse moves without changing the chart image I take a snapshow through DrawToBitmap
and proceed as described in chosen answer.我仍然每隔几秒左右就会偶尔出现闪烁,但我想我可以以某种方式解决这个问题.虽然我选择了 Gusman 的答案,但我要感谢所有参与的人,因为我使用了其他答案中提到的许多其他技巧,例如 Panel.BackgroundImage、使用 Plot() 方法而不是 Paint() 来锁定图像等.
I still experience occasional flicker every few seconds or so, but I guess I can figure that out somehow. Although I picked Gusman's answer, I would like to thank everyone involved, as I used many other tricks mentioned in other answers, like the Panel.BackgroundImage, use of Plot() method instead of Paint() to lock the image, etc.
为什么不将 ChartPanel 中的所有图形克隆到 CursorControl 上?
Why don't you clone all the graphics in the ChartPanel over your CursorControl?
此处的所有代码都必须放在 CursorControl 中.
All the code here must be placed inside your CursorControl.
首先,创建一个属性,该属性将保存对图表的引用并与其绘制事件挂钩,如下所示:
First, create a property which will hold a reference to the chart and hook to it's paint event, something like this:
ChartPanel panel;
public ChartPanel Panel
{
get{ return panel; }
set{
if(panel != null)
panel.Paint -= CloneAspect;
panel = value;
panel.Paint += CloneAspect;
}
}
现在定义 CloneAspect 函数,该函数将在图表面板中完成绘制操作时将控件的外观呈现为位图:
Now define the CloneAspect function which will render the control's appearance to a bitmap whenever a Paint opperation has been done in the Chart panel:
Bitmap aspect;
void CloneAspect(object sender, PaintEventArgs e)
{
if(aspect == null || aspect.Width != panel.Width || aspect.Height != panel.Height)
{
if(aspect != null)
aspect.Dispose();
aspect = new Bitmap(panel.Width, panel.Height, System.Drawing.Imaging.PixelFormat.Format32bppPArgb);
}
panel.DrawToBitmap(aspect, new Rectangle(0,0, panel.Width, panel.Height);
}
然后在 OnPaint 重写方法中执行以下操作:
Then in the OnPaint overriden method do this:
public override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(aspect);
//Now draw the cursor
(...)
}
最后,无论您在何处创建图表和自定义光标:
And finally wherever you create the chart and the customcursor you do:
CursorControl.Panel = ChartPanel;
瞧,您可以根据需要重新绘制多次,而无需重新计算图表的内容.
And voila, you can redraw as many times you need without recalculating the chart's content.
干杯.
这篇关于将现有图形转换为位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!