I have problem with draw two polygons.
I want to fill two triangles, but one is greater than the second.
I am using UserControl in winforms.
Code:
Point[] DOWN = new Point[] {new Point(0, 0), new Point(10, 0), new Point(5, 5)};
Point[] UP = new Point[] { new Point(0, 15), new Point(10, 15), new Point(5, 10) };
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19));
e.Graphics.FillPolygon(brush, DOWN);
e.Graphics.FillPolygon(brush, UP);
brush.Dispose();
}
Where is problem?
Try setting the PixelOffsetMode property:
e.Graphics.PixelOffsetMode = PixelOffsetMode.Half;
using (SolidBrush brush = new SolidBrush(Color.FromArgb(253, 198, 19))) {
e.Graphics.FillPolygon(brush, DOWN);
e.Graphics.FillPolygon(brush, UP);
}
Result:
Try keeping the order counter-clockwise and start from the highest point:
new Point(5, 10), new Point(10, 15), new Point(0, 15)
Tell us if that helped. Sometimes those algorithms don't behave well on border conditions.
Related
I need to create a framed GraphicsPath that self-intersects giving a z-order feeling:
The code that I used to obtain the image is the following:
private void Example1(PaintEventArgs e) {
Brush brush = new SolidBrush(Color.FromArgb(200, Color.LightBlue));
GraphicsPath path1 = new GraphicsPath(FillMode.Winding);
path1.AddLines(new Point[] {
new Point(400, 200),
new Point(400, 300),
new Point(100, 300),
new Point(100, 400),
new Point(500, 400),
new Point(500, 100)
});
e.Graphics.FillPath(brush, path1);
e.Graphics.DrawPath(Pens.Blue, path1);
GraphicsPath path2 = new GraphicsPath(FillMode.Winding);
path2.AddLines(new Point[] {
new Point(500, 100),
new Point(200, 100),
new Point(200, 500),
new Point(300, 500),
new Point(300, 200),
new Point(400, 200)
});
e.Graphics.FillPath(brush, path2);
e.Graphics.DrawPath(Pens.Blue, path2);
}
in which I draw the two paths independently.
My need is to handle it as a unique graphic object, but if I join the paths I obtain this image:
Example code:
private void Example2(PaintEventArgs e) {
Brush brush = new SolidBrush(Color.FromArgb(200, Color.LightBlue));
GraphicsPath path1 = new GraphicsPath(FillMode.Winding);
path1.AddLines(new Point[] {
new Point(400, 200),
new Point(400, 300),
new Point(100, 300),
new Point(100, 400),
new Point(500, 400),
new Point(500, 100)
});
GraphicsPath path2 = new GraphicsPath(FillMode.Winding);
path2.AddLines(new Point[] {
new Point(500, 100),
new Point(200, 100),
new Point(200, 500),
new Point(300, 500),
new Point(300, 200),
new Point(400, 200)
});
path1.AddPath(path2, true);
e.Graphics.FillPath(brush, path1);
e.Graphics.DrawPath(Pens.Blue, path1);
}
Same problem if I use StartFigure/CloseFigure. Maybe I can solve the problem using the SetMarkers method in conjunction with the GraphicsPathIterator, but it seems overwhelming.
The simplest way that I found is to use the GraphicsPathIterator. In this way I can store more figures on a single path and have the flexibility I need during the painting. The only drawback is that the paint method has to be modified accordingly.
Here is an example in which I define the path and that do also the painting:
private void Example4(PaintEventArgs e) {
Brush brush = new SolidBrush(Color.FromArgb(200, Color.LightBlue));
GraphicsPath path = new GraphicsPath(FillMode.Winding);
path.StartFigure();
path.AddLines(new Point[] {
new Point(400, 200),
new Point(400, 300),
new Point(100, 300),
new Point(100, 400),
new Point(500, 400),
new Point(500, 100)
});
path.StartFigure();
path.AddLines(new Point[] {
new Point(500, 100),
new Point(200, 100),
new Point(200, 500),
new Point(300, 500),
new Point(300, 200),
new Point(400, 200)
});
GraphicsPathIterator pathIterator = new GraphicsPathIterator(path);
GraphicsPath p = new GraphicsPath();
while (pathIterator.NextSubpath(p, out bool isClosed) > 0) {
e.Graphics.FillPath(brush, p);
e.Graphics.DrawPath(Pens.Blue, p);
}
}
I'm new ,and I don't understand why this method when copied to windows forms doesn't do anything when running the program. I was copy it from MSDN page.
public void DrawLinesPoint(PaintEventArgs e)
{
// Create pen.
Pen pen = new Pen(Color.Black, 3);
// Create array of points that define lines to draw.
Point[] points =
{
new Point(10, 10),
new Point(10, 100),
new Point(200, 50),
new Point(250, 300)
};
//Draw lines to screen.
e.Graphics.DrawLines(pen, points);
}
Typically, when you draw on a form, you handle the form’s Paint event and perform the drawing using the Graphics property of the PaintEventArgs
In your code you need to add the DrawLinesPoint to the paint event before being able to use it
In your Constructor() add
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.DrawLinesPoint);
And in your Paint PaintEventHandler
private void DrawLinesPoint(object sender, PaintEventArgs e)
{
Pen pen = new Pen(Color.Black, 3);
// Create array of points that define lines to draw.
Point[] points =
{
new Point(10, 10),
new Point(10, 100),
new Point(200, 50),
new Point(250, 300)
};
//Draw lines to screen.
e.Graphics.DrawLines(pen, points);
}
Is there a way to create non-rectangular pictureBoxes. I have round shapes that should overlap and if possible should be in different pictureboxes..
I tried the this here, but you could not see both pictureboxes that are overlapping at a time, but just one..
Here the picture that resulted from my tests:
You can make the PictureBoxes circular by adding an Ellipse to a GraphicsPath and then building a new Region from it.
Here's a quick example:
public class Target : PictureBox
{
public Target()
{
this.Size = new Size(100, 100);
this.Paint += Target_Paint;
Rectangle rc = this.ClientRectangle;
rc.Inflate(-10, -10);
System.Drawing.Drawing2D.GraphicsPath gp = new System.Drawing.Drawing2D.GraphicsPath();
gp.AddEllipse(rc);
this.Region = new Region(gp);
}
void Target_Paint(object sender, PaintEventArgs e)
{
Rectangle rc = this.ClientRectangle;
rc.Inflate(-10, -10);
using (Pen pen = new Pen(Color.Blue, 5))
{
e.Graphics.DrawEllipse(pen, rc);
}
rc = new Rectangle(new Point(this.Size.Width / 2, this.Size.Height / 2), new Size(1, 1));
rc.Inflate(9, 9);
e.Graphics.FillEllipse(Brushes.Red, rc);
}
}
After compiling, the new control appears at the top of your ToolBox.
Here's a screenshot with three of them overlapping each other:
I draw Polygon with this code:
Graphics surface;
surface = this.CreateGraphics();
SolidBrush brush = new SolidBrush(Color.Olive);
Point[] points = { new Point(50, 50), new Point(250, 50), new Point(50, 250) };
surface.FillPolygon(brush, points);
how to resize polygon Similar to the following?
Pic
Try this:
var g = e.Graphics;
var points=new PointF[] { new PointF(0, 0), new PointF(1, 0), new PointF(0, 1) };
var st=g.SaveState();
g.TranslateTransform(100f, 100f);
g.ScaleTransform(40f, 40f);
g.FillPolygon(Brushes.Olive, points);
g.Transform=mx;
g.TranslateTransform(300f, 100f);
g.ScaleTransform(80f, 80f);
g.FillPolygon(Brushes.MediumOrchid, points);
g.Restore(st);
which draws to polygons of the same shape on different locations with different sizes.
(red annotations added by me)
You have a couple options. A simple, rather silly solution would be to use linq:
double resizeValue = 1.5;
points.Select(x => new Point(x.X*resizeValue, x.Y*resizeValue);
That way is simple to understand, I think. May be better ways, but if this is all you're doing, it's probably sufficient.
I'd like to know how to paint User Control with background Transparency even if the user control is raised or moved at runtime.
my code is
private void UserControl1_Paint(object sender, PaintEventArgs e)
{
var g = e.Graphics;
g.Clear(Color.White);
g.SmoothingMode = SmoothingMode.HighQuality;//可以反锯齿
var rectBound = new Rectangle(0, 0, Width-1, Height-1);
var b = new SolidBrush(Color.FromArgb(0, 122, 204));
var rect = new Rectangle(2, 2, Width - 4, Height - 4);
if(!_isSelected)//FillRectangle
g.FillEllipse(b, rectBound);
else
g.FillEllipse(b, rect);
var pen = new Pen(Color.Yellow);
pen.DashStyle = DashStyle.DashDot;
g.DrawLine(pen,10,10,100,10);
pen.DashStyle = DashStyle.Dash;
g.DrawLine(pen, 10, 15, 100, 15);
pen.DashStyle = DashStyle.DashDotDot;
g.DrawLine(pen, 10, 20, 100, 20);
pen.DashStyle = DashStyle.Dot;
g.DrawLine(pen, 10, 25, 100, 25);
pen.DashStyle = DashStyle.Solid;
g.DrawLine(pen, 10, 30, 100, 30);
if (_isSelected)
{
pen = new Pen(Color.Black) { DashStyle = DashStyle.Dot, Width = 1 };
g.DrawRectangle(pen, rectBound);
}
}
and demo picture here
there will be a lot of User Controls Created at runtime,some of them maybe overlapped,then it should be clear the user control's background.how to do that?