FillEllipse Error - c#

I get a JIT compiling error running this code
void draw(PaintEventArgs e)
{
Graphics gr =this.CreateGraphics();
Pen pen = new Pen(Color.Black, 5);
int x = 50;
int y = 50;
int width = 100;
int height = 100;
gr.DrawEllipse(pen, x, y, width, height);
gr.Dispose();
SolidBrush brush = new SolidBrush(Color.White);
gr.FillEllipse(brush, x,y,width,height);
}
Error says: System Argument Exception: Invalid argument in
FillEllipse(Brush, int32 x,int32 y,int32 width,int 32 height);

Since you are passing in the PaintEventArgs e you can and should use its e.Graphics!
And since you didn't create it, don't dispose of it!
But those Pens and Brushes you create you should disposed of or better yet, create them inside a using clause! For the SolidBrush we can use the standard Brush, which we can't change and must not dispose of either!
To be sure the Fill won't overwrite the Draw I have switched the order.
So, try this:
void draw(PaintEventArgs e)
{
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
gr.FillEllipse(Brushes.White, x, y, width, height);
using (Pen pen = new Pen(Color.Black, 5) )
gr.DrawEllipse(pen, x, y, width, height);
}

Related

Getting two ellipses instead of one

So I have a code that needs to draw a circle, on random position, and also it needs to stay in predefined borders. In my code, I'm supposed to draw only one circle. In some cases, it draws only one but in other cases, it draws two of them and I have no idea why.
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
Random r = new Random();
int leftX = 20;
int topY = 60;
int width = this.Width - (3 * leftX);
int height = this.Height - (int)(2.5 * topY);
float x = r.Next(20, width - 100);
float y = r.Next(60, height - 100);
Rectangle rect = new Rectangle((int)x, (int)y, 100, 100);
g.DrawRectangle(new Pen(Color.Black), rect); //rectangle around ellipse
g.DrawRectangle(new Pen(Color.Black, 3), leftX, topY, width, height); //border rectangle
g.FillEllipse(new SolidBrush(Color.Black), rect);
}
The paint event gets called whenever the form is re-drawn, so from your point of view, that's unpredictable. You would be better moving the code to generate the position into the load event:
float x;
float y;
private void Form1_Load(object sender, System.EventArgs e)
{
Random r = new Random();
x = r.Next(20, width - 100);
y = r.Next(60, height - 100);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
int leftX = 20;
int topY = 60;
int width = this.Width - (3 * leftX);
int height = this.Height - (int)(2.5 * topY);
Rectangle rect = new Rectangle((int)x, (int)y, 100, 100);
g.DrawRectangle(new Pen(Color.Black), rect); //rectangle around ellipse
g.DrawRectangle(new Pen(Color.Black, 3), leftX, topY, width, height); //border rectangle
g.FillEllipse(new SolidBrush(Color.Black), rect);
}

C#, How to get the color of specific area inside an image

How can I get the color of an area that has 5x5px inside an image.
int xPixel = 200; int yPixel = 100;
Bitmap myBitmap = new Bitmap(“C:/Users/admin/Desktop/image.png");
Color pixelColor = myBitmap.GetPixel(xPixel, yPixel, 5, 5);
MessageBox.Show(pixelColor.Name);
This code does not work!.
You can use something this extension method to get dominant color in a region of an image in case they are not all the same
public static Color GetDominantColor(this Bitmap bitmap, int startX, int startY, int width, int height) {
var maxWidth = bitmap.Width;
var maxHeight = bitmap.Height;
//TODO: validate the region being requested
//Used for tally
int r = 0;
int g = 0;
int b = 0;
int totalPixels = 0;
for (int x = startX; x < (startX + width); x++) {
for (int y = startY; y < (startY + height); y++) {
Color c = bitmap.GetPixel(x, y);
r += Convert.ToInt32(c.R);
g += Convert.ToInt32(c.G);
b += Convert.ToInt32(c.B);
totalPixels++;
}
}
r /= totalPixels;
g /= totalPixels;
b /= totalPixels;
Color color = Color.FromArgb(255, (byte)r, (byte)g, (byte)b);
return color;
}
You can then use it like
Color pixelColor = myBitmap.GetDominantColor(xPixel, yPixel, 5, 5);
there is room for improvement, like using a Point and Size, or even a Rectangle
public static Color GetDominantColor(this Bitmap bitmap, Rectangle area) {
return bitmap.GetDominantColor(area.X, area.Y, area.Width, area.Height);
}
but this should be enough to get started.
A solution that uses the actual drawing methods provided by System.Drawing to resize the given area to 1x1 and get its pixel value:
public static Color GetRectangleColor(Bitmap sourceBitmap, Int32 x, Int32 y, Int32 width, Int32 height)
{
using(Bitmap onePix = new Bitmap(1,1, PixelFormat.Format24bppRgb))
{
using (Graphics pg = Graphics.FromImage(onePix)){
pg.DrawImage(sourceBitmap,
new Rectangle(0, 0, 1, 1),
new Rectangle(x, y, width, height)),
GraphicsUnit.Pixel);
return onePix.GetPixel(0, 0);
}
}
Though, if you are consistently working with squares of a uniform colour, I personally wouldn't bother. Just avoid any potential fades at the edges and you're fine:
public static Color GetRectangleCenterColor(Bitmap sourceBitmap, Int32 x, Int32 y, Int32 width, Int32 height)
{
return sourceBitmap.GetPixel(x + (width / 2), y + (height / 2));
}

How to fill curved area with color in C# Winform?

I want to draw curved area with color(black in my case) in a rectangle.
I tried multiple things FillPie, FillEllipse in OnPaint event but not able to do, I want to draw like thisFill Curved Area
I have tried the below code. but this is not what I want
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width / 2, height / 2);
gr.FillRectangle(Brushes.Black, rect);
gr.FillPie(Brushes.White, x, y, width, height, 180, 90);
using (Pen pen = new Pen(Color.Yellow, 1))
gr.DrawArc(pen, x, y, width, height, 180, 90);
}
this code is drawing like this. I dont want to create extra rectangle. MyCode
I'm not 100% sure if this is that what you wanted:
I achieved this by creating a Region with a quarter of the specified rectangle. I then exclude the pie from it using a GraphicsPath. The resulting curve is then filled using Graphics.FillRegion with a black brush:
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics gr = e.Graphics;
int x = 50;
int y = 50;
int width = 100;
int height = 100;
Rectangle rect = new Rectangle(x, y, width/ 2, height / 2);
Region r = new Region(rect);
GraphicsPath path = new GraphicsPath();
path.AddPie(x, y, width, height, 180, 90);
r.Exclude(path);
gr.FillRegion(Brushes.Black,r);
}

Dynamically created textbox not visible in Usercontrol - winforms, C#

I am trying to create textboxes and draw circles dynamically within a user control. The circle is visible but the textboxes are not visible when I run my application. Am I missing something in the code?
Please find the code below,
public partial class uscCircle : UserControl
{
public uscCircle()
{
InitializeComponent();
}
public void DrawCircle(PaintEventArgs args, int x, int y, int width, int height)
{
Pen pen = new Pen(Color.Red, 3);
Brush myBrush = new System.Drawing.SolidBrush(System.Drawing.Color.Red);
args.Graphics.FillEllipse(myBrush, x - width / 2, y - height / 2, width, height);
}
public void AddTextBox(string text, int x, int y, int width, int height)
{
markerlabel.Size = new Size(40, 15);
markerlabel.Text = text;
markerlabel.TextAlign = HorizontalAlignment.Center;
markerlabel.BorderStyle = BorderStyle.FixedSingle;
markerlabel.ForeColor = Color.White;
markerlabel.BackColor = Color.Red;
markerlabel.Location = new Point(x - (width + 14), y + height / 2);
markerlabel.Visible = true;
this.Controls.Add(markerlabel);
}
}
public partial class CalibrationForm : Form
{
private CalibrationForm_Click(object sender, EventArgs e)
{
int x = e.X;
int y = e.Y;
DrawTextBox(X, Y, 25, 25, "1234", "abcd");
}
private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
{
PaintEventArgs arg = new PaintEventArgs(this.CreateGraphics(), new Rectangle());
uscCircle circle = new uscCircle();
circle.DrawCircle(arg, x, y, width, height);
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x + 40, y, width, height);
}
}
I don't see where you add uscCircle to the form. If that isn't displayed, neither will the textbox be.
Such as:
private void DrawCircle(int x, int y, int width, int height, string MarkerID, string type)
{
uscCircle circle = new uscCircle();
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x+40, y, width, height);
this.Controls.Add(circle);
}
You are adding your TextBox into a new User control which is then not used.
uscCircle circle = new uscCircle();
circle.AddTextBox(ID, x, y, width, height);
circle.AddTextBox(type, x+40, y, width, height);
You either need to add your 'uscCircle' into the controls of the Form
this.Controls.Add(uscCircle); // Must be in your Form file
or you move the TextBox-Generation code into your form

visual c# - onPaint and transparency

I am making a simple form with two semi-transparent texts
and i put it in a paint event.
only, when I wider the form, the texts turn darker and grainy.
actualy I want the darker color but not the grainy effect.
here is my code snippet:
private void sbfToolBox_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Graphics formGraphics = this.CreateGraphics();
formGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
string drawString = "tekst";
System.Drawing.Font drawFont = new System.Drawing.Font("Arial", 50);
Color color_red = Color.FromArgb(30, 100, 0, 0);
Color color_cyan = Color.FromArgb(30, 0, 100, 100);
System.Drawing.SolidBrush brush_red = new System.Drawing.SolidBrush(color_red);
System.Drawing.SolidBrush brush_cyan = new System.Drawing.SolidBrush(color_cyan);
float x = 0.0F;
float x2 = 20.0F;
float y = 50.0F;
formGraphics.DrawString(drawString, drawFont, brush_red, x, y);
formGraphics.DrawString(drawString, drawFont, brush_cyan, x2, y);
drawFont.Dispose();
brush_red.Dispose();
brush_cyan.Dispose();
formGraphics.Dispose();
}
thanks in advance
Use the Graphics object from PaintEventArgs.
Change
System.Drawing.Graphics formGraphics = this.CreateGraphics();
To
System.Drawing.Graphics formGraphics = e.Graphics;
And remove
formGraphics.Dispose();

Categories