c#常用图形绘制方法

封装的一些常用图形的绘制类:


 /// <summary>
    /// 选择图片类
    /// </summary>
    public class DrawChoose
    {
        #region 选择图片
        /// <summary>
        /// 选择图片
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public static void drawChoose(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(Color.DodgerBlue))
            {
                pen.DashStyle = DashStyle.Custom;
                pen.DashPattern = new float[] { 1f, 1f };
                graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, 
                    endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);


                graphics.FillRectangle(Brushes.DodgerBlue,
                    new Rectangle(startPoint.X - 3, startPoint.Y - 3, 3, 3));


                graphics.FillRectangle(Brushes.DodgerBlue,
                    new Rectangle(endPoint.X, endPoint.Y, 3, 3));


                graphics.FillRectangle(Brushes.DodgerBlue, 
                    new Rectangle(endPoint.X, startPoint.Y - 3, 3,3));


                graphics.FillRectangle(Brushes.DodgerBlue,
                    new Rectangle(startPoint.X - 3, endPoint.Y, 3, 3));


                graphics.FillRectangle(Brushes.DodgerBlue,
                    new Rectangle((startPoint.X + endPoint.X) / 2 - 1, startPoint.Y -3, 3, 3));


                graphics.FillRectangle(Brushes.DodgerBlue,
                    new Rectangle((startPoint.X + endPoint.X) / 2 - 1, endPoint.Y, 3, 3));


                graphics.FillRectangle(Brushes.DodgerBlue,
                    new Rectangle(startPoint.X -3, (startPoint.Y + endPoint.Y) / 2, 3, 3));


                graphics.FillRectangle(Brushes.DodgerBlue,
                    new Rectangle(endPoint.X, (startPoint.Y + endPoint.Y) / 2, 3, 3));
            }
        } 
        #endregion

}


/// <summary>

    /// 实心椭圆类
    /// </summary>
    [Serializable]public class Circle:ShapeBase
    {
        #region 绘制实心椭圆
        /// <summary>
        /// 绘制实心椭圆
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                graphics.FillEllipse(new SolidBrush(color), startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }
        #endregion       

    }


  /// <summary>
    /// 图片类
    /// </summary>
    [Serializable]public class DrawImage:Rectangles
    {
        private Image myImage;
        /// <summary>
        /// 存储图片数据
        /// </summary>
        public Image MyImage
        {
            get { return myImage; }
            set { myImage = value; }
        }
}

        #region 绘制图片
        /// <summary>
        /// 绘制图片
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                graphics.DrawImage(myImage, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }  
        #endregion

}

/// <summary>
    /// 实心矩形类
    /// </summary>
    [Serializable]public class Orthogon : ShapeBase
    {
        #region 绘制实心矩形
        /// <summary>
        /// 绘制实心矩形
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color))
            {
                graphics.FillRectangle(new SolidBrush(color), startPoint.X, startPoint.Y,
                    endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }
        #endregion

}


/// <summary>
    /// 空心矩形类
    /// </summary>
    [Serializable]public class Rectangles : ShapeBase
    {
        #region 绘制空心矩形
        /// <summary>
        /// 绘制空心矩形
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                graphics.DrawRectangle(pen, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        } 
        #endregion

}

/// <summary>
    /// 空心直角三角形类
    /// </summary>
    [Serializable]
    public class RightTriangle : ShapeBase
    {
        #region 绘制空心直角三角形
        /// <summary>
        /// 绘制空心直角三角形
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                Point leftPoint = new Point(startPoint.X,endPoint.Y);
                Point[] pointArry = { startPoint, leftPoint, endPoint };
                graphics.DrawPolygon(pen, pointArry);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }
        #endregion

}

/// <summary>
    /// 空心椭圆类
    /// </summary>
    [Serializable]
    public class Roundness : ShapeBase
    {
        #region 绘制空心椭圆
        /// <summary>
        /// 绘制空心椭圆
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                graphics.DrawEllipse(pen, startPoint.X, startPoint.Y, endPoint.X - startPoint.X, endPoint.Y - startPoint.Y);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }
        #endregion

}

 /// <summary>
    /// 实心直角三角形类
    /// </summary>
    [Serializable]
    public class SolidRightTriangle : ShapeBase
    {
        #region 绘制实心直角三角形
        /// <summary>
        /// 绘制实心直角三角形
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                Point leftPoint = new Point(startPoint.X, endPoint.Y);
                Point[] pointArry = { startPoint, leftPoint, endPoint };
                graphics.FillPolygon(new SolidBrush(color), pointArry);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }
        #endregion

}

/// <summary>
    /// 直线类
    /// </summary>
    [Serializable]
    public class StraightLine : ShapeBase
    {
        #region 绘制直线
        /// <summary>
        /// 绘制直线
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                graphics.DrawLine(pen, startPoint, endPoint); //两个坐标连成直线
            }
            if (this.StraightLineChecked)
            {
                using (Pen pen = new Pen(Color.DodgerBlue))
                {
                    graphics.FillRectangle(Brushes.DodgerBlue, new Rectangle(startPoint.X - 2, startPoint.Y - 1, size + 2, size + 1));
                    graphics.FillRectangle(Brushes.DodgerBlue, new Rectangle(endPoint.X - 2, endPoint.Y - 2, size + 1, size + 1));
                }
            }
        }
        #endregion

}


/// <summary>
    /// 空心三角形类
    /// </summary>
    [Serializable]
    public class Triangle : ShapeBase
    {
        #region 绘制空心三角形
        /// <summary>
        /// 绘制空心三角形
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                Point leftPoint = new Point(startPoint.X, endPoint.Y);
                Point topPoint = new Point(startPoint.X + (endPoint.X - startPoint.X) / 2, startPoint.Y);
                Point[] pointArry = { leftPoint, topPoint, endPoint };
                graphics.DrawPolygon(pen, pointArry);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }
        #endregion

}

/// <summary>
    /// 实心三角形类
    /// </summary>
    [Serializable]
    public class Trilateral : ShapeBase
    {
        #region 绘制实心三角形
        /// <summary>
        /// 绘制实心三角形
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {


                Point leftPoint = new Point(startPoint.X, endPoint.Y);
                Point topPoint = new Point(startPoint.X + (endPoint.X - startPoint.X) / 2, startPoint.Y);
                Point[] pointArry = { leftPoint, topPoint, endPoint };
                graphics.FillPolygon(new SolidBrush(color), pointArry);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics, startPoint, endPoint, color, size);
            }
        }
        #endregion

}


/// <summary>
    /// 文字类
    /// </summary>
    [Serializable]
    public class Word : ShapeBase
    {
        #region 字段和属性
        private string text;
        /// <summary>
        /// 文本内容
        /// </summary>
        public string Text
        {
            get { return text; }
            set { text = value; }
        }
        private int wordSize = 8;
        /// <summary>
        /// 字体大小
        /// 默认大小为8
        /// </summary>
        public int WordSize
        {
            get { return wordSize; }
            set { wordSize = value; }
        } 
        #endregion


        #region 绘制文字
        /// <summary>
        /// 绘制文字
        /// </summary>
        /// <param name="graphics">画板</param>
        /// <param name="startPoint">起始点</param>
        /// <param name="endPoint">结束点</param>
        /// <param name="color">颜色</param>
        /// <param name="size">粗细大小</param>
        public override void Draw(Graphics graphics, Point startPoint, Point endPoint, Color color, int size)
        {
            using (Pen pen = new Pen(color, size))
            {
                graphics.DrawString(text, new Font("SimSun", wordSize), new SolidBrush(color), startPoint.X, startPoint.Y);
            }
            if (this.StraightLineChecked)
            {
                DrawChoose.drawChoose(graphics,startPoint,endPoint,color,size);
            }
        }
        #endregion


    }

  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值