c#图像或位图中的麦田圈

本文关键字:麦田 位图 图像 | 更新日期: 2023-09-27 17:51:23

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

其他选项是AForge, imagatistics。我需要得到一个圆圈(图像的一部分),并得到StdDev。

谢谢你的帮助。安德鲁。

—update to Chris post.

这是chris post用c#写的。没有他干净,但也算是个开始。

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

c#图像或位图中的麦田圈

你可以使用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

制作一个与原始大小和像素格式匹配的新位图。从新的位图创建一个图形。将图形夹设置为一个新圆。将原始图像绘制到新图形上。

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

这段代码可能对您有所帮助截图

类代码:

using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
public class ImageHelper
{
public Image CropImage(Image img)
{
    int x = img.Width/2;
    int y = img.Height/2;
    int r = Math.Min(x, y);
    Bitmap tmp = null;
    tmp = new Bitmap(2 * r, 2 * r);
    using (Graphics g = Graphics.FromImage(tmp))
    {
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.TranslateTransform(tmp.Width / 2, tmp.Height / 2);
        GraphicsPath gp = new GraphicsPath();
        gp.AddEllipse(0 - r, 0 - r, 2 * r, 2 * r);
        Region rg = new Region(gp);
        g.SetClip(rg, CombineMode.Replace);
        Bitmap bmp = new Bitmap(img);
        g.DrawImage(bmp, new Rectangle(-r, -r, 2 * r, 2 * r), new Rectangle(x - r, y - r, 2 * r, 2 * r), GraphicsUnit.Pixel);
    }

        return tmp;
}
public void SaveImage(Image img,string path,ImageFormat imageFormat)
{
    img.Save(path, imageFormat);
}
}

Form1.cs

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
namespace CropImageToCircle
{

    //Browse Image
    private void button1_Click(object sender, EventArgs e)
    {
        var ofd = new OpenFileDialog();
        ofd.Filter = "Choose Image(*.jpg;,*.jpeg;,*.png;)|*.jpg;,*.jpeg;,*.png;";
        if(ofd.ShowDialog()== DialogResult.OK)
        {
            pictureBox1.Image =Image.FromFile( ofd.FileName);
        }
    }
    //CropToCircle
    private void button2_Click(object sender, EventArgs e)
    {
        var imageHelper = new ImageHelper();
       pictureBox2.Image= imageHelper.CropImage(pictureBox1.Image);
    }
    //Save Image
    private void button3_Click(object sender, EventArgs e)
    {
        var imageHelper = new ImageHelper();
        var sfd = new SaveFileDialog();
        sfd.FileName = "*";
        sfd.DefaultExt = "png";
        sfd.Filter = "Png Image (.png)|*.png ";
        sfd.ValidateNames = true;
        sfd.FilterIndex = 1;
        if (sfd.ShowDialog() == DialogResult.OK)
        {
            imageHelper.SaveImage(pictureBox2.Image,sfd.FileName,ImageFormat.Png);
        }
    }
}

}