C#中GDI+编程10个基本技巧二
5、渐变色填充
需要使用两个刷子:
线性梯度刷子(LinearGradientBrush)
路径梯度刷子(PathGuadientBrush)
private void button4_Click(object sender,System.EventArgs e)
{
//绘图表面
Graphics g =this.pictureBoxII1.CreateGraphics();
g.FillRectangle(Brushes.White,this.pictureBoxII1.ClientRectangle);
//定义一个线性梯度刷子
LinearGradientBrush lgbrush =
new LinearGradientBrush(
new Point(0, 10),
new Point(150, 10),
Color.FromArgb(255, 0, 0),
Color.FromArgb(0, 255, 0));
Pen pen = new Pen(lgbrush);
//用线性笔刷梯度效果的笔绘制一条直线段并填充一个矩形
g.DrawLine(pen, 10, 130, 500, 130);
g.FillRectangle(lgbrush, 10, 150, 370, 30);
//定义路径并添加一个椭圆
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(10, 10, 200, 100);
//用该路径定义路径梯度刷子
PathGradientBrush brush =
new PathGradientBrush(gp);
//颜色数组
Color[] colors = {
Color.FromArgb(255, 0, 0),
Color.FromArgb(100, 100, 100),
Color.FromArgb(0, 255, 0),
Color.FromArgb(0, 0, 255)};
//定义颜色渐变比率
float[] r = {0.0f, 0.3f, 0.6f, 1.0f};
ColorBlend blend = new ColorBlend();
blend.Colors = colors;
blend.Positions = r;
brush.InterpolationColors = blend;
//在椭圆外填充一个矩形
g.FillRectangle(brush, 0, 0, 210, 110);
//用添加了椭圆的路径定义第二个路径梯度刷子
GraphicsPath gp2 = new GraphicsPath();
gp2.AddEllipse(300, 0, 200, 100);
PathGradientBrush brush2 = new PathGradientBrush(gp2);
//设置中心点位置和颜色
brush2.CenterPoint = new PointF(450, 50);
brush2.CenterColor = Color.FromArgb(0, 255, 0);
//设置边界颜色
Color[] color2 = {Color.FromArgb(255, 0, 0)};
brush2.SurroundColors = color2;
//用第二个梯度刷填充椭圆
g.FillEllipse(brush2, 300, 0, 200, 100);
}
6、GDI+的坐标系统
通用坐标系——用户自定义坐标系。
页面坐标系——虚拟坐标系。
设备坐标系——屏幕坐标系。
当页面坐标系和设备坐标系的单位都是象素时,它们相同。
private void button10_Click(object sender, System.EventArgse)
{
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
this.Draw(g);
}
private void Draw(Graphics g)
{
g.DrawLine(Pens.Black, 10, 10, 100, 100);
g.DrawEllipse(Pens.Black, 50, 50, 200, 100);
g.DrawArc(Pens.Black, 100, 10, 100, 100, 20, 160);
g.DrawRectangle(Pens.Green, 50, 200, 150, 100);
}
private void button5_Click(object sender, System.EventArgs e)
{
//左移
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.TranslateTransform(-50, 0);
this.Draw(g);
}
private void button6_Click(object sender, System.EventArgs e)
{
//右移
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.TranslateTransform(50, 0);
this.Draw(g);
}
private void button7_Click(object sender, System.EventArgs e)
{
//旋转
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.RotateTransform(-30);
this.Draw(g);
}
private void button8_Click(object sender, System.EventArgs e)
{
//放大
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.ScaleTransform(1.2f, 1.2f);
this.Draw(g);
}
private void button9_Click(object sender, System.EventArgs e)
{
//缩小
Graphics g = this.pictureBoxII1.CreateGraphics();
g.Clear(Color.White);
g.ScaleTransform(0.8f, 0.8f);
this.Draw(g);
}
7、全局坐标——变换对于绘图表面上的每个图元都会产生影响。通常用于设定通用坐标系。
下程序将原定点移动到控件中心,并且Y轴正向朝上。
//先画一个圆
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, this.ClientRectangle);
g.DrawEllipse(Pens.Black, -100, -100, 200, 200);
//使y轴正向朝上,必须做相对于x轴镜像
//变换矩阵为[1,0,0,-1,0,0]
Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);
g.Transform = mat;
Rectangle rect = this.ClientRectangle;
int w = rect.Width;
int h = rect.Height;
g.TranslateTransform(w/2, -h/2);
//以原点为中心,做一个半径为100的圆
g.DrawEllipse(Pens.Red, -100, -100, 200, 200);
g.TranslateTransform(100, 100);
g.DrawEllipse(Pens.Green, -100, -100, 200, 200);
g.ScaleTransform(2, 2);
g.DrawEllipse(Pens.Blue, -100, -100, 200, 200);
8、局部坐标系——只对某些图形进行变换,而其它图形元素不变。
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//客户区设置为白色
g.FillRectangle(Brushes.White, this.ClientRectangle);
//y轴朝上
Matrix mat = new Matrix(1, 0, 0, -1, 0, 0);
g.Transform = mat;
//移动坐标原点到窗体中心
Rectangle rect = this.ClientRectangle;
int w = rect.Width;
int h = rect.Height;
g.TranslateTransform(w/2, -h/2);
//在全局坐标下绘制椭圆
g.DrawEllipse(Pens.Red, -100, -100, 200, 200);
g.FillRectangle(Brushes.Black, -108, 0, 8, 8);
g.FillRectangle(Brushes.Black, 100, 0, 8, 8);
g.FillRectangle(Brushes.Black, 0, 100, 8, 8);
g.FillRectangle(Brushes.Black, 0, -108, 8, 8);
//创建一个椭圆然后在局部坐标系中进行变换
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(-100, -100, 200, 200);
Matrix mat2 = new Matrix();
//平移
mat2.Translate(150, 150);
//旋转
mat2.Rotate(30);
gp.Transform(mat2);
g.DrawPath(Pens.Blue, gp);
PointF[] p = gp.PathPoints;
g.FillRectangle(Brushes.Black, p[0].X-2, p[0].Y+2, 4, 4);
g.FillRectangle(Brushes.Black, p[3].X-2, p[3].Y+2, 4, 4);
g.FillRectangle(Brushes.Black, p[6].X-4, p[6].Y-4, 4, 4);
g.FillRectangle(Brushes.Black, p[9].X-4, p[9].Y-4, 4, 4);
gp.Dispose();
//base.OnPaint (e);
}
9、Alpha混合
Color.FromArgb()的A就是Alpha。Alpha的取值范围从0到255。0表示完全透明,255完全不透明。
当前色=前景色×alpha/255+背景色×(255-alpha)/255
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//创建一个填充矩形
SolidBrush brush = new SolidBrush(Color.BlueViolet);
g.FillRectangle(brush, 180, 70, 200, 150);
//创建一个位图,其中两个位图之间有透明效果
Bitmap bm1 = new Bitmap(200, 100);
Graphics bg1 = Graphics.FromImage(bm1);
SolidBrush redBrush =
new SolidBrush(Color.FromArgb(210, 255, 0, 0));
SolidBrush greenBrush =
new SolidBrush(Color.FromArgb(210, 0, 255, 0));
bg1.FillRectangle(redBrush, 0, 0, 150, 70);
bg1.FillRectangle(greenBrush, 30, 30, 150, 70);
g.DrawImage(bm1, 100, 100);
//创建一个位图,其中两个位图之间没有透明效果
Bitmap bm2 = new Bitmap(200, 100);
Graphics bg2 = Graphics.FromImage(bm2);
bg2.CompositingMode = CompositingMode.SourceCopy;
bg2.FillRectangle(redBrush, 0, 0, 150, 170);
bg2.FillRectangle(greenBrush, 30, 30, 150, 70);
g.CompositingQuality = CompositingQuality.GammaCorrected;
g.DrawImage(bm2, 300, 200);
//base.OnPaint (e);
}
10、反走样
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
//放大8倍
g.ScaleTransform(8, 8);
//没有反走样的图形和文字
Draw(g);
//设置反走样
g.SmoothingMode = SmoothingMode.AntiAlias;
//右移40
g.TranslateTransform(40, 0);
//再绘制就是反走样之后的了
Draw(g);
//base.OnPaint (e);
}
private void Draw(Graphics g)
{
//绘制图形和文字
g.DrawLine(Pens.Gray, 10, 10, 40, 20);
g.DrawEllipse(Pens.Gray, 20, 20, 30, 10);
string s = "反走样测试";
Font font = new Font("宋体", 5);
SolidBrush brush = new SolidBrush(Color.Gray);
g.DrawString(s, font, brush, 10, 40);
}
更多 C#中GDI+编程10个基本技巧二相关文章请关注PHP中文网!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

本文探讨了C中的无指针启用的挑战。它认为问题本身不是零,而是滥用。 本文详细介绍了预防退出的最佳实践,包括提出前检查,指针pitiberi

本文解释了如何使用printf中的\ n逃脱序列在C中创建新线字符并列出函数。 它详细介绍了功能并提供了代码示例,以说明其用于输出中的线路断裂。

本文指导初学者选择C编译器。 它认为,海湾合作委员会由于其易用性,广泛的可用性和广泛的资源,最适合初学者。 但是,它也比较了海湾室,Clang,MSVC和TCC,突出了它们的差异

本文强调了NULL在现代C编程中的持续重要性。 尽管取得了进步,但NULL对于明确的指针管理仍然至关重要,从而通过标记没有有效的内存地址来防止细分故障。 最好的prac

本文回顾了初学者的在线C编译器,重点是易用性和调试功能。 在线GDB和REPL。 其他选项,例如Programiz和Compil

本文比较在线C编程平台,突出了诸如调试工具,IDE功能,标准合规性和内存/执行限制等功能的差异。 它认为“最佳”平台取决于用户需求

本文讨论了C IDE中的有效代码复制。 它强调,复制是IDE功能,而不是编译器功能,并且详细提高了效率的策略,包括使用IDE选择工具,代码折叠,搜索/替换,Templa

本文在C程序编译中对缺少输出窗口进行故障排除。 它研究了诸如无法运行可执行文件,程序错误,错误编译器设置,背景过程和快速程序终止之类的原因。解决方案涉及ch
