加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c#图片处理之图片裁剪成不规则图形

发布时间:2020-12-15 04:04:07 所属栏目:百科 来源:网络整理
导读:为了让大家知道下面内容是否是自己想要的,我先发效果图。 好了,那就开始贴代码了 以下为一个按钮的事件,为裁剪准备图片、裁剪路径、保存路径 复制代码 代码如下: private void button1_Click(object sender,EventArgs e) { GraphicsPath path = new Graph

为了让大家知道下面内容是否是自己想要的,我先发效果图。

好了,那就开始贴代码了

以下为一个按钮的事件,为裁剪准备图片、裁剪路径、保存路径

复制代码 代码如下:

private void button1_Click(object sender,EventArgs e)
        {
            GraphicsPath path = new GraphicsPath();
            Point[] p = {
                            new Point(424,244),
                            new Point(240,440),
                            new Point(340,552),
                            new Point(550,526),
                            new Point(478,366),
                            new Point(348,660),
                            new Point(424,244)
                        };
            path.AddLines(p);
            OpenFileDialog openFileDialog = new OpenFileDialog();

            openFileDialog.Filter = @"Bitmap文件(*.bmp)|*.bmp|Jpeg文件(*.jpg)|*.jpg|所有合适文件(*.bmp,*.jpg)|*.bmp;*.jpg";

            openFileDialog.FilterIndex = 3;

            openFileDialog.RestoreDirectory = true;

            if (DialogResult.OK == openFileDialog.ShowDialog())
            {

                Bitmap bit = (Bitmap)Bitmap.FromFile(openFileDialog.FileName,false);

                Bitmap newBit = null;
                BitmapCrop(bit,path,out newBit);
                newBit.Save(@"C:UsersPublicPicturesSample Pictures沙漠22.jpg");
            }
        }

以下为裁剪的关键代码

复制代码 代码如下:

/// <summary>
        /// 图片截图
        /// </summary>
        /// <param name="bitmap">原图</param>
        /// <param name="path">裁剪路径</param>
        /// <param name="outputBitmap">输出图</param>
        /// <returns></returns>
        public static Bitmap BitmapCrop(Bitmap bitmap,GraphicsPath path,out Bitmap outputBitmap)
        {
            RectangleF rect = path.GetBounds();
            int left = (int)rect.Left;
            int top = (int)rect.Top;
            int width = (int)rect.Width;
            int height = (int)rect.Height;
            Bitmap image = (Bitmap)bitmap.Clone();
            outputBitmap = new Bitmap(width,height);
            for (int i = left; i < left + width; i++)
            {
                for (int j = top; j < top + height; j++)
                {
                    //判断坐标是否在路径中  
                    if (path.IsVisible(i,j))
                    {
                        //复制原图区域的像素到输出图片  
                        outputBitmap.SetPixel(i - left,j - top,image.GetPixel(i,j));
                        //设置原图这部分区域为透明  
                        image.SetPixel(i,j,Color.FromArgb(0,j)));
                    }
                    else
                    {
                        outputBitmap.SetPixel(i - left,255,255));
                    }
                }
            }
            bitmap.Dispose();
            return image;
        }

有了这个前台只需要获取用户的鼠标路径传到后台就可以。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读