C# Graphics的用法?
发布网友
发布时间:2022-05-01 00:22
我来回答
共2个回答
热心网友
时间:2022-06-21 08:05
以下是我写的一个WinForm控件.验证码的.你看看就知道了.其实字符串变图片就是这样的
public partial class CodeControl : UserControl
{
private string _code = "";
public string Code {
get { return this._code; }
}
public CodeControl()
{
InitializeComponent();
}
public void CreateCodeImage() {
string[] str = new string[4];
string serverCode = "";
//生成随机生成器
Random random = new Random();
_code = "";
for (int i = 0; i < 4; i++)
{
str[i] = random.Next(10).ToString().Substring(0, 1);
_code += str[i];
}
CreateCodeImage(str);
}
private void CreateCodeImage(string[] checkCode)
{
if (checkCode == null || checkCode.Length <= 0)
return;
//System.Drawing.Bitmap image = new System.Drawing.Bitmap((int)Math.Ceiling((checkCode.Length * 32.5)), this.CodePictureBox.Height);
System.Drawing.Bitmap image = new System.Drawing.Bitmap(this.CodePictureBox.Width, this.CodePictureBox.Height);
System.Drawing.Graphics g = Graphics.FromImage(image);
try
{
Random random = new Random();
//清空图片背景色
g.Clear(Color.White);
//画图片的背景噪音线
for (int i = 0; i < 20; i++)
{
int x1 = random.Next(image.Width);
int x2 = random.Next(image.Width);
int y1 = random.Next(image.Height);
int y2 = random.Next(image.Height);
g.DrawLine(new Pen(Color.Silver), x1, y1, x2, y2);
}
//定义颜色
Color[] c = { Color.Black, Color.Red, Color.DarkBlue, Color.Green, Color.Orange, Color.Brown, Color.DarkCyan, Color.Purple };
//定义字体
string[] f = { "Verdana", "Microsoft Sans Serif", "Comic Sans MS", "Arial", "宋体" };
for (int k = 0; k <= checkCode.Length - 1; k++)
{
int cindex = random.Next(7);
int findex = random.Next(5);
Font drawFont = new Font(f[findex], 16, (System.Drawing.FontStyle.Bold));
SolidBrush drawBrush = new SolidBrush(c[cindex]);
float x = 3.0F;
float y = 0.0F;
float width = 20.0F;
float height = 25.0F;
int sjx = random.Next(10);
int sjy = random.Next(image.Height - (int)height);
RectangleF drawRect = new RectangleF(x + sjx + (k * 14), y + sjy, width, height);
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Center;
g.DrawString(checkCode[k], drawFont, drawBrush, drawRect, drawFormat);
}
//画图片的前景噪音点
for (int i = 0; i < 100; i++)
{
int x = random.Next(image.Width);
int y = random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver), 0, 0, image.Width - 1, image.Height - 1);
System.IO.MemoryStream ms = new System.IO.MemoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
this.CodePictureBox.Image = Image.FromStream(ms);
}
finally
{
g.Dispose();
image.Dispose();
}
}
}
热心网友
时间:2022-06-21 08:05
C# graphics方法
C# graphics方法
命名空间:System.Drawing
程序集:System.Drawing(在 system.drawing.dll 中)
封装一个 GDI+ 绘图图面。无法继承此类。
C# 用法
public sealed class Graphics : MarshalByRefObject, IDeviceContext, IDisposable
System.Drawing.Pen myPen = new System.Drawing.Pen(System.Drawing.Color.Red);//画笔 System.Drawing.SolidBrush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);//画刷 System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.FillEllipse(myBrush, new Rectangle(0,0,100,200));//画实心椭圆
formGraphics.DrawEllipse(myPen, new Rectangle(0,0,100,200));//空心圆
formGraphics.FillRectangle(myBrush, new Rectangle(0,0,100,200));//画实心方
formGraphics.DrawRectangle(myPen, new Rectangle(0,0,100,200));//空心矩形
formGraphics.DrawLine(myPen, 0, 0, 200, 200);//画线
formGraphics.DrawPie(myPen,90,80,140,40,120,100); //画馅饼图形 //画多边形
formGraphics.DrawPolygon(myPen,new Point[]{ new Point(30,140), new Point(270,250), new Point(110,240), new Point (200,170), new Point(70,350), new Point(50,200)}); //清理使用的资源 myPen.Dispose(); myBrush.Dispose(); formGraphics.Dispose();
使用Graphics对象绘制线条和形状、呈现文本或显示与操作图像,所用到的属性和方法如表所示。