c#设置我的画笔别名和平滑
本文关键字:平滑 别名 画笔 设置 我的 | 更新日期: 2023-09-27 18:06:19
我如何调整我的代码,使画笔画平滑与混叠?我不知道该怎么调整,该改变什么。我知道有一个平滑图形属性,但我不确定在哪里放置它或如何使用它?
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();
}
}
}
}
首先,不要在MouseMove
中画画。在Paint
中做。在鼠标事件中调用Invalidate()
来强制重画。跟踪变量中的数据,以便您知道如何绘制。
第二,如果你能帮助它,不要使用CreateGraphics
。它是不稳定的,不是在表单中渲染的正确方式。
关于质量,你可以这样做:
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
当然,还有其他可用的属性,这取决于您想要调整的内容。