How to create circle with bitmap c#

12,618

Solution 1

Use ColorTranslator.FromHtml for this purpose.

This will give you the corresponding System.Drawing.Color:

using (Bitmap btm = new Bitmap(25, 30))
{
   using (Graphics grf = Graphics.FromImage(btm))
   {
      using (Brush brsh = new SolidBrush(ColorTranslator.FromHtml("#ff00ffff")))
      {
         grf.FillEllipse(brsh, 0, 0, 19, 19);
      }
   }
}

Or Refer Code:

Bitmap bmp = new Bitmap(pictureBox1.Width, pictureBox1.Height);

Graphics g = Graphics.FromImage(bmp);

Pen blackPen = new Pen(Color.Black);

int x = pictureBox1.Width/4;

int y = pictureBox1.Height/4;

int width = pictureBox1.Width / 2;

int height = pictureBox1.Height / 2;

int diameter = Math.Min(width, height);

g.DrawEllipse(blackPen, x, y, diameter, diameter);

pictureBox1.Image = bmp;

If the PictureBox already contains a bitmap, replace the first and second lines with:

Graphics g = Graphics.FromImage(pictureBox1.Image);

Referance Link:

http://www.c-sharpcorner.com/Forums/Thread/30986/

Hope Its Helpful.

Solution 2

Bitmap b = new Bitmap(261, 266);// height & width of picturebox

int xo = 50, yo = 50;// center of circle
double r, rr;

r = 20;
rr = Math.Pow(r, 2);          

        for (int i = xo - (int)r; i <= xo + r; i++)
            for (int j = yo - (int)r; j <= yo + r; j++)
                if (Math.Abs(Math.Pow(i - xo, 2) + Math.Pow(j - yo, 2) - rr) <= r)
                    b.SetPixel(i, j, Color.Black);

pictureBox1.Image = b;
Share:
12,618
Yaroslav L.
Author by

Yaroslav L.

Updated on June 04, 2022

Comments

  • Yaroslav L.
    Yaroslav L. almost 2 years

    Hello How to draw a circle with the bitmap. That is, I need to get the image of the circle, using it to draw something.