c#制作我的画笔别名和光滑

时间:2014-06-03 13:33:48

标签: c# gdi+ paint

如何调整代码以使画笔使用锯齿更平滑?我不确定如何调整或改变什么。我知道有一个SmoothGraphics属性,但我不确定它放在哪里或如何使用它?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Polisher
{
    public partial class Form1 : Form
    {
        bool paint = false;
        SolidBrush color;

        public Form1()
        {
            InitializeComponent();
            //this.BackColor = Color.White;
            //this.TransparencyKey = Color.White;
            //this.TransparencyKey = Color.Empty;
        }

        private void uiOpacitySlider_Scroll(object sender, EventArgs e)
        {
            var opacityVal = (uiOpacitySlider.Value) / 100.0;

            this.Opacity = opacityVal;
            uiOpacity.Text = (uiOpacitySlider.Value).ToString() + "%";
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            Graphics g1 = uiCanvas.CreateGraphics();
            g1.Clear(uiCanvas.BackColor);
        }

        private void uiCanvas_MouseUp(object sender, MouseEventArgs e)
        {
            paint = false;
        }

        private void uiCanvas_MouseDown(object sender, MouseEventArgs e)
        {
            paint = true;
        }

        private void uiCanvas_MouseMove(object sender, MouseEventArgs e)
        {
            if (paint)
            {

                color = new SolidBrush(Color.Black);
                var size = (int)numBrushSize.Value;

                Graphics g = uiCanvas.CreateGraphics();

                // center paint brush onto cursor
                var xPos = (float)(e.X - ( (float)size / 2.0) );
                var yPos = (float)(e.Y - ( (float)size / 2.0) );

                g.FillEllipse(color, xPos, yPos, size, size);
                // e is the mouse, in this case e.x is the current X position of the mouse.
                g.Dispose();
            }
        }


    }
}

1 个答案:

答案 0 :(得分:3)

首先,不要在MouseMove中进行绘图。在Paint中执行此操作。在鼠标事件中调用Invalidate()以强制重绘。跟踪变量中的数据,以便了解绘制的内容和方式。

其次,如果可以提供帮助,请不要使用CreateGraphics。它是易变的,而不是在表单中呈现的正确方法。

关于质量,您可以执行以下操作:

e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;

当然,还有其他属性,具体取决于您想要调整的内容。