**我在运行时使用以下代码绘制椭圆。在该代码中,我使用图形路径进行绘图(实际上这是项目要求)并使用加宽方法进行图形路径。但它给出了运行时异常“内存不足”。我可以在椭圆的情况下使用这种方法吗?
在运行时绘制矩形的情况下使用加宽方法,它可以正常工作。
请解决这个问题并给我一些建议?**
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Rectangle r;
bool isDown = false;
int initialX;
int initialY;
bool IsDrowing =true;
GraphicsPath gp1;
GraphicsPath gp2;
GraphicsPath gp3;
GraphicsPath gp;
Graphics g;
bool contained;
bool containedE;
bool containedC;
private void Form2_MouseDown(object sender, MouseEventArgs e)
{
isDown = true;
IsDrowing = true;
initialX = e.X;
initialY = e.Y;
}
private void Form2_MouseMove(object sender, MouseEventArgs e)
{
//IsDrowing = true;
if (isDown == true)
{
int width = e.X - initialX, height = e.Y - initialY;
r = new Rectangle(Math.Min(e.X, initialX),
Math.Min(e.Y, initialY),
Math.Abs(e.X - initialX),
Math.Abs(e.Y - initialY));
this.Invalidate();
}
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
g = this.CreateGraphics();
gp = new GraphicsPath();
Pen pen = new Pen(Color.Red);
gp.AddEllipse(r);
gp.Widen(pen);
pen.DashStyle = DashStyle.Dash;
if (IsDrowing)
{
g.DrawPath(pen, gp);
}
private void Form2_MouseUp(object sender, MouseEventArgs e)
{
IsDrowing = false;
this.Refresh();
}
}