private void button2_Click(object sender, EventArgs e)
        {
            Bitmap image = new Bitmap(200, 200);
            Graphics g = Graphics.FromImage(image);
            //使绘图质量最高,即消除锯齿
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            Pen p = new Pen(Color.Blue, 2);//定义了一个蓝色,宽度为的画笔  
            g.DrawString("字符串", new Font("宋体", 10),  new SolidBrush(Color.Blue), new PointF(10, 10)); 
            g.DrawLine(p, 0, 0, 200, 200);//在画板上画直线
            g.DrawRectangle(p, 0, 0, 200, 200);//在画板上画矩形
            g.DrawEllipse(p, 0, 0, 200, 200);//在画板上画椭圆 
            //保存图片
            image.Save(@"c:\1.jpg");
            #region 显示
            //pictureBox1.Image = (Image)image;
            //pictureBox1.Image.Save(@"c:\1.jpg");
            #endregion
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Bitmap image = new Bitmap(400, 800);
            Graphics g = Graphics.FromImage(image);
            //使绘图质量最高,即消除锯齿
            g.SmoothingMode = SmoothingMode.AntiAlias;
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.CompositingQuality = CompositingQuality.HighQuality;
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
            Pen p = new Pen(Color.Black, 5);//定义了黑色,宽度为5的画笔  
            Pen p1 = new Pen(Color.Black, 2);//定义了黑色,宽度为2的画笔 
            SolidBrush sb = new SolidBrush(Color.Black); //定义笔刷
            g.DrawString("测试", new Font("宋体", 18, FontStyle.Bold), sb, new PointF(20, 20));
            g.DrawString("GSM 数字移动电话机", new Font("宋体", 10), sb, new PointF(120, 30));
            g.DrawString("黑色   台", new Font("宋体", 12, FontStyle.Bold), sb, new PointF(300, 25));
            g.DrawRectangle(p, 0, 0, 400, 800);//在画板上画外框矩形
            g.DrawLine(p1, 20, 50, 380, 50);//在画板上画直线  
            g.DrawRectangle(p1, 20, 120, 360, 650);//在画板上画内框矩形

            //粘贴本地条码图片到画板
            Image image1 = Image.FromFile(@"c:\barcode.gif");
            g.DrawImage(image1, 20, 60, image1.Width / 2, image1.Height / 2);

            int heightC = 90; //起始Y坐标
            int heightH = 60; //IMEI行间距
            int imeiCount = 20; //循环IMEI数
            int rows = imeiCount / 2; //行数
            for (int i = 1; i <= rows; i++)
            {
                g.DrawImage(image1, 40, heightC + (heightH * i), image1.Width / 2, image1.Height / 2);
                g.DrawImage(image1, 220, heightC + (heightH * i), image1.Width / 2, image1.Height / 2);
            }
            image1.Dispose();
            g.Dispose();

            //保存图片
            image.Save(@"c:\1.jpg");
            #region 显示
            //pictureBox1.Image = (Image)image;
            //pictureBox1.Image.Save(@"c:\1.jpg");
            #endregion
        }
private void Form1_Paint(object sender, PaintEventArgs e)
{
	Graphics g = e.Graphics; //创建画板,这里的画板是由Form提供的.
	Pen p = new Pen(Color.Blue, 2);//定义了一个蓝色,宽度为的画笔
	g.DrawLine(p, 10, 10, 100, 100);//在画板上画直线,起始坐标为(10,10),终点坐标为(100,100)
	g.DrawRectangle(p, 10, 10, 100, 100);//在画板上画矩形,起始坐标为(10,10),宽为,高为
	g.DrawEllipse(p, 10, 10, 100, 100);//在画板上画椭圆,起始坐标为(10,10),外接矩形的宽为,高为

	/*
		Pen  p = new  Pen(Color.Blue, 5);//设置笔的粗细为,颜色为蓝色
		Graphics  g = this.CreateGraphics();

		//画虚线
		p.DashStyle = DashStyle.Dot;//定义虚线的样式为点
		g.DrawLine(p, 10, 10, 200, 10);

		//自定义虚线
		p.DashPattern = new  float[] { 2, 1 };//设置短划线和空白部分的数组
		g.DrawLine(p, 10, 20, 200, 20);

		//画箭头,只对不封闭曲线有用
		p.DashStyle = DashStyle.Solid;//恢复实线
		p.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头
		g.DrawLine(p, 10, 30, 200, 30);

		g.Dispose();
		p.Dispose();
	 */

	/*
		Graphics g = this.CreateGraphics();
		Rectangle rect = new Rectangle(10, 10, 50, 50);//定义矩形,参数为起点横纵坐标以及其长和宽

		//单色填充
		SolidBrush b1 = new SolidBrush(Color.Blue);//定义单色画刷          
		g.FillRectangle(b1, rect);//填充这个矩形

		//字符串
		g.DrawString("字符串", new Font("宋体", 10), b1, new PointF(90, 10));

		//用图片填充
		TextureBrush b2 = new TextureBrush(Image.FromFile(@"e:\picture\1.jpg"));
		rect.Location = new Point(10, 70);//更改这个矩形的起点坐标
		rect.Width = 200;//更改这个矩形的宽来
		rect.Height = 200;//更改这个矩形的高
		g.FillRectangle(b2, rect);

		//用渐变色填充
		rect.Location = new Point(10, 290);
		LinearGradientBrush b3 = new  LinearGradientBrush(rect, Color.Yellow , Color.Black , LinearGradientMode.Horizontal);
		g.FillRectangle(b3, rect);	
	*/
}


/*
    C#仿QQ截图
    接下来看看这是如何做到的. 
  思路:聊天窗体上有一个截图按钮,点击按钮后,程序将整个屏幕画在一个新的全屏窗体上,
    然后显示这个窗体.因为是全屏的窗体,并且隐藏了菜单栏、工具栏等,
	所以在我们看来就好像是一个桌面的截图,然后在这个新窗体上画矩形,
	最后保存矩形中的内容并显示在原来的聊天窗体中.
  步骤:
  A.新建一个窗体.命名为Catch.然后设置这个窗体的FormBorderStyle为None,WindowState为Maximized.
  B.我们对代码进行编辑:
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Client
{
    public partial class Catch : Form
    {
        public Catch()
        {
            InitializeComponent();
        }

        #region 用户变量
        private Point DownPoint = Point.Empty;//记录鼠标按下坐标,用来确定绘图起点
        private bool CatchFinished = false;//用来表示是否截图完成
        private bool CatchStart = false;//表示截图开始
        private Bitmap originBmp;//用来保存原始图像
        private Rectangle CatchRect;//用来保存截图的矩形
        #endregion

        //窗体初始化操作
        private void Catch_Load(object sender, EventArgs e)
        {
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint, true);
            this.UpdateStyles();
            //以上两句是为了设置控件样式为双缓冲,这可以有效减少图片闪烁的问题,关于这个大家可以自己去搜索下
            originBmp = new Bitmap(this.BackgroundImage);//BackgroundImage为全屏图片,我们另用变量来保存全屏图片
        }

        //鼠标右键点击结束截图
        private void Catch_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
        }

        //鼠标左键按下时动作
        private void Catch_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (!CatchStart)
                {//如果捕捉没有开始
                    CatchStart = true;
                    DownPoint = new Point(e.X, e.Y);//保存鼠标按下坐标
                }
            }
        }

        private void Catch_MouseMove(object sender, MouseEventArgs e)
        {
            if (CatchStart)
            {//如果捕捉开始
                Bitmap destBmp = (Bitmap)originBmp.Clone();//新建一个图片对象,并让它与原始图片相同
                Point newPoint = new Point(DownPoint.X, DownPoint.Y);//获取鼠标的坐标
                Graphics g = Graphics.FromImage(destBmp);//在刚才新建的图片上新建一个画板
                Pen p = new Pen(Color.Blue,1);
                int width = Math.Abs(e.X - DownPoint.X), height = Math.Abs(e.Y - DownPoint.Y);//获取矩形的长和宽
                if (e.X < DownPoint.X)
                {
                    newPoint.X = e.X;
                }
                if (e.Y < DownPoint.Y)
                {
                    newPoint.Y = e.Y;
                }
                CatchRect = new Rectangle(newPoint,new Size(width,height));//保存矩形
                g.DrawRectangle(p,CatchRect);//将矩形画在这个画板上
                g.Dispose();//释放目前的这个画板
                p.Dispose();
                Graphics g1 = this.CreateGraphics();//重新新建一个Graphics类
                //如果之前那个画板不释放,而直接g=this.CreateGraphics()这样的话无法释放掉第一次创建的g,因为只是把地址转到新的g了.如同string一样
                g1 = this.CreateGraphics();//在整个全屏窗体上新建画板
                g1.DrawImage(destBmp,new Point(0,0));//将刚才所画的图片画到这个窗体上
                //这个也可以属于二次缓冲技术,如果直接将矩形画在窗体上,会造成图片抖动并且会有无数个矩形.
                g1.Dispose();
                destBmp.Dispose();//要及时释放,不然内存将会被大量消耗
                
            }
        }

        private void Catch_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                if (CatchStart)
                {
                    CatchStart = false;
                    CatchFinished = true;
                  
                }
            }
        }

        //鼠标双击事件,如果鼠标位于矩形内,则将矩形内的图片保存到剪贴板中
        private void Catch_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left&&CatchFinished)
            {
                if (CatchRect.Contains(new Point(e.X, e.Y)))
                {
                    Bitmap CatchedBmp = new Bitmap(CatchRect.Width, CatchRect.Height);//新建一个于矩形等大的空白图片
                    Graphics g = Graphics.FromImage(CatchedBmp);
                    g.DrawImage(originBmp, new Rectangle(0, 0, CatchRect.Width, CatchRect.Height), CatchRect, GraphicsUnit.Pixel);
                    //把orginBmp中的指定部分按照指定大小画在画板上
                    Clipboard.SetImage(CatchedBmp);//将图片保存到剪贴板
                    g.Dispose();
                    CatchFinished = false;
                    this.BackgroundImage = originBmp;
                    CatchedBmp.Dispose();
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
        }
    }
}

/*
C.创建了Catch窗体后,我们在截图按钮(位于聊天窗体上)上加入以下事件:
*/
private void bCatch_Click(object sender, EventArgs e)
{

	if (bCatch_HideCurrent.Checked)
	{
		this.Hide();//隐藏当前窗体
		Thread.Sleep(50);//让线程睡眠一段时间,窗体消失需要一点时间
		Catch CatchForm = new Catch();
		Bitmap CatchBmp = new Bitmap(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height);//新建一个和屏幕大小相同的图片         
		Graphics g = Graphics.FromImage(CatchBmp);
		g.CopyFromScreen(new Point(0, 0), new Point(0, 0), new Size(Screen.AllScreens[0].Bounds.Width, Screen.AllScreens[0].Bounds.Height));//保存全屏图片
		CatchForm.BackgroundImage = CatchBmp;//将Catch窗体的背景设为全屏时的图片
		if (CatchForm.ShowDialog() == DialogResult.OK)
		{//如果Catch窗体结束,就将剪贴板中的图片放到信息发送框中
			IDataObject iData = Clipboard.GetDataObject();
			DataFormats.Format myFormat = DataFormats.GetFormat(DataFormats.Bitmap);
			if (iData.GetDataPresent(DataFormats.Bitmap))
			{
				richtextbox1.Paste(myFormat);
				Clipboard.Clear();//清除剪贴板中的对象
			}
			this.Show();//重新显示窗体
		}
	}
}


本文转载:CSDN博客