C#实现的基于windows的画图功能

    public partial class DrawingUnit : Form
    {
        private string imgSaveUrl;

        private static MouseStatic mouseStatic = MouseStatic.defaultStatic;  //标题栏
        private int Thickness = 10; //线条,边框粗细
        private Point[] trianglePoints; //正在编辑中的三角形点位
        private Color currentColor = Color.Red; // 当前选择的颜色
        private Panel colorPalette; // 颜色调色板Panel
        private Label currentColorLabel; // 显示当前颜色的Label
        private Label currentColorLabe2;

        private Panel canvas; // 画布
        private Graphics canvasGraphics; // 用于绘制图形
        private Point dragStartPoint;

        //新增图形的缓冲区
        private CurrentGraphicsType currentGraphicsType = CurrentGraphicsType.NullType;//默认当前无编辑图形
        private Bitmap bufferBitmap; // 缓冲区Bitmap
        private Graphics bufferGraphics; // 缓冲区Graphics

        //虚线
        private Rectangle resizeRect; // 虚线框的范围
        private bool isResizing = false; // 是否编辑中
        private bool isEraser = false;
        private bool isPen = false;

        //当前的canvas备份,不计入缓冲区
        private bool hasSavedBackground = false;
        private Bitmap backgroundBitmap;
        private Graphics backgroundGraphics;

        //移动拖拽正方形的位置信息
        private Rectangle[] squarePositions = new Rectangle[8];

        public DrawingUnit(string imgSaveUrl)
        {
            this.imgSaveUrl = imgSaveUrl;
            //System.Diagnostics.Debug.WriteLine(11);
            this.Size = new Size(306, 321);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.FormBorderStyle = FormBorderStyle.FixedSingle;
            this.MaximizeBox = false; // 不允许最大化
            this.Text = "标绘";
            //导航栏初始化
            InitializeComponent();
            //工具栏初始化
            InitToolbar();
            //画布初始化
            InitCover();
        }
        private void InitCover()
        {
            // 创建画布
            canvas = new Panel();
            canvas.Location = new Point(0, 65);
            canvas.Size = new Size(256, 256);
            canvas.BackColor = Color.White;
            canvas.MouseMove += Canvas_MouseMove;
            canvas.MouseDown += Canvas_MouseDown;
            canvas.MouseUp += Canvas_MouseUp;

            // 获取Graphics对象
            canvasGraphics = canvas.CreateGraphics();

            //创建图形缓冲区
            bufferBitmap = new Bitmap(canvas.Width, canvas.Height, PixelFormat.Format32bppArgb);
            bufferGraphics = Graphics.FromImage(bufferBitmap);
            // 设置Graphics对象的背景色为透明
            bufferGraphics.Clear(Color.Transparent);


            backgroundBitmap = new Bitmap(canvas.Width, canvas.Height, PixelFormat.Format32bppArgb);
            backgroundGraphics = Graphics.FromImage(backgroundBitmap);
            backgroundGraphics.Clear(Color.Transparent);
            //将画布添加到窗体
            this.Controls.Add(canvas);
        }
        private void InitializeComponent()
        {
            // 创建导航栏组
            MenuStrip menuStrip = new MenuStrip();
            menuStrip.Text = "创建";
            menuStrip.Location = new Point(0, 0);
            //创建导航栏一级标题
            ToolStripMenuItem btnSave = new ToolStripMenuItem("保存");
            ToolStripMenuItem import = new ToolStripMenuItem("导入");
            ToolStripMenuItem create = new ToolStripMenuItem("创建");

            btnSave.Click += SaveImg;
            import.Click += ImportImg;

            //创建导航栏二级标题
            ToolStripMenuItem createSolidCircle = new ToolStripMenuItem("实心圆");
            ToolStripMenuItem createHollowCircle = new ToolStripMenuItem("空心圆");
            ToolStripMenuItem createSolidSqual = new ToolStripMenuItem("实心方形");
            ToolStripMenuItem createHollowSqual = new ToolStripMenuItem("空心方形");
            ToolStripMenuItem createFillTriangle = new ToolStripMenuItem("实心三角形");
            ToolStripMenuItem createHollowTriangle = new ToolStripMenuItem("空心三角形");

            createSolidCircle.Click += CreateGraphics;
            createHollowCircle.Click += CreateGraphics;
            createSolidSqual.Click += CreateGraphics;
            createHollowSqual.Click += CreateGraphics;
            createFillTriangle.Click += CreateGraphics;
            createHollowTriangle.Click += CreateGraphics;

            create.DropDownItems.AddRange(new ToolStripItem[] { createSolidCircle, createHollowCircle, createSolidSqual, createHollowSqual, createFillTriangle, createHollowTriangle });
            menuStrip.Items.AddRange(new ToolStripItem[] { btnSave, import, create });
            this.Controls.Add(menuStrip);
        }

        private void ImportImg(object? sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Filter = "Image Files|*.jpg;*.jpeg;*.png;*.bmp;*.gif;*.tiff";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    // 加载用户选择的图片
                    string imagePath = openFileDialog.FileName;
                    using (Image importedImage = Image.FromFile(imagePath))
                    {
                        // 创建一个新的 Bitmap 并绘制导入的图片
                        backgroundBitmap = new Bitmap(importedImage);
                        hasSavedBackground = true;
                        canvasGraphics.DrawImage(backgroundBitmap, Point.Empty);
                    }
                }
            }
        }

        private void InitToolbar()
        {
            //创建工具栏组
            Panel topPanel = new Panel();
            topPanel.Location = new Point(0, 25);
            topPanel.Height = 40;
            topPanel.Width = 500;
            topPanel.BackColor = Color.Gray;


            Label label = new Label
            {
                Text = "粗细",
                AutoSize = true,
                Location = new Point(10, 10) // 设置标签的位置
            };
            topPanel.Controls.Add(label);
            ComboBox comboBox = new ComboBox
            {
                Location = new Point(40, 8), // 设置下拉框的位置
                Width = 40,
            };
            comboBox.Items.AddRange(new object[] { 2, 4, 6, 8, 10, 12 });
            comboBox.SelectedIndex = 4;
            comboBox.SelectedIndexChanged += changeThickness;

            topPanel.Controls.Add(comboBox);



            // 创建显示当前颜色的Label
            currentColorLabel = new Label();
            currentColorLabel.Text = "当前颜色: ";
            currentColorLabel.ForeColor = Color.Black;
            currentColorLabel.Location = new Point(80, 12);
            currentColorLabel.Size = new Size(60, 30);
            topPanel.Controls.Add(currentColorLabel);

            // 创建显示当前颜色的Label
            currentColorLabe2 = new Label();
            currentColorLabe2.Location = new Point(140, 12);
            currentColorLabe2.Size = new Size(16, 16);
            currentColorLabe2.BackColor = currentColor;
            topPanel.Controls.Add(currentColorLabe2);

            // 创建“颜料卡”Panel
            colorPalette = new Panel();
            colorPalette.Location
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值