110,499
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace WindowsApplication73
{
public partial class Form1 : Form
{
Bitmap OrgBmp = null;
int PosX = 0;
Thread T = null;
delegate void DrawForm(Bitmap CacheBmp);
public Form1()
{
InitializeComponent();
OrgBmp = new Bitmap(this.Width, this.Height);
Graphics G = Graphics.FromImage(OrgBmp);
G.FillRectangle(new SolidBrush(this.BackColor), this.Bounds);
G.Dispose();
T = new Thread(new ThreadStart(Run));
T.IsBackground = true;
T.Start();
}
void Run()
{
Bitmap CacheBmp = null;
Graphics G = null;
try
{
while (true)
{
if (++PosX > this.Width)
PosX = 0;
CacheBmp = new Bitmap(OrgBmp);
G = Graphics.FromImage(CacheBmp);
G.DrawString("ABCDEFG12345", new Font("宋体", 10), Brushes.Green, new PointF(PosX, 0));
if (!this.IsDisposed && this.IsHandleCreated)
this.Invoke(new DrawForm(DoDrawForm), new Object[] { CacheBmp });
}
}
catch
{
}
finally
{
if (G != null)
G.Dispose();
if (CacheBmp != null)
{
CacheBmp.Dispose();
GC.Collect();
}
}
}
void DoDrawForm(Bitmap CacheBmp)
{
Graphics G = null;
try
{
G = this.CreateGraphics();
G.DrawImage(CacheBmp, new Point(0, 0));
}
finally
{
if (G != null)
G.Dispose();
GC.Collect();
}
}
}
}
BufferedGraphicsContext current = BufferedGraphicsManager.Current; //(1)
BufferedGraphics bg;
bg = current.Allocate(this.CreateGraphics(),this.DisplayRectangle); //(2)
Graphics g = bg.Graphics;//(3)
//随机 宽400 高400
System.Random rnd = new Random();
int x,y,w,h,r,i;
for (i = 0; i < 10000; i++)
{
x = rnd.Next(400);
y = rnd.Next(400);
r = rnd.Next(20);
w = rnd.Next(10);
h = rnd.Next(10);
g.DrawEllipse(Pens.Blue, x, y, w, h);
}
bg.Render();//(4)
//bg.Render(this.CreateGraphics());
bg.Dispose();//(5)
自己开辟一个缓冲区(如一个不显示的Bitmap对象),在其中绘制完成后,再一次性显示。
完整代码如下:
Bitmap bt = new Bitmap(400, 400);
Graphics bg = Graphics.FromImage(bt);
System.Random rnd = new Random();
int x, y, w, h, r, i;
for (i = 0; i < 10000; i++)
{
x = rnd.Next(400);
y = rnd.Next(400);
r = rnd.Next(20);
w = rnd.Next(10);
h = rnd.Next(10);
bg.DrawEllipse(Pens.Blue, x, y, w, h);
}
this.CreateGraphics().DrawImage(bt, new Point(0, 0));
本文来源于网络小筑 http://www.iwebtrados.com.cn/ , 原文地址:http://www.iwebtrados.com.cn/post/41.html