C # crop circle in image or bitmap

I am sure it is easy, but cannot find anyone who has an answer. I have an image and I need to cut out a circle or even other shapes from this image. I need this code in .net C #. I do this in a class, so this is not wpf or winform. I will need to pass x and y pos and the size of the circle.

Another option is AForge, ImageStatistics. I need to get a circle (part of the image) and get StdDev.

Thanks for the help. Andrew

- update the message chris.

Here is the chris post in C #. Not as pure as his, but his beginning.

public System.Drawing.Image x(string sourceFile, int circleUpperLeftX, int circleUpperLeftY, int circleDiameter) { Bitmap SourceImage = new Bitmap(System.Drawing.Image.FromFile(sourceFile)); Rectangle CropRect = new Rectangle(circleUpperLeftX, circleUpperLeftY, circleDiameter, circleDiameter); Bitmap CroppedImage = SourceImage.Clone(CropRect, SourceImage.PixelFormat); TextureBrush TB = new TextureBrush(CroppedImage); Bitmap FinalImage = new Bitmap(circleDiameter, circleDiameter); Graphics G = Graphics.FromImage(FinalImage); G.FillEllipse(TB, 0, 0, circleDiameter, circleDiameter); return FinalImage; } 
+4
source share
3 answers

You can use TextureBrush . The code below cuts the image into a square, then loads it into a texture brush, and finally draws an ellipse / circle with this brush:

 Private Shared Function CropImageToCircle(ByVal sourceFile As String, ByVal circleUpperLeftX As Integer, ByVal circleUpperLeftY As Integer, ByVal circleDiameter As Integer) As Image ''//Load our source image Using SourceImage As New Bitmap(Image.FromFile(sourceFile)) ''//Create a rectangle that crops a square of our image Dim CropRect As New Rectangle(circleUpperLeftX, circleUpperLeftY, circleDiameter, circleDiameter) ''//Crop the image to that square Using CroppedImage = SourceImage.Clone(CropRect, SourceImage.PixelFormat) ''//Create a texturebrush to draw our circle with Using TB As New TextureBrush(CroppedImage) ''//Create our output image Dim FinalImage As New Bitmap(circleDiameter, circleDiameter) ''//Create a graphics object to draw with Using G = Graphics.FromImage(FinalImage) ''//Draw our cropped image onto the output image as an ellipse with the same width/height (circle) G.FillEllipse(TB, 0, 0, circleDiameter, circleDiameter) Return FinalImage End Using End Using End Using End Using End Function 
+5
source

Make a new bitmap that matches the original in pixel format and format. Create graphics from this new bitmap. Set the graphic clip in a new circle. Draw the original image on the new chart.

 public Bitmap ClipToCircle(Bitmap original, PointF center, float radius) { Bitmap copy = new Bitmap(original); using (Graphics g = Graphics.FromImage(copy)) { RectangleF r = new RectangleF(center.X - radius, center.Y - radius, radius * 2, radius * 2); GraphicsPath path = new GraphicsPath(); path.AddEllipse(r); g.Clip = new Region(path); g.DrawImage(original, 0, 0); return copy; } } 
+3
source

See this . You will need to call the GDI + classes (which will not be a problem in C #) for the required functionality.

0
source

All Articles