c# 如何在panel上绘图??
下面给出代码,请高手帮忙看看是哪里出错了??非常感谢啊。usingSystem;usingSystem.Collections.Generic;usingSystem.C...
下面给出代码,请高手帮忙看看是哪里出错了??非常感谢啊。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsApplication1.els
{
public partial class els : Form
{
public Color[] mycolor = new Color[] { Color.Black, Color.Brown };
public Point mypoint;
public Pen mypen = new Pen(Brushes.Black);
public els()
{
InitializeComponent();
draw();
}
void draw()
{
Graphics g =this.panelEx1.CreateGraphics();
GraphicsPath gp = new GraphicsPath();
Rectangle rec = new Rectangle(new Point(30, 30), new Size(100, 30));
gp.AddRectangle(rec);
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.Blue;
pb.SurroundColors = mycolor;
g.DrawRectangle(mypen, rec);
g.FillPath(pb, gp);
}
}
} 展开
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsApplication1.els
{
public partial class els : Form
{
public Color[] mycolor = new Color[] { Color.Black, Color.Brown };
public Point mypoint;
public Pen mypen = new Pen(Brushes.Black);
public els()
{
InitializeComponent();
draw();
}
void draw()
{
Graphics g =this.panelEx1.CreateGraphics();
GraphicsPath gp = new GraphicsPath();
Rectangle rec = new Rectangle(new Point(30, 30), new Size(100, 30));
gp.AddRectangle(rec);
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.Blue;
pb.SurroundColors = mycolor;
g.DrawRectangle(mypen, rec);
g.FillPath(pb, gp);
}
}
} 展开
展开全部
由于Panel采用系统支持的绘图模式, 所以你直接在上面绘图很有可能被系统重绘覆盖. 我帮你修改了绘图函数, 在Panel的BackGroundImage上绘图, 见代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsApplication1.els
{
public partial class els : Form
{
public Color[] mycolor = new Color[] { Color.Black, Color.Brown };
public Point mypoint;
public Pen mypen = new Pen(Brushes.Black);
public els()
{
InitializeComponent();
draw();
}
void draw() //修改的绘图函数
{
Image im = new Bitmap(this.panelEx1.Width, this.panelEx1.Height);
Graphics g = Graphics.FromImage(im);
GraphicsPath gp = new GraphicsPath();
Rectangle rec = new Rectangle(new Point(30, 30), new Size(100, 30));
gp.AddRectangle(rec);
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.Blue;
pb.SurroundColors = mycolor;
g.DrawRectangle(mypen, rec);
g.FillPath(pb, gp);
this.panelEx1.BackgroundImageLayout = ImageLayout.Stretch;
this.panelEx1.BackgroundImage = im;
}
}
}
已经调试通过, 希望对你有帮助.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace WindowsApplication1.els
{
public partial class els : Form
{
public Color[] mycolor = new Color[] { Color.Black, Color.Brown };
public Point mypoint;
public Pen mypen = new Pen(Brushes.Black);
public els()
{
InitializeComponent();
draw();
}
void draw() //修改的绘图函数
{
Image im = new Bitmap(this.panelEx1.Width, this.panelEx1.Height);
Graphics g = Graphics.FromImage(im);
GraphicsPath gp = new GraphicsPath();
Rectangle rec = new Rectangle(new Point(30, 30), new Size(100, 30));
gp.AddRectangle(rec);
PathGradientBrush pb = new PathGradientBrush(gp);
pb.CenterColor = Color.Blue;
pb.SurroundColors = mycolor;
g.DrawRectangle(mypen, rec);
g.FillPath(pb, gp);
this.panelEx1.BackgroundImageLayout = ImageLayout.Stretch;
this.panelEx1.BackgroundImage = im;
}
}
}
已经调试通过, 希望对你有帮助.
展开全部
代码编译没错。
只不过不知道你想要的效果是怎么样的,是要点击控件激发事件,还是你想一运行程序就绘制出图形?
我试了下,在按钮事件中添加draw()方法,可以绘制出图形;但是我在Form_Load事件中添加draw()方法,却不能一运行程序就绘制出图形,这个问题我也是在困惑当中。
只不过不知道你想要的效果是怎么样的,是要点击控件激发事件,还是你想一运行程序就绘制出图形?
我试了下,在按钮事件中添加draw()方法,可以绘制出图形;但是我在Form_Load事件中添加draw()方法,却不能一运行程序就绘制出图形,这个问题我也是在困惑当中。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
给个邮箱啊,不然怎么发你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询