遇到关于GDI+画图的问题

open422000 2012-08-28 09:41:55
现在一个小问题

我将画的线存起来,一次性画出来,想实现就是连续画线的功能,并且能够随时取到里面的点,然后进行点与点之间的捕捉
我现在程序的问题是在画布上,连线条都没显示出来,不知道什么原因



在winform里面的
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D ;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DrawWall
{
public partial class DrawWall : Form
{

bool isDrawLine = false;//标志是否进入画线状态


PointF first;
PointF start;

public DrawWall()
{
InitializeComponent();

}

private void InitCanvas(PictureBox pCanvas)
{
int canvasHeight = pCanvas.Height;
int canvasWidth = pCanvas.Width;
int space = 30;
Bitmap bitMap = new Bitmap(pCanvas.Width, pCanvas.Height);
Canvas.DrawCanvas(canvasWidth, canvasHeight, bitMap, space);
pCanvas.Image = bitMap;
}



private void DrawWall_Load(object sender, EventArgs e)
{
InitCanvas(pCanvas);
}


private void Draw_Click(object sender, EventArgs e)
{
if (isDrawLine)
{
isDrawLine = false;
}

else
{
isDrawLine = true;
}

}

private void pCanvas_MouseClick(object sender, MouseEventArgs e)
{
//按下鼠标时发生

if (e.Button == MouseButtons.Left)
{
if (Vector.ListPt.Count == 0 && isDrawLine)
{
first = e.Location;
start = e.Location;
Vector.SavePoint(start);
}


}
}

private void pCanvas_Paint(object sender, PaintEventArgs e)
{

}

private void pCanvas_MouseMove(object sender, MouseEventArgs e)
{
if (isDrawLine)
{
Vector.PointCurrent = e.Location;

}

}

protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = pCanvas.CreateGraphics();
DrawTools.DrawAllLines(g);

}




}


}

自己定义的类

Canvas.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DrawWall
{
public class Canvas
{
#region 创建画布

/// <summary>
/// 创建画布,画网格
/// </summary>
/// <param name="width">画布的宽度</param>
/// <param name="height">画布的高度</param>
/// <param name="bitMap">画布的位图</param>
/// <param name="space">网格线的间距</param>
public static void DrawXY(float width, float height, Bitmap bitMap, int space)
{

int penWidth = 1;
Pen penCenter = new Pen(Color.Red, penWidth);

Graphics g = Graphics.FromImage(bitMap);
Pen penXY = new Pen(Color.Black, penWidth);
float y = 0;
float x = 0;


for (int i = 0; i < height / space; i++)
{
PointF pt1 = new PointF(0, y + i * space);
PointF pt2 = new PointF(width, y + i * space);
float flag = Math.Abs(i * space) - (height / 2);

// if ((flag >= 0 && flag < space/2) || flag <= 0 && flag > -space/2)
if (Math.Abs(flag) < space / 2)
{
g.DrawLine(penCenter, pt1, pt2);
}
else
g.DrawLine(penXY, pt1, pt2);

}

for (int i = 0; i < width / space; i++)
{
PointF pt3 = new PointF(x + i * space, 0);
PointF pt4 = new PointF(x + i * space, height);
float flag = Math.Abs(i * space) - (width / 2);
//if ((flag >= 0 && flag < space/2) || flag <= 0 && flag > -space/2)
if (Math.Abs(flag) < space / 2)
{
g.DrawLine(penCenter, pt3, pt4);
}
else
{
g.DrawLine(penXY, pt3, pt4);
}

}
g.Dispose();
penCenter.Dispose();
penXY.Dispose();

}

public static void DrawCanvas(int canvasWidth, int canvasHeight, Bitmap bitMap, int space)
{

DrawXY(canvasWidth, canvasHeight, bitMap, space);
}

#endregion



}
}



DrawTools.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DrawWall
{
class DrawTools
{

public static void DrawAllLines(Graphics g)
{

if (Vector.ListPt.Count != 0)
{
Pen pen = new Pen(Color.Blue, 1);

g.DrawLines(pen, Vector.ListPt.ToArray());
g.DrawLine(pen, Vector.GetLastPoint(), Vector.PointCurrent);
}


}
}
}



Vector.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace DrawWall
{
class Vector
{

private static PointF pointCurrent;

public static PointF PointCurrent
{
get { return pointCurrent; }
set { pointCurrent = value; }
}

private static List<PointF> listPt;

public static List<PointF> ListPt
{
get { return listPt; }
set { listPt = value; }
}


public static void SavePoint(PointF pf)
{
listPt.Add(pf);
}


public static PointF GetLastPoint()
{
if (listPt.Count == 0)
return pointCurrent;
return listPt[listPt.Count - 1];

}

}

}

求高手老师指点
...全文
4508 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
fzy_2237505291 2014-06-22
  • 打赏
  • 举报
回复
绘制图形路径 protected void Page_Load(object sender, EventArgs e) { Bitmap bitmap = new Bitmap(300, 150); Graphics g = Graphics.FromImage(bitmap); GraphicsPath gPath = new GraphicsPath(); g.Clear(Color.YellowGreen); Font font1 = new Font("宋体", 12); //设置字体类型和大小 Brush brush = new SolidBrush(Color.Blue); //设置画刷颜色 Pen myPen = new Pen(Color.Blue, 5); //创建画笔对象 g.DrawString("GDI+绘制绘制图形路径", font1, brush, 80, 5); Point[] myPoints = { new Point(15, 130), new Point(30, 40), new Point(50, 130) }; gPath.AddArc(50, 100, 80, 50, 210, 180);//针对圆弧 gPath.StartFigure();//CodeGo.net/ gPath.AddCurve(myPoints);//针对基数样条 gPath.AddPie(150, 80, 80, 100, 210, 120);//针对扇形 g.DrawPath(myPen, gPath); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Gif); Response.ClearContent(); Response.ContentType = "image/Gif"; Response.BinaryWrite(ms.ToArray()); }
open422000 2012-08-29
  • 打赏
  • 举报
回复
我想问下 怎么做成控件呢、、大概。。
bdmh 2012-08-28
  • 打赏
  • 举报
回复
Graphics g = pCanvas.CreateGraphics(); 直接使用e.Graphics
bigbaldy 2012-08-28
  • 打赏
  • 举报
回复
DrawXY和DrawCanvas的bitMap参数前加ref
E次奥 2012-08-28
  • 打赏
  • 举报
回复
画线貌似不好弄。

可以做成控件啊。自己维护自己的状态!

8,834

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 组件/控件开发
社区管理员
  • 组件/控件开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧