net: [[C#]]

画出相交直线

首先,使用Graphics对象在窗体中画出两条相交的直线。将在窗体中央垂直相交的两条直线绘制出来:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
csharp using System.Drawing;
using System.Windows.Forms;
public class Form1 : Form {
public Form1() {
this.Paint += new PaintEventHandler(MainForm_Paint);
}
private void MainForm_Paint(object sender, PaintEventArgs e) {
Graphics g = e.Graphics;
int centerX = this.ClientSize.Width / 2;
int centerY = this.ClientSize.Height / 2;
Pen pen = new Pen(Color.Black);
// 画垂直线
g.DrawLine(pen, new Point(centerX, 0), new Point(centerX, this.ClientSize.Height));
// 画水平线
g.DrawLine(pen, new Point(0, centerY), new Point(this.ClientSize.Width, centerY)); } }

注意,此代码将在窗体的Paint事件中绘制直线,因此要确保在窗体中添加此事件的处理程序。
可以通过this.ClientSize.Widththis.ClientSize.Height来获取窗体的长宽属性。

画出正方形的内切圆

问题一:

在使用DrawRectangle方法进行正方形绘制时出现如图的错误,原因是在上一个函数CrossLine中写了g.dispose(),这是因为我并没有创建新的graphics对象,而是直接使用的统一的e.Graphics作为绘图对象,只要在新函数中创建新的graphics对象:g = this.CreateGraphics();或者将上一个函数的g.dispose()删掉就好。

问题二:
在填充正方形后,圆的内部也被填充为了蓝色,需要将圆内的颜色清除掉,可以使用this.BackColor获取窗体颜色进行填充。

最终代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private void CircleSquare(object sender, PaintEventArgs e){
Graphics g;
g = this.CreateGraphics();
Point location = new Point(this.ClientSize.Width / 8, this.ClientSize.Height / 8);
Rectangle square = new Rectangle(location.X, location.Y, this.ClientSize.Height / 4, this.ClientSize.Height / 4);
Pen penS = new Pen(Color.Green,2);
Pen penC = new Pen(Color.Red);
Brush brush = new SolidBrush(Color.Blue);
g.DrawRectangle(penS, square);
g.FillRectangle(brush, square);
g.DrawEllipse(penC, square);
g.FillEllipse(new SolidBrush(this.BackColor), square);
g.Dispose();
penC.Dispose();
penS.Dispose();
brush.Dispose();
}

用不同颜色,不同字体,显示不同大小的文字

这个到没遇到什么问题,只要选择好字体、字号和字体样式,选不同颜色的brush使用drawstring方法将对应的字符串打印出来即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
private void Rt(object sender, PaintEventArgs e){
Graphics g = e.Graphics;
Font font1 = new Font("Arial", 16);
Font font2 = new Font("Times New Roman", 24, FontStyle.Bold);
Font font3 = new Font("Courier New", 32, FontStyle.Italic);
Brush b1 = new SolidBrush(Color.Orange);
Brush b2 = new SolidBrush(Color.Blue);
Brush b3 = new SolidBrush(Color.DarkGreen);
g.DrawString("new game", font1, b1, new Point(this.ClientSize.Width / 2 + 10, 20));
g.DrawString("select level", font2, b2, new Point(this.ClientSize.Width / 2 + 10, 60));
g.DrawString("quit game", font3, b3, new Point(this.ClientSize.Width / 2 + 10, 80));
b1.Dispose();
b2.Dispose();
b3.Dispose();
}

显示图片

问题一:

提示参数无效是因为没有把图片的扩展名(.png)写完全,补充完整就好了。

接下来调整一下在DrawImage方法中利用rectangle类,调整一下图片的位置和大小就好,下面是完整代码:

1
2
3
4
5
private void Show_picture(object sender, PaintEventArgs e){
Graphics g = e.Graphics;
Bitmap bitmap = new Bitmap(Application.StartupPath+"\\WPS图片(1).png");
g.DrawImage(bitmap, new Rectangle(10, this.ClientSize.Height / 2 + 10, this.ClientSize.Width / 2 - 10, this.ClientSize.Height / 2 - 20));
}

截取图片的一部分

这个部分直接使用bitmap.clone方法对像素进行有选择地截取,再用DrawImage方法将clone的副本打印出来就好。

1
2
3
4
5
6
7
private void Show_part_picture(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Bitmap bitmap = new Bitmap(Application.StartupPath + "\\WPS图片(1).png");
Bitmap copyed = bitmap.Clone(new Rectangle(20, 20, bitmap.Width / 4, bitmap.Height / 4),bitmap.PixelFormat);
g.DrawImage(copyed, new Rectangle(this.ClientSize.Width / 2 + 20, this.ClientSize.Height / 2 + 10, this.ClientSize.Width / 2 - 20, this.ClientSize.Height / 2 - 20));
}

最终效果:


References