I am using the code below to change to shape of the winform.
It's changing the shape, but not like how I wanted.
I need the form to have curved corners.
What points should I use to get it?
public void MakeNonRectangularForm()
{
var p = new GraphicsPath();
int width = ClientSize.Width;
int height = ClientSize.Height;
p.AddClosedCurve(new Point[] { new Point(width / 2, height / 2),
new Point(width, 0), new Point(width, height / 3),
new Point(width - width / 3, height),
new Point(width / 7, height - height / 8)});
Region = new Region(p);
}
The following is some code I have used to create rounded edges before, using AddArc and lines to piece together the border:
(You can play with xRadius and yRadius to achieve your desired amount of "rounded-ness")
int xRadius = {insert value here};
int yRadius = {insert value here};
GraphicsPath edge = new GraphicsPath();
int rightHandLeft = this.Width - xRadius - 1;
int bottomSideTop = this.Height - yRadius - 1;
edge.AddArc(0, 0, xRadius, yRadius, 180, 90);
edge.AddLine(xRadius, 0, rightHandLeft, 0);
edge.AddArc(rightHandLeft, 0, xRadius, yRadius, 270, 90);
edge.AddLine(this.Width, yRadius, this.Width, bottomSideTop);
edge.AddArc(rightHandLeft, bottomSideTop, xRadius, yRadius, 0, 90);
edge.AddLine(rightHandLeft, this.Height, xRadius, this.Height);
edge.AddArc(0, bottomSideTop, xRadius, yRadius, 90, 90);
edge.AddLine(0, bottomSideTop, 0, yRadius);
this.Region = new Region(edge);
Related
I have this little code to use AddArc() method in a label, but when I execute the code the label disappears. I believe it is the numbers I have used, I followed instructions from the Windows documentation and it had these parameters there too.
GraphicsPath gp = new GraphicsPath();
Rectangle rec = new Rectangle(20, 20, 50, 100);
gp.AddArc(rec, 0 , 180);
label2.Region = new Region(gp);
label2.Invalidate();
I used another code to make the correct way or curve in a text
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var center = new Point(Width / 2, Height / 2);
var radius = Math.Min(Width, Height) / 3;
var text = "Hello";//txtUp.Text;
var font = new Font(FontFamily.GenericSansSerif, 24, FontStyle.Bold);
for (var i = 0; i < text.Length; ++i)
{
var c = new String(text[i], 1);
var size = e.Graphics.MeasureString(c, font);
var charRadius = radius + size.Height;
var angle = (((float)i / text.Length) - 2);
var x = (int)(center.X + Math.Cos(angle) * charRadius);
var y = (int)(center.Y + Math.Sin(angle) * charRadius);
e.Graphics.TranslateTransform(x, y);
e.Graphics.RotateTransform((float)(90 + 360 * angle / (2 * Math.PI)));
e.Graphics.DrawString(c, font, Brushes.Red, 0, 0);
e.Graphics.ResetTransform();
e.Graphics.DrawArc(new Pen(Brushes.Transparent, 2.0f), center.X - radius, center.Y - radius, radius * 2, radius * 2, 0, 360);
}
}
but it wont show in front of a panel is it possible.
This is what it looks like:
Is it possible to move that text in front of the green circle?
I have UserControl of Size 300*200.
and rectangle of size 300*200.
graphics.DrawRectangle(Pens.Black, 0, 0, 300, 200);
When I rotate rectangle in userControl by 30 degree, I get rotated rectangle but it is outsized.
PointF center = new PointF(150,100);
graphics.FillRectangle(Brushes.Black, center.X, center.Y, 2, 2); // draw center point.
using (Matrix matrix = new Matrix())
{
matrix.RotateAt(30, center);
graphics.Transform = matrix;
graphics.DrawRectangle(Pens.Black, 0, 0, 300, 200);
graphics.ResetTransform();
}
I want to fit rectangle like actual result.Check Image here
Can anyone have solution about this.
Thanks.
It's more of a math question than programming one.
Calculate bouning box of any rectangle rotated by any angle in radians.
var newWidth= Math.Abs(height*Math.Sin(angle)) + Math.Abs(width*Math.Cos(angle))
var newHeight= Math.Abs(width*Math.Sin(angle)) + Math.Abs(height*Math.Cos(angle))
Calculate scale for x and y:
scaleX = width/newWidth;
scaleY = height/newHeight;
Apply it to your rectangle.
EDIT:
Applied to your example:
PointF center = new PointF(150, 100);
graphics.FillRectangle(Brushes.Black, center.X, center.Y, 2, 2); // draw center point.
var height = 200;
var width = 300;
var angle = 30;
var radians = angle * Math.PI / 180;
var boundingWidth = Math.Abs(height * Math.Sin(radians)) + Math.Abs(width * Math.Cos(radians));
var boundingHeight = Math.Abs(width * Math.Sin(radians)) + Math.Abs(height * Math.Cos(radians));
var scaleX = (float)(width / boundingWidth);
var scaleY = (float)(height / boundingHeight);
using (Matrix matrix = new Matrix())
{
matrix.Scale(scaleX, scaleY, MatrixOrder.Append);
matrix.Translate(((float)boundingWidth - width) / 2, ((float)boundingHeight - height) / 2);
matrix.RotateAt(angle, center);
graphics.Transform = matrix;
graphics.DrawRectangle(Pens.Black, 0, 0, width, height);
graphics.ResetTransform();
}
I want to create a custom combo-box with rounded corners and gradient color. I have implemented the same feature in Button by overriding the OnPaint method. But it is not working for ComboBox. Any help will be appreciated.
The code I am using for overriding OnPaint is given below:
protected override void OnPaint(PaintEventArgs paintEvent)
{
Graphics graphics = paintEvent.Graphics;
SolidBrush backgroundBrush = new SolidBrush(this.BackColor);
graphics.FillRectangle(backgroundBrush, ClientRectangle);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
Rectangle rectangle = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 1);
GraphicsPath graphicsPath = RoundedRectangle(rectangle, cornerRadius, 0);
Brush brush = new LinearGradientBrush(rectangle, gradientTop, gradientBottom, LinearGradientMode.Horizontal);
graphics.FillPath(brush, graphicsPath);
rectangle = new Rectangle(ClientRectangle.X, ClientRectangle.Y, ClientRectangle.Width - 1, ClientRectangle.Height - 100);
graphicsPath = RoundedRectangle(rectangle, cornerRadius, 2);
brush = new LinearGradientBrush(rectangle, gradientTop, gradientBottom, LinearGradientMode.Horizontal);
graphics.FillPath(brush, graphicsPath);
}
private GraphicsPath RoundedRectangle(Rectangle rectangle, int cornerRadius, int margin)
{
GraphicsPath roundedRectangle = new GraphicsPath();
roundedRectangle.AddArc(rectangle.X + margin, rectangle.Y + margin, cornerRadius * 2, cornerRadius * 2, 180, 90);
roundedRectangle.AddArc(rectangle.X + rectangle.Width - margin - cornerRadius * 2, rectangle.Y + margin, cornerRadius * 2, cornerRadius * 2, 270, 90);
roundedRectangle.AddArc(rectangle.X + rectangle.Width - margin - cornerRadius * 2, rectangle.Y + rectangle.Height - margin - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 0, 90);
roundedRectangle.AddArc(rectangle.X + margin, rectangle.Y + rectangle.Height - margin - cornerRadius * 2, cornerRadius * 2, cornerRadius * 2, 90, 90);
roundedRectangle.CloseFigure();
return roundedRectangle;
}
I was able to find a solution to this problem. Actually, the problem was that OnPaint event was not getting called for Combobox. So I had to do the following changes.
public CustomComboBox()
{
InitializeComponent();
SetStyle(ControlStyles.UserPaint, true);
}
I had to call SetStyle() method inside the constructor so that OnPaint() event will get called.
I found this post as helful : OnPaint override is never called
The same solution can be used for Custom TextBoxes.
Ive got a panel and im drawing a heart on that panel..
But i dont want to draw the heart i want to draw everything except the heart so the heart is transparent.
Can i invert the Region selected out of the Path?
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
this.Region = new Region(path);
this.BackColor = Color.Black;
What it looks like(white = transparent):
What i want it to look like(white = transparent):
I think you can just add 2 graphics paths together.
You could try this code out:
private void panel1_Paint(object sender, PaintEventArgs e)
{
GraphicsPath path = new GraphicsPath();
path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
GraphicsPath path2 = new GraphicsPath();
path2.AddRectangle(new Rectangle(new Point(0, 0), panel1.Size));
path2.AddPath(path, false);
e.Graphics.FillPath(Brushes.Black, path2);
}
Result is:
You can try excluding a region from from the Graphic object of your panel's Paint event:
GraphicsPath path = new GraphicsPath();
path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
using (Region r = new Region(path)) {
e.Graphics.ExcludeClip(r);
}
// continue drawing...
e.Graphics.Clear(Color.Yellow);
or if trying to modify the control's region, then just use the Region's Exclude property:
GraphicsPath path = new GraphicsPath();
path.AddArc(0, 0, (this.Width / 2), (this.Height / 2), 135, 195);
path.AddArc((this.Width / 2), 0, (this.Width / 2), (this.Height / 2), 210, 195);
path.AddLine((this.Width / 2), this.Height, (this.Width / 2), this.Height);
Region r = new Region(new Rectangle(Point.Empty, this.ClientSize));
r.Exclude(path);
this.Region = r;
I don't know of a way to invert the selection, but you can always draw a heart in the background color, then change the background color to black.
this.BackColor = Form.BackColor;
Form.BackColor = Color.Black;
Original solution here. I've modified that for your needs
this.Region = InvertRegion(new Region(path), this.Width, this.Height);
and
private Region InvertRegion(Region region, int width, int height)
{
Bitmap mask = new Bitmap(width, height);
Graphics.FromImage(mask).FillRegion(Brushes.Black, region);
int matchColor = Color.Black.ToArgb();
Region inverted = new System.Drawing.Region();
inverted.MakeEmpty();
Rectangle rc = new Rectangle(0, 0, 0, 0);
bool inimage = false;
for (int y = 0; y < mask.Height; y++)
{
for (int x = 0; x < mask.Width; x++)
{
if (!inimage)
{
if (mask.GetPixel(x, y).ToArgb() != matchColor)
{
inimage = true;
rc.X = x;
rc.Y = y;
rc.Height = 1;
}
}
else
{
if (mask.GetPixel(x, y).ToArgb() == matchColor)
{
inimage = false;
rc.Width = x - rc.X;
inverted.Union(rc);
}
}
}
if (inimage)
{
inimage = false;
rc.Width = mask.Width - rc.X;
inverted.Union(rc);
}
}
return inverted;
}
hello how to fill lineargradientbrush on the graphicspath..
i have this code and solidbrush can only fill it.. i dont know how to fill it with lineargradientbrush.. any help please..
i have this code..
class KamoteButton : Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
int width = this.Width-1;
int height = this.Height-1;
Color gradColor_a = Color.FromArgb(162, 177, 183);
Color gradColor_b = Color.FromArgb(104, 111, 114);
int radius = this.Width / 8;
Graphics gFx = pevent.Graphics;
gFx.SmoothingMode = SmoothingMode.AntiAlias;
GraphicsPath gp = new GraphicsPath();
gp.AddArc(0, 0, radius, radius, 180, 90);
gp.AddLine(width / 8, 0, width - width / 8, 0);
gp.AddArc(width - radius, 0, radius, radius, 270, 90);
gp.AddLine(width, width / 8, width, height - width / 8);
gp.AddArc(width - radius, height - radius, radius, radius, 0, 90);
gp.AddLine(width - width / 8, height, width / 8, height);
gp.AddArc(0, height - radius, radius, radius, 90, 90);
gp.AddLine(0, height - width / 8, 0, width / 8);
Rectangle r = new Rectangle(0, 0, 100, 100);
LinearGradientBrush lbg = new LinearGradientBrush(gp, gradColor_a, gradColor_b, LinearGradientMode.Vertical);
SolidBrush sb = new SolidBrush(Color.Red);
gFx.FillPath(lbg, gp);
this.Region = new Region(gp);
}
}
Your code to create the GradientBrushh won't compile:
LinearGradientBrush lbg = new LinearGradientBrush(gp,
gradColor_a, gradColor_b, LinearGradientMode.Vertical);
There is no constructor with a Path as parameter. Replace it with 2 points or 1 Rect