温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用C#内存Graphics对象

发布时间:2021-12-02 11:15:40 来源:亿速云 阅读:139 作者:iii 栏目:编程语言

本篇内容主要讲解“怎么用C#内存Graphics对象”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用C#内存Graphics对象”吧!

SetBackgroundBitmap函数首先将窗体背景图像保存到BackgroundBitmap变量中,然后根据该位图图像轮廓和透明色创建Region,BitmapToRegion就用于完成Bitmap到Region的转换,程序再将这个Region付值给窗体的Region属性以完成不规则窗体的创建。

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方法中完成,而且利用了双重缓冲区技术来进行绘制操作,代码如下:

  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中,然后创建一个C#内存Graphics对象offScreenGraphics和内存位图对象offscreenBitmap,将内存位图对象的引用付值给offScreenGraphics,这样所有对offScreenGraphics的绘制操作也都同时作用于offscreenBitmap,这时就将需要绘制到通知窗体表面的背景图像BackgroundBitmap绘制到C#内存Graphics对象上,DrawText函数根据需要显示文字的大小和范围调用Graphics.DrawString将文字显示在窗体的特定区域。***,调用Graphics.DrawImage将内存中已经绘制完成的图像显示到通知窗体表面。

我们还需要捕获窗体的鼠标操作,有三个操作在这里进行,
1、处理拖动窗体操作
2、处理通知窗体的关闭操作
3、内容区域的单击操作。
三个操作都需要检测鼠标的当前位置与每个Rectangle区域的包含关系,只要单击落在特定区域我们就进行相应的处理,代码如下:

private void TaskbarForm_MouseDown(object sender, MouseEventArgs e)  {  if (e.Button == MouseButtons.Left)  {  if (TitlebarRectangle.Contains(e.Location)) //单击标题栏时拖动  {  ReleaseCapture(); //释放鼠标捕捉  SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);   //发送左键点击的消息至该窗体(标题栏)  }  if (CloseBtnRectangle.Contains(e.Location)) //单击Close按钮关闭  {  this.Hide();  currentTop = 1;  }  if (ContentRectangle.Contains(e.Location )) //单击内容区域  {  System.Diagnostics.Process.Start("http://www.Rithia.com");  }  }  }

该程序可以很好的进行通知窗体的显示、停留和隐藏操作,并且具备简单的换肤机制,在利用了双重缓冲区绘图技术后,可以保证窗体的绘制平滑且没有闪烁。

到此,相信大家对“怎么用C#内存Graphics对象”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI