Hello All,
I am working on GDI+ programming and created a windows form to draw lines by mouse.
Here is my sample code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace SampleApplication
{
public partial class frmTest : Form
{
SolidBrush brush;
bool isMouseDown = false;
Pen pen;
Point startPos;
Point curPos;
public frmTest()
{
InitializeComponent();
brush = new SolidBrush(Color.FromArgb(100, Color.Black));
pen = new Pen(brush, 20); // A big size semi transparent pen
}
private void frmTest_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
startPos.X = e.X;
startPos.Y = e.Y;
}
private void frmTest_MouseMove(object sender, MouseEventArgs e)
{
if (isMouseDown)
{
curPos.X = e.X;
curPos.Y = e.Y;
Graphics g = this.CreateGraphics();
g.SmoothingMode = SmoothingMode.AntiAlias;
g.PixelOffsetMode = PixelOffsetMode.HighQuality;
//pen.StartCap = LineCap.Round; I already tried this with bad effect. Ugly caps displaying in line when you move mouse up and down.
//pen.EndCap = LineCap.Round;
g.DrawLine(pen, startPos, curPos);
g.Dispose();
startPos = curPos;
}
}
private void frmTest_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
}
}
When I try to draw line and move mouse up and down, it draws with bad effects and leaves some area undrawn and zigzagged line appears. I want to draw line exactly like Ms Paint Marker brush.
I already tried StartCap and EndCap properties of pen with no desired result. It works fine only if color is not transparent and completely fails with transparent color. Any help appreciated.
Regards,
Jayant