如何在圆圈中裁剪指定区域的图像 [英] how to crop images specified area in circle

查看:134
本文介绍了如何在圆圈中裁剪指定区域的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想以圆圈形状裁剪图像指定区域 ..

我可以裁剪矩形,但不知道如何裁剪圆圈。这是代码...

I want to crop image in circle shape Specified area..
I can crop rectangle but dont have any idea how to crop it in circle. Here is the code...

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WaheedSab
{
    public partial class Form1 : Form
    {
        //Browse Folder
        public FolderBrowserDialog open = new FolderBrowserDialog();

        int countImage;

        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            //set check folder is selected
            if (open.ShowDialog() == DialogResult.OK)
            {
                countImage = Directory.GetFiles(open.SelectedPath, "*.jpg", SearchOption.TopDirectoryOnly).Length;
                lblDetectedImages.Text = countImage.ToString();
                textBox1.Text = open.SelectedPath;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            lblDetectedImages.Text = string.Empty;
            textBox1.Text = "";
        }

        private void btnCrop_Click(object sender, EventArgs e)
        {
            string subdirectory = Path.Combine(open.SelectedPath, "CroppedImges");
            Directory.CreateDirectory(subdirectory);

            //listing images type
            List<string> extensions = new List<string> { ".jpg", };
            string[] DetectedImages = Directory.GetFiles(open.SelectedPath);

            //initialize progressbar
            progressBar1.Minimum = 1;
            progressBar1.Maximum = countImage;
            progressBar1.Value = 1;
            progressBar1.Step = 1;
            //loop images
            foreach (string file in DetectedImages)
            {
                if (extensions.Contains(Path.GetExtension(file).ToLower()))
                {
                    Bitmap source = new Bitmap(file);
                    Rectangle section = new Rectangle(new Point(1270, 390), new Size(500, 450));

                    Bitmap CroppedImage = CropImage(source, section);

                    //update user
                    lblUpdater.Text = Path.GetFileName(file);
                    //save croppedImage in subdirectory
                    string CroppedImageName = Path.Combine(subdirectory, Path.GetFileNameWithoutExtension(file) + ".jpg");

                    CroppedImage.Save(CroppedImageName);
                    progressBar1.PerformStep();
                }
            }
            lblDetectedImages.Text = string.Empty;
            lblStatus.Text = "Cropped successfully";

        }

        //Crop Function
        public Bitmap CropImage(Bitmap source, Rectangle section)
        {   //empty file for holding croped image
            Bitmap bmp = new Bitmap(section.Width, section.Height);
            Graphics g = Graphics.FromImage(bmp);

            // Draw the given area (section) of the source image
            // at location 0,0 on the empty bitmap (bmp)
            g.DrawImage(source, 0, 0, section, GraphicsUnit.Pixel);

            return bmp;

        }
    }
}

推荐答案

术语裁剪意味着什么不同。您无法将图像裁剪为圆形;根据定义,它只是一个矩形。也许你应该提到剪裁。



你可以做以下两件事之一:1)使图像部分透明,在不透明的内部有一些圆形区域,其余的透明;图像看起来很圆,但实际上它将是一个正方形; 2)你可以在一些控件中渲染图像,你可以将其剪裁成圆形。



第一个选项非常明显,我们来讨论第二个选项。您可以将属性 System.Windows.Forms.Control.Region 设置为所需的非矩形区域:

Control.Region Property(System.Windows.Forms) [ ^ ],

区域类(System.Drawing) [ ^ ]。



注意将转向控制非矩形的不仅仅是它的外观。例如,如果您将其设置为圆形并单击其边界矩形的角落,则不会将事件分派给它,而是分配给下面的控件。这对表格也有效。



-SA
The term "cropping" means something different. You cannot crop an image to a round shape; by definition, it's only a rectangle. Perhaps you should have mentioned "clipping" instead.

You can do one of the two things: 1) make an image partially transparent, with some round area inside opaque, and the rest transparent; the image will look round, but actually it will be a square; 2) you can render image in some control which you can clip to a round shape.

First option is pretty obvious, let's discuss the second one. You can set the property System.Windows.Forms.Control.Region to required non-rectangular region:
Control.Region Property (System.Windows.Forms)[^],
Region Class (System.Drawing)[^].

Note that will turn to control to a non-rectangular one not only by its look. For example, if you make it round and click in the corner of its bounding rectangle, the event will be dispatched not to it, but to a control underneath. This is valid even for forms.

—SA


这篇关于如何在圆圈中裁剪指定区域的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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