怎么用C#內(nèi)存Graphics對(duì)象

本篇內(nèi)容主要講解“怎么用C#內(nèi)存Graphics對(duì)象”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“怎么用C#內(nèi)存Graphics對(duì)象”吧!

堅(jiān)守“ 做人真誠(chéng) · 做事靠譜 · 口碑至上 · 高效敬業(yè) ”的價(jià)值觀,專業(yè)網(wǎng)站建設(shè)服務(wù)10余年為成都混凝土攪拌罐小微創(chuàng)業(yè)公司專業(yè)提供成都企業(yè)網(wǎng)站定制營(yíng)銷網(wǎng)站建設(shè)商城網(wǎng)站建設(shè)手機(jī)網(wǎng)站建設(shè)小程序網(wǎng)站建設(shè)網(wǎng)站改版,從內(nèi)容策劃、視覺設(shè)計(jì)、底層架構(gòu)、網(wǎng)頁(yè)布局、功能開發(fā)迭代于一體的高端網(wǎng)站建設(shè)服務(wù)。

SetBackgroundBitmap函數(shù)首先將窗體背景圖像保存到BackgroundBitmap變量中,然后根據(jù)該位圖圖像輪廓和透明色創(chuàng)建Region,BitmapToRegion就用于完成Bitmap到Region的轉(zhuǎn)換,程序再將這個(gè)Region付值給窗體的Region屬性以完成不規(guī)則窗體的創(chuàng)建。

public void SetBackgroundBitmap(Image image, Color transparencyColor)  {  BackgroundBitmap = new Bitmap(image);  Width = BackgroundBitmap.Width;  Height = BackgroundBitmap.Height;  Region = BitmapToRegion(BackgroundBitmap, transparencyColor);  }   public Region BitmapToRegion(Bitmap bitmap, Color transparencyColor)  {  if (bitmap == null)  throw new ArgumentNullException("Bitmap", "Bitmap cannot be null!");   int height = bitmap.Height;  int width = bitmap.Width;  GraphicsPath path = new GraphicsPath();  for (int j = 0; j < height; j++)  for (int i = 0; i < width; i++)  {  if (bitmap.GetPixel(i, j) == transparencyColor)  continue;  int x0 = i;  while ((i < width) && (bitmap.GetPixel(i, j) != transparencyColor))  i++;  path.AddRectangle(new Rectangle(x0, j, i - x0, 1));  }  Region region = new Region(path);  path.Dispose();  return region;  }

通知窗體背景以及文字的繪制在重載的OnPaintBackground方法中完成,而且利用了雙重緩沖區(qū)技術(shù)來(lái)進(jìn)行繪制操作,代碼如下:

  1. protected override void OnPaintBackground(PaintEventArgs e)  

  2. {  

  3. Graphics grfx = e.Graphics;  

  4. grfx.PageUnit = GraphicsUnit.Pixel;  

  5. Graphics offScreenGraphics;  

  6. Bitmap offscreenBitmap;  

  7. offscreenBitmap = new Bitmap(BackgroundBitmap.Width, BackgroundBitmap.Height);  

  8. offScreenGraphics = Graphics.FromImage(offscreenBitmap);  

  9. if (BackgroundBitmap != null)  

  10. {  

  11. offScreenGraphics.DrawImage(BackgroundBitmap, 0, 0, 
    BackgroundBitmap.Width, BackgroundBitmap.Height);  

  12. }  

  13. DrawText(offScreenGraphics);  

  14. grfx.DrawImage(offscreenBitmap, 0, 0);  

上述代碼首先返回窗體繪制表面的Graphics并保存在變量grfx中,然后創(chuàng)建一個(gè)C#內(nèi)存Graphics對(duì)象offScreenGraphics和內(nèi)存位圖對(duì)象offscreenBitmap,將內(nèi)存位圖對(duì)象的引用付值給offScreenGraphics,這樣所有對(duì)offScreenGraphics的繪制操作也都同時(shí)作用于offscreenBitmap,這時(shí)就將需要繪制到通知窗體表面的背景圖像BackgroundBitmap繪制到C#內(nèi)存Graphics對(duì)象上,DrawText函數(shù)根據(jù)需要顯示文字的大小和范圍調(diào)用Graphics.DrawString將文字顯示在窗體的特定區(qū)域。***,調(diào)用Graphics.DrawImage將內(nèi)存中已經(jīng)繪制完成的圖像顯示到通知窗體表面。

我們還需要捕獲窗體的鼠標(biāo)操作,有三個(gè)操作在這里進(jìn)行,
1、處理拖動(dòng)窗體操作
2、處理通知窗體的關(guān)閉操作
3、內(nèi)容區(qū)域的單擊操作。
三個(gè)操作都需要檢測(cè)鼠標(biāo)的當(dāng)前位置與每個(gè)Rectangle區(qū)域的包含關(guān)系,只要單擊落在特定區(qū)域我們就進(jìn)行相應(yīng)的處理,代碼如下:

private void TaskbarForm_MouseDown(object sender, MouseEventArgs e)  {  if (e.Button == MouseButtons.Left)  {  if (TitlebarRectangle.Contains(e.Location)) //單擊標(biāo)題欄時(shí)拖動(dòng)  {  ReleaseCapture(); //釋放鼠標(biāo)捕捉  SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);   //發(fā)送左鍵點(diǎn)擊的消息至該窗體(標(biāo)題欄)  }  if (CloseBtnRectangle.Contains(e.Location)) //單擊Close按鈕關(guān)閉  {  this.Hide();  currentTop = 1;  }  if (ContentRectangle.Contains(e.Location )) //單擊內(nèi)容區(qū)域  {  System.Diagnostics.Process.Start("http://www.Rithia.com");  }  }  }

該程序可以很好的進(jìn)行通知窗體的顯示、停留和隱藏操作,并且具備簡(jiǎn)單的換膚機(jī)制,在利用了雙重緩沖區(qū)繪圖技術(shù)后,可以保證窗體的繪制平滑且沒(méi)有閃爍。

到此,相信大家對(duì)“怎么用C#內(nèi)存Graphics對(duì)象”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

網(wǎng)頁(yè)標(biāo)題:怎么用C#內(nèi)存Graphics對(duì)象
本文路徑:http://muchs.cn/article48/pidiep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、搜索引擎優(yōu)化、關(guān)鍵詞優(yōu)化、網(wǎng)站改版網(wǎng)頁(yè)設(shè)計(jì)公司、品牌網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司