C#在图像或位图中裁剪圆圈

我确信它很容易,但似乎找不到任何有答案的人.
我有一个图像,我需要从该图像中切出一个圆或甚至其他形状.我需要这个代码在.net c#中.我在课堂上这样做,所以它不是wpf或winform.
我需要传递x和y pos以及圆的大小.

其他选择是AForge,ImageStatistics.我需要得到一个圆圈(图像的一部分)并获得StdDev.

谢谢您的帮助.
安德鲁

– 更新到chris帖子.

这是c#中的chris帖子.不像他那么干净,但却是一个开始.

 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;
    }
最佳答案
您可以使用TextureBrush.下面的代码将图像裁剪为正方形,然后将其加载到纹理画笔中,最后使用该画笔绘制椭圆/圆形:

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

转载注明原文:C#在图像或位图中裁剪圆圈 - 代码日志