我使用GDI+制作绘图板,需要在绘图区添加网格线(辅助线),像在photoshop中一样,应该怎样实现?

解决方案 »

  1.   


    for(int  i = 0;i<10;i++)
       for(int j = 0;j<10;j++)
          g.drawLine (两个点);这样就ok了。、、
      

  2.   

    试下如下代码,不知道是否是楼主想要的?
    protected override void OnPaint(PaintEventArgs e)
    {
    base.OnPaint(e);
    ControlPaint.DrawGrid(e.Graphics, this.ClientRectangle, new Size(8, 8), Color.White);
    }
      

  3.   

    这是我之前写的一个小程序中绘制图形的代码。
    绘制了一个坐标轴,网络线等。      #region 绘图方法        private void panRight_Paint(object sender, PaintEventArgs e)
            {
                //手工绘制坐标轴       
                if (backgroundBuffer == null)
                {
                    Rectangle rect = e.ClipRectangle;
                    BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
                    backgroundBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle);
                    backgroundBuffer.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                    backgroundBuffer.Graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;                Graphics gBG = backgroundBuffer.Graphics;
                    gBG.Clear(Color.White);
                    drawAXIS(gBG);                //绘制坐标轴
                    drawTEXTRefLine(gBG);         //绘制坐标轴文字
                }            if (backBuffer == null)
                {
                    Rectangle rect = e.ClipRectangle;
                    BufferedGraphicsContext currentContext = BufferedGraphicsManager.Current;
                    backBuffer = currentContext.Allocate(e.Graphics, e.ClipRectangle);
                    backBuffer.Graphics.SmoothingMode = SmoothingMode.HighQuality;
                    backBuffer.Graphics.PixelOffsetMode = PixelOffsetMode.HighSpeed;
                }
                //用背景缓冲区渲染双缓冲
                backgroundBuffer.Render(backBuffer.Graphics);            Graphics g = backBuffer.Graphics;            drawSW(g);                  //绘制方波
                drawFrame(g);               //绘制拖放时的边框
                backBuffer.Render(e.Graphics);      //双缓冲防闪烁(暂时未达到效果)
                
                
                        }
                    private void drawFrame(Graphics g)
            {
                if (DragMode != SW.CPos.Other)
                {
                    Pen p = new Pen(Color.Gray, 1);
                    p.DashPattern = new float[] { 1, 1 };                if (DragMode == SW.CPos.Top)
                    {
                        //
                        g.DrawLine(p, new Point(0, this.HFramePos), new Point(panRight.Width, this.HFramePos));
                    }
                    else     //水平拖放
                    {
                        g.DrawLine(p, new Point(this.VFramePos, 0), new Point(this.VFramePos, panRight.Height));                }
                }
            }
            private void drawAXIS(Graphics g)
            {
                Pen p = new Pen(Color.Black, 3);        //创建绘制画笔                        g.DrawLine(p, new Point(opX, opY), new Point(opX + dX * cX, opY));  //画出X轴            g.DrawLine(p, new Point(opX, opY), new Point(opX, opY - dY * cY));  //画出Y轴
            }        private void drawTEXTRefLine(Graphics g)
            {
                SolidBrush sbX = new SolidBrush(Color.Blue);
                SolidBrush sbY = new SolidBrush(Color.Green);            Pen pRefX = new Pen(Color.Blue);
                pRefX.DashPattern = new float[] { 2, 1 };            Pen pRefY = new Pen(Color.Green);
                pRefY.DashPattern = new float[] { 2, 1 };            for (int i = 0; i < cX; i++)
                {
                    //输出坐标标注
                    Point outputPos = new Point(opX + dX * i, opY + 5);
                    g.DrawString(i.ToString(), new Font("Tahoma", 9), sbX, outputPos);                //绘制相应参考线
                    if (i > 0)
                    {
                        Point pS = new Point(opX + dX * i, opY);
                        Point pE = new Point(opX + dX * i, opY - dY * cY);                    g.DrawLine(pRefX, pS, pE);
                    }
                }            for (int i = 0; i < cY; i++)
                {                Point outputPos = new Point(opX - 25, opY - dY * i);
                    g.DrawString(i.ToString(), new Font("Tahoma", 9), sbY, outputPos);                if (i > 0)
                    {
                        Point pS = new Point(opX, opY - dY * i);
                        Point pE = new Point(opX + dX * cX, opY - dY * i);                    g.DrawLine(pRefY, pS, pE);
                    }
                }
            }        private void drawSW(Graphics g)
            {
                //绘制笔
                Pen p = new Pen(Color.Red, 2);
                //方波区域对角点坐标            
                Point LeftTop = new Point(swMain.Left, swMain.Top);
                Point LeftBottom = new Point(swMain.Left, swMain.Top + swMain.Height);            Point RightTop = new Point(swMain.Left + swMain.Width, swMain.Top);
                Point RightBottom = new Point(swMain.Left + swMain.Width, swMain.Top + swMain.Height);            //绘制左侧线
                g.DrawLine(p, LeftTop, LeftBottom);            //绘制右侧线
                g.DrawLine(p, RightTop, RightBottom);            //绘制顶部线
                g.DrawLine(p, LeftTop, RightTop);
            }
            #endregion