C#绘制图象
要想在页面或窗体上绘出一张图像或图形,如果是BS必须要有一张图像,CS可以不用
CS下地做法:
一:定义GDI
Graphics q = this.CreateGraphics();//在using System.Drawing;里
二:定义颜色,可自定义或用系统的,
Random r = new Random();//随机类
自定义颜色:
Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));//用随机实现RGB调色,
自定义颜色类型:
Brush test = new LinearGradientBrush(new Rectangle(20, 20, 50, 50), Color.Blue, Color.Green, 45);//定义渐近色颜色
Brush t = new TextureBrush(s, new Rectangle(0, 0, s.Width, s.Height));//自定义背景颜色
自定义背景颜色如果是用图像做背景的话,就要定义图像类:
Bitmap b = new Bitmap("5.jpg");
Bitmap s = new Bitmap(b, new Size(b.Width / 10, b.Height / 10));//这两种都可以
绘图:
q.FillRectangle(test, new Rectangle(40, 40, 400, 300));//填充矩形
q.FillEllipse(t, ClientRectangle);//填充椭圆
注意:要想图形有颜色,要使用填充类的,不能是绘制!!
画线:
Pen p = new Pen(Color.Red, 2);
Point[] po = new Point[3];
po[0] = new Point(10, 20);
po[1] = new Point(50, 100);
po[2] = new Point(400, 200);
t.DrawLines(p, po);
画图:
b.DrawImage(b, ClientRectangle);//ClientRectangle表示作为背景绘制;
画矩形:
t.DrawRectangle(p, 200, 300, 80, 100);
另2个绘制随机线的方法:
/// <summary>
/// 可见的慢慢地画出随机色线条
/// </summary>
private void DrawRandomLines()
{
Graphics q = this.CreateGraphics();
Random r = new Random();
for (int x = 0; x < ClientRectangle.Width; x++)
{
for (int y = 0; y < ClientRectangle.Height; y += 10)
{
Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
Pen p = new Pen(c, 1);
q.DrawLine(p, 250, 250, x, y);
p.Dispose();
}
}
}
/// <summary>
/// 快速画出随机色线
/// </summary>
/// <param name="q">Graphics对象</param>
private void DrawRandomLines(Graphics q)
{
Image i = new Bitmap(ClientRectangle.Width, ClientRectangle.Height);
Graphics g = Graphics.FromImage(i);
Random r = new Random();
for (int x = 0; x < ClientRectangle.Width; x++)
{
for (int y = 0; y < ClientRectangle.Height; y += 10)
{
Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
Pen p = new Pen(c, 1);
g.DrawLine(p, 250, 250, x, y);
p.Dispose();
}
}
q.DrawImage(i, ClientRectangle);
i.Dispose();
}
B/S的做法:
必须先新建图像:
string p = "snap.bmp";
Bitmap bit = new Bitmap(Server.MapPath(p));
Bitmap b = new Bitmap(bit, new Size(bit.Width / 10, bit.Height / 10));
Graphics t = Graphics.FromImage(bit);
自定义颜色:
Brush br = new LinearGradientBrush(new Rectangle(10, 10, 100, 100), Color.Green, Color.Blue, 45);//定义渐近色颜色
Brush br2 = new TextureBrush(b, new RectangleF(0, 0, b.Width, b.Height));//自定义背景颜色
定义画笔:
Pen newpen = new Pen(br, 2);
t.DrawLine(Pens.Red, new Point(100, 200), new Point(500, 600));//画线
t.FillRectangle(br2, new Rectangle(0, 0, 300, 300));//填充矩形
t.DrawImage(b, new Rectangle(100,100,100,100));//画图像
t.DrawString("AAA", new Font("ff", 10, FontStyle.Bold), Brushes.Blue, new PointF(0, 0));//画字符串
bit.Save(Response.OutputStream, ImageFormat.Jpeg);//表示画到那里,这里是页面,这是与CS最大的区别
bit.Dispose();//释放资源
t.Dispose();//释放资源
同样2个画随机线的方法:
/// <summary>
/// 可见的慢慢地画出随机色线条
/// </summary>
private void DrawRandomLines()
{
string pa = "snap.bmp";新建图像
Bitmap bit = new Bitmap(Server.MapPath(pa));
Graphics q = Graphics.FromImage(bit);
Random r = new Random();
for (int x = 0; x < 1000; x+=20)
{
for (int y = 0; y < 1000; y += 20)
{
Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
Pen p = new Pen(c, 1);
q.DrawLine(p, 250, 250, x, y);
p.Dispose();
}
}
bit.Save(Response.OutputStream, ImageFormat.Jpeg);
//bit.Dispose();
}
/// <summary>
/// 快速画出随机色线
/// </summary>
private void DrawRandomLiness()
{
string pa = "snap.bmp";新建图像
Bitmap bit = new Bitmap(Server.MapPath(pa));
Graphics g = Graphics.FromImage(bit);
Random r = new Random();
for (int x = 0; x < 1000; x+=50)
{
for (int y = 0; y < 1000; y += 50)
{
Color c = Color.FromArgb(r.Next(255), r.Next(255), r.Next(255));
Pen p = new Pen(c, 1);
g.DrawLine(p, 250, 250, x, y);
p.Dispose();
}
}
g.DrawImage(bit, new Rectangle(0, 0, 1000, 1000));
bit.Save(Response.OutputStream, ImageFormat.Jpeg);
bit.Dispose();
}