国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

c#圖片處理之圖片裁剪成不規則圖形

2020-01-24 02:41:07
字體:
來源:轉載
供稿:網友

為了讓大家知道下面內容是否是自己想要的,我先發效果圖。

好了,那就開始貼代碼了

以下為一個按鈕的事件,為裁剪準備圖片、裁剪路徑、保存路徑

復制代碼 代碼如下:

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

有了這個前臺只需要獲取用戶的鼠標路徑傳到后臺就可以。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 洪湖市| 南靖县| 辽宁省| 丹凤县| 东城区| 桑日县| 仲巴县| 永寿县| 海伦市| 思茅市| 故城县| 繁昌县| 临夏市| 高尔夫| 临朐县| 祁门县| 卢湾区| 长岭县| 肥城市| 宜君县| 景宁| 房产| 汶川县| 罗定市| 安远县| 齐河县| 嵊州市| 满洲里市| 吴堡县| 阿荣旗| 宝清县| 瑞安市| 桐乡市| 沽源县| 娄烦县| 兰考县| 临猗县| 黄梅县| 通道| 萨嘎县| 冕宁县|