C#—图形图像编程基础

/*
 * 绘制要求的线;
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Pen p = new Pen(Color.CadetBlue, 10.5f);
            g.DrawString("蓝色,宽度为10.5", this.Font, new SolidBrush(Color.CadetBlue), new PointF(5.0f, 10.0f));
            g.DrawLine(p, new Point(110, 10), new PointF(380, 10));

            p.Width = 2;
            p.Color = Color.Red;
            g.DrawString("红色,宽度为2", this.Font, new SolidBrush(Color.Black), 5,25);
            g.DrawLine(p, new Point(110, 30), new PointF(380, 30));

            p.StartCap = LineCap.Flat;
            p.EndCap = LineCap.ArrowAnchor;
            p.Width = 9;
            g.DrawString("红色箭头线", this.Font, new SolidBrush(Color.Black), 5, 45);
            g.DrawLine(p, new Point(110, 50), new PointF(380, 50));

            p.DashStyle = DashStyle.Custom;
            p.DashPattern = new float[] { 4, 4 };
            p.Width = 2;
            p.EndCap = LineCap.NoAnchor;
            g.DrawString("自定义虚线", this.Font, new SolidBrush(Color.Black), 5, 65);
            g.DrawLine(p, new Point(110, 70), new PointF(380, 70));

            p.DashStyle = DashStyle.Dot;
            g.DrawString("点划线", this.Font, new SolidBrush(Color.Black), 5, 85);
            g.DrawLine(p, new Point(110, 90), new Point(380, 90));
        }
    }
}

运行结果:


/*
 * 纹理画刷TextureBrush类应用实例:将一副名为b.png的图像添加到项目中,
 * 并设置其“复制到输出目录”属性为“如果较新则复制”;
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            Bitmap bitmap = new Bitmap(Application.StartupPath + @"\b.png");   //创建Bitmap对象
            bitmap = new Bitmap(bitmap, this.ClientRectangle.Size);     //将其缩放到当前窗体大小
            TextureBrush brush = new TextureBrush(bitmap);
            g.FillEllipse(brush, this.ClientRectangle);
            g.Dispose();
        }
    }
}

运行结果:


/*
 * 图案画刷HatchBrush类应用。该画刷使用90%的阴影,前景色与背景色的比例为90:100,并使用白色作为前景色,红色作为背景色.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            HatchBrush brush = new HatchBrush(HatchStyle.Percent90, Color.White, Color.Red);
            g.FillEllipse(brush, new Rectangle(10, 10, 300, 100));
        }
    }
}

运行结果:


/*
 * LinearGradientBrush类创建渐变画刷实例。实现由白色渐变到黑色.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            LinearGradientBrush brush1 = new LinearGradientBrush(this.ClientRectangle, Color.White, Color.Black, LinearGradientMode.Vertical);
            g.FillRectangle(brush1, this.ClientRectangle);
            g.Dispose();
        }
    }
}
运行结果:



This Wrox Blox teaches you how to add graphics to C# 2008 applications, explaining fundamental graphics techniques such as: drawing shapes with different colors and line styles; filling areas with colors, gradients, and patterns; drawing text that is properly aligned, sized, and clipped exactly where you want it; manipulating images and saving results in bitmap, JPEG, and other types of files. Also covered are instructions for how to greatly increase your graphics capabilities using transformations. Transformations allow you to move, stretch, or rotate graphics. They also let you work in coordinate systems that make sense for your application. You will also learn how to use all of these techniques in printouts. The author describes the sequence of events that produce a printout and shows how to generate and preview printouts. The final sections describe two powerful new graphic tools that were introduced with .NET Framework 3.0: WPF graphics and FlowDocuments. WPF applications can use XAML graphic commands to declaratively draw and fill the same kinds of shapes that a program can draw by using graphics objects. Finally, a discussion on the FlowDocument object shows you how to define items that should be flowed across multiple pages as space permits. This lets you display text, graphics, controls, and other items that automatically flow across page breaks. FlowDocument viewers make displaying these documents easy for you, and simplifies the user's reading of the documents. This Wrox Blox also contains 35 example programs written in C# 2008, although most of the code works in previous versions of C# as well. The most notable exceptions are WPF graphics and FlowDocuments, both of which require WPF provided in .NET Framework 3.0 and later.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值