C#麦田怪圈一个图像或位图 [英] C# crop circle in a image or bitmap

查看:81
本文介绍了C#麦田怪圈一个图像或位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信它很容易,但似乎无法找到任何人有答案。
我有一个形象,我需要削减了一圈,甚至其他形状出形象。我需要这个code是在.NET C#。我在一个类这样做所以它不是WPF或WinForm的。
我需要通过x和y POS和圆的大小

其他选项AForge,ImageStatistics。我需要得到一个圆(图像的一部分),并获得STDDEV。

感谢您的帮助。
安德鲁

- 更新后克里斯

下面是克里斯后在C#。不一样干净了,但它的一个开始。

 公共System.Drawing.Image对象X(字符串的资源文件,诠释circleUpperLeftX,诠释circleUpperLeftY,诠释circleDiameter)
    {
        位图SourceImage =新位图(System.Drawing.Image.FromFile(的资源文件));
        矩形克罗马preCT =新的Rectangle(circleUpperLeftX,circleUpperLeftY,circleDiameter,circleDiameter);
        位图CroppedImage = SourceImage.Clone(CRO preCT,SourceImage.PixelFormat);
        的TextureBrush TB =新的TextureBrush(CroppedImage);
        位图FinalImage =新位图(circleDiameter,circleDiameter);
        图形G = Graphics.FromImage(FinalImage);
        G.FillEllipse(TB,0,0,circleDiameter,circleDiameter);
        返回FinalImage;
    }


解决方案

您可以使用的的TextureBrush 。下面的code图像裁剪成正方形,然后加载到贴图刷,最后使用画笔绘制椭圆/圆:

 专用共享功能CropImageToCircle(BYVAL的资源文件作为字符串,BYVAL circleUpperLeftX整数,BYVAL circleUpperLeftY整数,BYVAL circleDiameter作为整数)作为图像
    ''//加载我们的源图像
    使用SourceImage作为新位图(Image.FromFile(的资源文件))
        ''//创建农作物我们的形象的一个正方形的矩形
        昏暗的克罗马preCT作为新的矩形(circleUpperLeftX,circleUpperLeftY,circleDiameter,circleDiameter)
        ''//图像裁剪到广场
        使用CroppedImage = SourceImage.Clone(CRO preCT,SourceImage.PixelFormat)
            ''//创建的TextureBrush绘制我们的圈子用
            使用TB作为新的TextureBrush(CroppedImage)
                ''//创建我们的输出图像
                昏暗FinalImage作为新位图(circleDiameter,circleDiameter)
                ''//创建一个图形对象进行绘制
                采用G = Graphics.FromImage(FinalImage)
                    ''//绘制我们的裁剪图像到输出图像为具有相同宽度/高度(圆)椭圆
                    G.FillEllipse(TB,0,0,circleDiameter,circleDiameter)
                    返回FinalImage
                使用完
            使用完
        使用完
    使用完
结束功能

I am sure its easy but can't seem to find anyone that has the answer. I have an image and I need to cut a circle or even other shapes out of that image. I need this code to be in .net c#. I am doing this in a class so its not wpf or a winform. I will need to pass x and y pos and the size of the circle.

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

Thanks for the help. Andrew

-- update to chris post.

Here is chris post in c#. Not as clean as his but its a start.

 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;
    }

解决方案

You can use a TextureBrush. The code below crops the image to a square, then loads that into a texture brush and finally draws an ellipse/circle using that 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

这篇关于C#麦田怪圈一个图像或位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆