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

时间:2023-02-23 02:43:58 

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

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

好了,那就开始贴代码了

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


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:\Users\Public\Pictures\Sample 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, image.GetPixel(i, j)));
                    }
                    else
                    {
                        outputBitmap.SetPixel(i - left, j - top, Color.FromArgb(0, 255, 255, 255));
                    }
                }
            }
            bitmap.Dispose();
            return image;
        }

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

标签:c#,图片
0
投稿

猜你喜欢

  • Android AutoCompleteTextView控件使用实例

    2023-11-24 03:01:56
  • Android中隐藏标题栏和状态栏的方法

    2022-04-16 07:04:27
  • C#中委托的基础入门与实现方法

    2022-05-24 22:04:24
  • SpringBoot整合Groovy脚本实现动态编程详解

    2023-04-02 03:24:16
  • 全面解析Android中对EditText输入实现监听的方法

    2022-09-15 15:05:55
  • springboot依赖冲突问题及解决过程

    2023-03-13 11:49:53
  • Java 深拷贝与浅拷贝的分析

    2023-07-30 14:13:13
  • Android开发之WebView组件的使用解析

    2022-07-22 21:52:15
  • TransmittableThreadLocal解决线程间上下文传递烦恼

    2023-11-09 17:09:35
  • WinFrom中label背景透明的实现方法

    2023-05-08 13:05:40
  • Spring事务失效问题分析及解决方案

    2023-07-09 11:08:15
  • C#中执行批处理文件(*.bat)的方法代码

    2022-12-01 10:25:01
  • 详解java中的阻塞队列

    2021-11-26 12:24:44
  • C#实现图片加相框的方法

    2022-06-02 13:52:28
  • 布隆过滤器(Bloom Filter)的Java实现方法

    2022-09-14 18:06:06
  • 解决mybatis三表连接查询数据重复的问题

    2023-09-13 15:24:25
  • C# this关键字的四种用法

    2022-10-06 07:09:22
  • springBoot 创建定时任务过程详解

    2022-10-17 18:51:55
  • Javacsv实现Java读写csv文件

    2022-02-16 01:10:15
  • java实现字符串反转案例

    2021-11-20 04:45:55
  • asp之家 软件编程 m.aspxhome.com