基本信息
源码名称:C# 实现毛笔画笔
源码大小:0.06M
文件格式:.rar
开发语言:C#
更新时间:2019-04-03
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 10 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Drawing2D;

namespace Frm_Index
{
    public partial class Form1 : Form
    {

        private drawFrom df;
        private Graphics _graphics;
        private bool _isPaintting;
        private Point _point;
        private Image _pen;

        private readonly float _maxPenWidth = 12;
        private readonly float _minPenWidth = 3;
        public Form1()
        {
            InitializeComponent();
            WindowState = FormWindowState.Maximized;//本窗体最大化
            //TransparencyKey = BackColor;//背景透明(鼠标穿透)
            DoubleBuffered = true;//双缓存处理

            df = new drawFrom();//不穿透鼠标透明窗体
            _graphics = CreateGraphics();
            _graphics.SmoothingMode = SmoothingMode.AntiAlias;
            string imagePath = Path.Combine(Application.StartupPath, "pen.png");
            _pen = new Bitmap(imagePath);           
          
            df.MouseDown  = Form1_MouseDown;
            df.MouseUp  = Form1_MouseUp;
            df.MouseMove  = Form1_MouseMove;
        }

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            //if (_isPaintting)
            //{
            //if (!_point.IsEmpty)
            //{
            //double distance = Math.Pow(Math.Pow(e.Location.X - _point.X, 2)   Math.Pow(e.Location.Y - _point.Y, 2), 0.5);
            //int width = (int)this.CalculatePenWidth(distance);

            //_pen = new Bitmap(_pen, new Size(width, width));

            //Point penLocation = new Point(e.Location.X, e.Location.Y);
            //_graphics.DrawImage(_pen, penLocation);
            //}
            if (e.Button == MouseButtons.Left)
            {
                _isPaintting = true;//开始
                _point = e.Location;
            }
           
            //}
        }
        // 这个方法用来计算画笔的宽度,试了几个方法,效果都不理想
        private double CalculatePenWidth(double distance)
        {
            if (distance < _maxPenWidth)
                return _maxPenWidth;
            distance = _maxPenWidth - 1;
            double result = (_maxPenWidth - _minPenWidth) * ((_maxPenWidth - distance) / _maxPenWidth);
            if (result < 1)
                return _maxPenWidth;
            return result;
        }
        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            _isPaintting = false;//结束画图
            //_point = Point.Empty;
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (_isPaintting)
            {
                double distance = Math.Pow(Math.Pow(e.Location.X - _point.X, 2)   Math.Pow(e.Location.Y - _point.Y, 2), 0.5);
                int width = (int)this.CalculatePenWidth(distance);
                _pen = new Bitmap(_pen, new Size(width, width));

                Point penLocation = new Point(e.Location.X, e.Location.Y);
                _graphics.DrawImage(_pen, penLocation);
                _point = e.Location;
            }
        }
    }
}