C# Graphics的用法?
我想用Graphics绘制图片,是把一些字符串绘制成图片!但是Graphics怎么用?我不太明白,请教大家!...
我想用Graphics绘制图片,是把一些字符串绘制成图片!
但是Graphics怎么用? 我不太明白,请教大家! 展开
但是Graphics怎么用? 我不太明白,请教大家! 展开
2个回答
展开全部
以下是我写的一个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();
}
}
}
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();
}
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
威孚半导体技术
2024-08-19 广告
2024-08-19 广告
威孚(苏州)半导体技术有限公司是一家专注生产、研发、销售晶圆传输设备整机模块(EFEM/SORTER)及核心零部件的高科技半导体公司。公司核心团队均拥有多年半导体行业从业经验,其中技术团队成员博士、硕士学历占比80%以上,依托丰富的软件底层...
点击进入详情页
本回答由威孚半导体技术提供
展开全部
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对象绘制线条和形状、呈现文本或显示与操作图像,所用到的属性和方法如表所示。
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对象绘制线条和形状、呈现文本或显示与操作图像,所用到的属性和方法如表所示。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询