pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

將現(xiàn)有圖形轉(zhuǎn)換為位圖

Existing Graphics into Bitmap(將現(xiàn)有圖形轉(zhuǎn)換為位圖)
本文介紹了將現(xiàn)有圖形轉(zhuǎn)換為位圖的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習吧!

問題描述

我正在為交易軟件(C#、w??informs、.NET 3.5)編寫一個插件,我想在包含數(shù)據(jù)的面板(比如 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:

  1. 我在面板中添加了一個 CursorControl
    • CursorControl 位于主繪圖面板上方,以便覆蓋整個區(qū)域
    • 它是 Enabled = false 以便所有輸入事件都傳遞給父級圖表面板
    • 實現(xiàn)了它的Paint方法,以便它在當前鼠標位置從上到下和從左到右繪制線條
  1. I added a CursorControl to the panel
    • this CursorControl is positioned over the main drawing panel so that it covers it's entire area
    • it's Enabled = false so that all input events are passed to the parent ChartPanel
    • it's Paint method is implemented so that it draws lines from top to bottom and from left to right at current mouse position
  • A) 調(diào)用 ChartPanel.Invalidate(),但正如我所說,底層數(shù)據(jù)的繪制可能很昂貴,這會導(dǎo)致每次移動鼠標時都重新繪制所有內(nèi)容,這是錯誤的(但是這是我現(xiàn)在可以完成這項工作的唯一方法)
  • B) 調(diào)用 CursorControl.Invalidate() 并且在繪制光標之前我會拍攝當前繪制數(shù)據(jù)的快照并將其作為光標的背景,每次光標都會恢復(fù)需要重新粉刷……問題是……我不知道該怎么做.
  • A) Call 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)
  • B) Call 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.意思是:

  • 將現(xiàn)有的 Graphics 對象轉(zhuǎn)換為 Bitmap(它(圖形)是通過 Paint 方法提供給我的,我必須在它上面作畫,所以我只是無法創(chuàng)建新的 Graphics 對象......也許我理解錯了,但我是這么理解的)
  • 在繪制十字準線之前,從位圖中恢復(fù)圖形內(nèi)容并重新繪制十字準線
  • Turn existing 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)
  • before the crosshair is painted, restore the Graphics contents from the Bitmap and repaint the crosshair

我無法控制繪制昂貴數(shù)據(jù)的過程.我只能訪問我的 CursorControl 以及通過 API 調(diào)用的方法.

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內(nèi)容存入Bitmap中,以后再恢復(fù)呢?或者有什么更好的方法可以解決這個問題?

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?

已解決:經(jīng)過數(shù)小時的反復(fù)試驗,我想出了一個可行的解決方案.我用的軟件有很多問題不能一概而論,但主要的原則是明確的:

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:

  • 已經(jīng)繪制的現(xiàn)有圖形不能直接轉(zhuǎn)換為位圖,而是我必須使用@Gusman 的回答中首先提到的 panel.DrawToBitmap 方法.我知道,我想避免它,但最終我不得不接受,因為它似乎是唯一的方法
  • 我還想避免每一幀都重復(fù)繪制,所以第一個十字線繪制總是直接繪制到ChartPanel.在鼠標移動而不更改圖表圖像后,我通過 DrawToBitmap 進行快照并按照所選答案中的描述進行操作.
  • 控件必須是不透明的(未啟用透明背景),以便刷新它不會在其父控件上調(diào)用 Paint(這會導(dǎo)致整個圖表重新繪制)
  • existing Graphics with already painted stuff can't be converted to Bitmap directly, instead I had to use 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 way
  • also I wanted to avoid double drawing of every frame, so the first crosshair paint is always drawn directly to the ChartPanel. After the mouse moves without changing the chart image I take a snapshow through DrawToBitmap and proceed as described in chosen answer.
  • The control has to be Opaque (not enabled Transparent background) so that refreshing it doesn't call Paint on it's parent controls (which would cause the whole chart to repaint)

我仍然每隔幾秒左右就會偶爾出現(xiàn)閃爍,但我想我可以以某種方式解決這個問題.雖然我選擇了 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.

首先,創(chuàng)建一個屬性,該屬性將保存對圖表的引用并與其繪制事件掛鉤,如下所示:

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;
    }

}

現(xiàn)在定義 CloneAspect 函數(shù),該函數(shù)將在圖表面板中完成繪制操作時將控件的外觀呈現(xiàn)為位圖:

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 重寫方法中執(zhí)行以下操作:

Then in the OnPaint overriden method do this:

public override void OnPaint(PaintEventArgs e)
{
    e.Graphics.DrawImage(aspect);

    //Now draw the cursor
    (...)
}

最后,無論您在何處創(chuàng)建圖表和自定義光標:

And finally wherever you create the chart and the customcursor you do:

CursorControl.Panel = ChartPanel;

瞧,您可以根據(jù)需要重新繪制多次,而無需重新計算圖表的內(nèi)容.

And voila, you can redraw as many times you need without recalculating the chart's content.

干杯.

這篇關(guān)于將現(xiàn)有圖形轉(zhuǎn)換為位圖的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應(yīng)用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應(yīng)用程序 - 如何制作交互式菜單?)
How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加圖標?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創(chuàng)建重復(fù)的表單?)
Building a database driven menu with ASP.NET, JQuery and Suckerfish(使用 ASP.NET、JQuery 和 Suckerfish 構(gòu)建數(shù)據(jù)庫驅(qū)動的菜單)
主站蜘蛛池模板: 中视电广_短视频拍摄_短视频推广_短视频代运营_宣传片拍摄_影视广告制作_中视电广 | 生物颗粒燃烧机-生物质燃烧机-热风炉-生物颗粒蒸汽发生器-丽水市久凯能源设备有限公司 | 衢州装饰公司|装潢公司|办公楼装修|排屋装修|别墅装修-衢州佳盛装饰 | 纸箱抗压机,拉力机,脂肪测定仪,定氮仪-山东德瑞克仪器有限公司 | 杭州用友|用友软件|用友财务软件|用友ERP系统--杭州协友软件官网 | 脱硫搅拌器厂家-淄博友胜不锈钢搅拌器厂家 | 微波萃取合成仪-电热消解器价格-北京安合美诚科学仪器有限公司 | 铸钢件厂家-铸钢齿轮-减速机厂家-淄博凯振机械有限公司 | 武汉高温老化房,恒温恒湿试验箱,冷热冲击试验箱-武汉安德信检测设备有限公司 | 我车网|我关心的汽车资讯_汽车图片_汽车生活! | wika威卡压力表-wika压力变送器-德国wika代理-威卡总代-北京博朗宁科技 | 应急灯_消防应急灯_应急照明灯_应急灯厂家-大成智慧官网 | 石磨面粉机|石磨面粉机械|石磨面粉机组|石磨面粉成套设备-河南成立粮油机械有限公司 | 一体化隔油提升设备-餐饮油水分离器-餐厨垃圾处理设备-隔油池-盐城金球环保产业发展有限公司 | PC构件-PC预制构件-构件设计-建筑预制构件-PC构件厂-锦萧新材料科技(浙江)股份有限公司 | 温州中研白癜风专科_温州治疗白癜风_温州治疗白癜风医院哪家好_温州哪里治疗白癜风 | 电动高尔夫球车|电动观光车|电动巡逻车|电动越野车厂家-绿友机械集团股份有限公司 | 模具钢_高速钢_不锈钢-万利钢金属材料 | 制冷采购电子商务平台——制冷大市场 | 滚筒烘干机_转筒烘干机_滚筒干燥机_转筒干燥机_回转烘干机_回转干燥机-设备生产厂家 | 丹佛斯变频器-丹佛斯压力开关-变送器-广州市风华机电设备有限公司 | 真空搅拌机-行星搅拌机-双行星动力混合机-广州市番禺区源创化工设备厂 | 语料库-提供经典范文,文案句子,常用文书,您的写作得力助手 | 铝板冲孔网,不锈钢冲孔网,圆孔冲孔网板,鳄鱼嘴-鱼眼防滑板,盾构走道板-江拓数控冲孔网厂-河北江拓丝网有限公司 | 斗式提升机_链式斗提机_带式斗提机厂家无锡市鸿诚输送机械有限公司 | 上海物流公司,上海货运公司,上海物流专线-优骐物流公司 | 档案密集柜_手动密集柜_智能密集柜_内蒙古档案密集柜-盛隆柜业内蒙古密集柜直销中心 | 肉嫩度仪-凝胶测试仪-国产质构仪-气味分析仪-上海保圣实业发展有限公司|总部 | 合肥活动房_安徽活动板房_集成打包箱房厂家-安徽玉强钢结构集成房屋有限公司 | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | 长沙广告公司|长沙广告制作设计|长沙led灯箱招牌制作找望城湖南锦蓝广告装饰工程有限公司 | 食品机械专用传感器-落料放大器-低价接近开关-菲德自控技术(天津)有限公司 | 浇注料-高铝砖耐火砖-郑州凯瑞得窑炉耐火材料有限公司 | 德国UST优斯特氢气检漏仪-德国舒赐乙烷检测仪-北京泽钏 | 重庆私家花园设计-别墅花园-庭院-景观设计-重庆彩木园林建设有限公司 | 磁粉制动器|张力控制器|气胀轴|伺服纠偏控制器整套厂家--台灵机电官网 | 潍坊大集网-潍坊信息港-潍坊信息网| 附着力促进剂-尼龙处理剂-PP处理剂-金属附着力处理剂-东莞市炅盛塑胶科技有限公司 | 钢绞线万能材料试验机-全自动恒应力两用机-混凝土恒应力压力试验机-北京科达京威科技发展有限公司 | 欧美日韩国产一区二区三区不_久久久久国产精品无码不卡_亚洲欧洲美洲无码精品AV_精品一区美女视频_日韩黄色性爱一级视频_日本五十路人妻斩_国产99视频免费精品是看4_亚洲中文字幕无码一二三四区_国产小萍萍挤奶喷奶水_亚洲另类精品无码在线一区 | YT保温材料_YT无机保温砂浆_外墙保温材料_南阳银通节能建材高新技术开发有限公司 |