GDI+在Picturebox上绘了椭圆,要做的事情是picuturebox上image落在椭圆外部的都变黑

.Net技术 码拜 9年前 (2016-04-30) 1596次浏览
GDI+在Picturebox上绘了椭圆,要做的事情是:picuturebox上的image落在椭圆外部的都变黑 是不是得遍历 ?怎么判断像素点能否在椭圆外部?还是说有其他好方法
解决方案

20

可以参考一下代码

using (var g = pictureBox1.CreateGraphics())
{
    GraphicsPath path = new GraphicsPath();
    path.AddEllipse(10, 10, 125, 125);
    path.AddRectangle(new Rectangle(new Point(), pictureBox1.Size));
    g.FillPath(Brushes.Black, path);
}

20

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace 裁剪
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            pictureBox1.Load("http://avatar.csdn.net/B/9/C/1_u011672494.jpg");
        }
        private void pictureBox1_Paint(object sender, PaintEventArgs e)
        {
            var el = sender as PictureBox;
            var g = e.Graphics;
            using (var path = new GraphicsPath())
            {
                path.AddEllipse(new Rectangle(10, 10, el.Width - 20, el.Height - 20));
                path.AddRectangle(el.ClientRectangle);
                g.FillPath(Brushes.Black, path);
            }
        }
        private void pictureBox2_Paint(object sender, PaintEventArgs e)
        {
            var el = sender as PictureBox;
            var g = e.Graphics;
            using (var path = new GraphicsPath())
            {
                path.AddEllipse(new Rectangle(10, 10, el.Width - 20, el.Height - 20));
                g.Clip = new Region(path);
                g.DrawImage(pictureBox1.Image, 0, 0);
            }
        }
    }
}

CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明GDI+在Picturebox上绘了椭圆,要做的事情是picuturebox上image落在椭圆外部的都变黑
喜欢 (0)
[1034331897@qq.com]
分享 (0)