最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c#圖片處理之圖片裁剪成不規(guī)則圖形

 更新時(shí)間:2014年05月06日 10:29:42   作者:  
最近項(xiàng)目要求實(shí)現(xiàn)不規(guī)則裁剪功能。本來(lái)想用html5的canvas在前端實(shí)現(xiàn)的,但是發(fā)現(xiàn)有點(diǎn)困難,以下為C#端對(duì)圖對(duì)片的處理

為了讓大家知道下面內(nèi)容是否是自己想要的,我先發(fā)效果圖。

好了,那就開(kāi)始貼代碼了

以下為一個(gè)按鈕的事件,為裁剪準(zhǔn)備圖片、裁剪路徑、保存路徑

復(fù)制代碼 代碼如下:

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

以下為裁剪的關(guān)鍵代碼

復(fù)制代碼 代碼如下:

/// <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++)
                {
                    //判斷坐標(biāo)是否在路徑中  
                    if (path.IsVisible(i, j))
                    {
                        //復(fù)制原圖區(qū)域的像素到輸出圖片  
                        outputBitmap.SetPixel(i - left, j - top, image.GetPixel(i, j));
                        //設(shè)置原圖這部分區(qū)域?yàn)橥该?nbsp; 
                        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;
        }

有了這個(gè)前臺(tái)只需要獲取用戶(hù)的鼠標(biāo)路徑傳到后臺(tái)就可以。

相關(guān)文章

最新評(píng)論

防城港市| 浦江县| 阜城县| 巴彦县| 长岛县| 锡林浩特市| 连州市| 保定市| 彩票| 漠河县| 万年县| 台州市| 连州市| 当涂县| 南涧| 长白| 荣昌县| 宁远县| 新巴尔虎左旗| 安丘市| 宜章县| 义乌市| 玛曲县| 海丰县| 江华| 曲麻莱县| 年辖:市辖区| 柞水县| 平度市| 炎陵县| 永德县| 鹿泉市| 哈尔滨市| 景洪市| 麻栗坡县| 乐陵市| 那曲县| 县级市| 西乌珠穆沁旗| 四川省| 库尔勒市|