窗口刷新的时候,会产生Paint事件,那么我们给这个事件添加一个处理函数。然后在这个函数里画图。就能保证所画的图不被刷新掉,

它可以总是显示。Paint事件对应的委托是:public delegate void PaintEventHandler(object sender, PaintEventArgs e);

示例1:在窗口中间画一根线。(创建WindowsForms应用程序)

            Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Black);
//在屏幕中间画一根线
g.DrawLine(p, , this.Height / , this.Width, this.Height / );
//画一个矩形
g.DrawRectangle(p, , , , );
//画一个圆
g.DrawEllipse(p, , , , );
//手动释放资源
g.Dispose();
p.Dispose();

示例2:一个填充矩形

        private void formPaint(Object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
//蓝色画刷
SolidBrush brush = new SolidBrush(Color.FromArgb(, , ));
//一个矩形
Rectangle rect = new Rectangle(, , , );
//填充一个矩形
graphics.FillRectangle(brush, rect);
}

示例3:画一张png图片(用PNG是因为可以显示透明的图片,GIF图片也有这个作用)

        private void formPaint(Object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
//加载图片
Image img = Image.FromFile(Application.StartupPath+ @"\111.png");
//图片显示起始位置
Point strPoint=new Point(,);
//不限制大小绘制
graphics.DrawImage(img, strPoint);
//缩小图片绘制,限制在一个矩形内
Rectangle rect=new Rectangle(,,,);
graphics.DrawImage(img, rect);
}

示例4:用DrawString显示文字

DrawString在Grahpics类里有好几个重载,有的可以让字符串在一个矩形内显示,有的可以使用特定的显示格式。这里就不做详细介绍了。

只讲比较常用的。

看例子吧,处理键盘输入字符事件,在窗口显示输入的字符。如下:

    public partial class Form2 : Form
{
public static String strText = "";
public Form2()
{
InitializeComponent();
this.Paint += formPaint;
}
private void formPaint(Object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
//创建画刷
SolidBrush brush=new SolidBrush(Color.FromArgb(,,));
//创建字体
Font font=new Font("宋体",20f);
//显示字符串,在一个矩形内
graphics.DrawString(strText, font, brush, this.ClientRectangle); }
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
strText += e.Button.ToString();
//刷新整个窗口
this.Invalidate();
}
}

效果图如下:

示例5:重写OnPaintBackground绘制背景的方法

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//重写OnPaintBackground方法
protected override void OnPaintBackground(PaintEventArgs e)
{
//禁止基类处理,我们自己来绘制背景
//base.OnPaintBackground(e);
//透明背景画刷
SolidBrush brush=new SolidBrush(Color.Transparent);
//填充整个窗口
e.Graphics.FillRectangle(brush,this.ClientRectangle);
//再画一个圆圈
Pen pen = new Pen(Color.FromArgb(, , ),);
e.Graphics.DrawEllipse(pen, this.ClientRectangle);
}
}

示例6:TextureBursh图片画刷

可以用图片来填充一个形状,如矩形,圆形。图片不够大则平铺显示。

private void formPaint(Object sender, PaintEventArgs e)
{
Graphics graphics = e.Graphics;
//创建图片画刷
Rectangle rect = new Rectangle(, , , );
TextureBrush brush = new TextureBrush(Image.FromFile(Application.StartupPath+ @"\111.png"),rect);
       graphics.FillEllipse(brush, , , , );
     }

构造函数最后一个参数,rect表示要用图片的哪部分进行填充,10,10表示图片起始位置(左上角),70,70表示宽度和高度,注意不能超出图片原有范围。整张图片填充的话,则不需要指定rect。构造函数里填一个参数就行了。

C# GDI+编程的更多相关文章

  1. GDI+编程说明及小结

    原文地址:http://blog.csdn.net/byxdaz/article/details/5972759 GDI+(Graphics Device Interface Plus图形设备接口加) ...

  2. GDI编程

    图形设备接口(GDI)是一个可执行程序,它接受Windows应用程序的绘图请求(表现为GDI函数调用),并将它们传给相应的设备驱动程序,完成特定于硬件的输出,象打印机输出和屏幕输出.GDI负责Wind ...

  3. GDI编程小结

    图形设备接口(GDI)是一个可运行程序,它接受Windows应用程序的画图请求(表现为GDI函数调用),并将它们传给对应的设备驱动程序,完毕特定于硬件的输出,象打印机输出和屏幕输出.GDI负责Wind ...

  4. MFC控件GDI编程

    MFC控件GDI编程 一丶学习内容 1.了解常用的GDI函数绘图. 2.使用常用的画笔画刷. 二丶常用的GDI函数绘图 上方则为我们常用的GDI函数了. 画线 矩形. 以及圆 等等. 2.1 画线代码 ...

  5. C# GDI+编程之剖析startAngle和sweepAngle

    以DrawArc为例,它有一种形式如下的构造函数 public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepA ...

  6. GDI+编程小结

    GDI+(Graphics Device Interface Plus图形设备接口加)是Windows XP和Windows Server 2003操作系统的子系统,也是.NET框架的重要组成部分,负 ...

  7. 一篇文章让你彻底弄懂WinForm GDI 编程基本原理

    一 GDI编程原理 GDI(Graphics Device Interface,图形设备接口),主要负责Windows系统与绘图程序之间的信息交换,处理所有Windows程序的图形输出. GDI的常用 ...

  8. GDI 编程基础简介

    今天准备重新对GDI的知识进行回顾一下,以便加深认识. 一.GDI 在进行Windows编程时,可能经常会用到设备描述表的类型句柄,例如,最厂家的HDC,它就是图像设备描述类型句柄.因为GDI的绘图函 ...

  9. GDI编程基础

    窗口和视口 视口是基于设备的采用的是设备坐标(单位:像素),窗口是基于程序的采用的是逻辑坐标(单位:像素/毫米/厘米等). 在默认的映射模式下,视口是与窗口等同的.但是如果改变其映射模式,则其对应的单 ...

  10. C#GDI+编程基础(一:Graphics画布类)

    GDI+存在的意义:将变成与具体硬件实现细节分开. GDI+步骤:获取画布,绘制图像.处理图像 命名空间: using System.Drawing;//提供对GDI+基本图形功能的访问 using ...

随机推荐

  1. C#如何使用结构化异常处理

    Knowledge Base: Chinese (Simplified) 如何使用 Visual C# .NET 和 Visual C# 2005 中的结构化异常处理文章ID: 816157 最近更新 ...

  2. java中byte数组与int,long,short间的转换

    http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * <l ...

  3. css系列(6)css的运用(二)

        本节继续介绍css在html页面重的应用实例.     (1)div的border-style属性: none: 无样式 hidden: 除了同表格的边框发生冲突的时候,其它同none dot ...

  4. 【Tech】Mac上安装MAMP打开本地网页

    不知道为什么实验室老是用些奇葩的东西,这次是madserve,主要是用来统计移动端广告点击率的,基于PHP/MYSQL实现. 昨天很快在Windows上搭好一个xampp,并用它建立了一个virtua ...

  5. 去duplicate的方法

    1.什么是duplicate,为什么要去除. 什么是duplicate:这是在建库的过程后,对已连有接头的DNA片段进行扩增,然后去接flowcell.之所以在建库后扩增,这是由于接flowcell的 ...

  6. ucsc genome brower的用法和说明(一)

    官网说明书:http://genome.ucsc.edu/goldenpath/help/hgTracksHelp.html 1.genome brower的作用 a,展示任何尺度的基因组片段.比如, ...

  7. ScreenOS地址转换

    目录 1. NAT-src 1.1 来自DIP池(启用PAT)的NAT-src 1.2 来自DIP池(禁用PAT)的NAT-src 1.3 来自DIP池(带有地址变换)的NAT-src 1.4 来自出 ...

  8. 轻量级的Java 开发框架 Spring

    Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development a ...

  9. 【bzoj1232】[Usaco2008Nov]安慰奶牛cheer(最小生成树)

    题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1232 这道题要保留的道路肯定是原图的一棵生成树,因为要保留n-1条边,且使删边后的图连 ...

  10. LeetCode——same-tree

    Question Given two binary trees, write a function to check if they are equal or not. Two binary tree ...