Filling a specific region of a shape containing another shape - c#

In Winforms application, i am doing 2D drawing.
for example, when i try fill a rectangle, that contains a circle. i want only region outside of circle should be filled with the specified color.
i tried but entire rectangle is getting filled.

Just try this to get the desired output. Open a windows forms and add a button. In the button click event, just add this code:
Region rgn = new Region(new Rectangle(50, 50, 200, 150));
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddEllipse(60, 60, 100, 100);
rgn.Exclude(path);
Graphics g = this.CreateGraphics();
g.FillRegion(Brushes.Blue, rgn);
"rgn.Exclude(Path)" will help you to paint the rectangle excluding the circle inside it.

Related

Highlight another application

i'm trying to put a red border around another application. I am trying to by using WinApi :
User32Methods.GetWindowRect(windowHandle, out var rectangle);
var g = System.Drawing.Graphics.FromHdc(User32Methods.GetDesktopWindow());
using (g)
{
//drawing borders
g.TranslateTransform(rectangle.Left, rectangle.Top);
g.DrawRectangle(new System.Drawing.Pen(System.Drawing.Color.Red, 10),
new System.Drawing.Rectangle(0, 0, rectangle.Width, rectangle.Height));
}
But if i put negative X/Y because the app is on left screen it didn't draw anything. I've try many methods but i'm blocked.
Thanks

Pen.Alignment is not working while drawing line

I want to draw a thick line and a thin line. The thick line should be positioned inside of the theoretical line. So I set the pen alignment as below.
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1);
Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10);
greenPen.Alignment = PenAlignment.Inset;
e.Graphics.DrawLine(greenPen, 10, 100, 100, 50);
e.Graphics.DrawLine(blackPen, 10, 100, 100, 50);
But the idea is not get worked. Any better idea to do the same ???
Pen.Alignment is partially implemented:
This property determines how the Pen draws closed curves and polygons. The PenAlignment enumeration specifies five values; however, only two values—Center and Inset—will change the appearance of a drawn line. Center is the default value for this property and specifies that the width of the pen is centered on the outline of the curve or polygon. A value of Inset for this property specifies that the width of the pen is inside the outline of the curve or polygon. The other three values, Right, Left, and Outset, will result in a pen that is centered.
So, Inset will work only on polygons or curves (e.g. Graphics.DrawRectangle).

How to draw moving text with GDI+

The task is to draw a moving text on a black background (for example, a moving number) with .NET GDI+. Some other elements may present on this background, so it isn't possible to fill all the area with black and then draw a string in a new position.
My current code is as follows:
Graphics g = this.CreateGraphics();
// drawing a string
Font myFont = new Font("Fixedsys", 10);
g.DrawString("1", myFont, Brushes.Gray, 100, 100);
// erasing a string
g.DrawString("1", myFont, Brushes.Black, 100, 100);
// then we repeat the code above with a new position for a string
The problem is that the text isn't erased entirely by the second DrawString with black brush. Small border remains visible. Please help, how to remove this trace, and draw moving text correctly.
The problem is solved by adding the following:
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel;

draw polygon click area

Drawing a polygon according to the input coordinates
i got some code from here, i just take..
void pictureBox1_Paint(object sender, PaintEventArgs e) {
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
List<Point> polyPoints = new List<Point>();
polyPoints.Add(new Point(30, 30));
polyPoints.Add(new Point(36, 105));
polyPoints.Add(new Point(66, 105));
polyPoints.Add(new Point(72, 66));
using (SolidBrush br = new SolidBrush(Color.FromArgb(100, Color.Yellow)))
{
e.Graphics.FillPolygon(br, polyPoints.ToArray());
}
e.Graphics.DrawPolygon(Pens.DarkBlue, polyPoints.ToArray());
}
note : SmoothingMode use header using System.Drawing.Drawing2D
then i got problem about click area, i just want the click area at the visible area, in this case the picturebox1 have size 1366 x 768
this is example of picturebox, i want the red area be clickable and the gray is not clickable area
by default all area in the box is clickable
Have you looked at the Documentation on PictureBox?
I'm looking at it and it seems there's many ways of resizing aspects of the PixtureBox object. Take a look at using the DefaultSize Property or setting the Size property. In either case, you have to wrap the size in a Size object and set the according PictureBox size property.
Such as:
pictureBox1.Size = new Size(xSize, ySize);
or
pictureBox1.DefaultSize = new Size(xSize, ySize);

Draw a line on a TabPage in C#

I am having trouble drawing a line on a TabPage.
I actually have a TabControl inside a TabControl. I have drawn a number of labels which I am using as boxes. I want to draw lines to join them together.
I have tried:
Pen P = new Pen(System.Drawing.Color.Black, 10);
tabname.CreateGraphics().DrawLine(P, 10, 10, 100, 100);
and
Pen P = new Pen(System.Drawing.Color.Black, 10);
tabcontrolname.TabPages[0].CreateGraphics().DrawLine(P, 10, 10, 100, 100);
Neither are displaying the line. I assume the line is being placed somewhere as there are no errors.
Any ideas how I can get it to display on the correct TabPage?
Thank you!
You probably need to override the OnPaint method (or handle the Paint event) to get this to work properly. If you don't your controls will just end up drawing over your lines.
Here's a link to the relevant docs.
Where do you try these codes, in which function? If you are doing it once in the initialization or construction, they will not be displayed as you expect. Whenever the control needs to be redrawn, you need to draw this line too, again. Either override the OnPaint method of the control or register for the Paint event and do the line drawing there.
I was able to get the arrow to show up using the following code:
TabPage.Paint += new PaintEventHandler(TabPage_Paint);
and
protected void TabPage_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
Pen arrow = new Pen(Brushes.Black, 4);
arrow.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
e.Graphics.DrawLine(arrow, 10, 10, 100, 100);
arrow.Dispose();
}
However, when scrolling is initiated the Paint messes up :(

Categories