程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#圖片處置之圖片裁剪成不規矩圖形

c#圖片處置之圖片裁剪成不規矩圖形

編輯:C#入門知識

c#圖片處置之圖片裁剪成不規矩圖形。本站提示廣大學習愛好者:(c#圖片處置之圖片裁剪成不規矩圖形)文章只能為提供參考,不一定能成為您想要的結果。以下是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;
        }

有了這個前台只須要獲得用戶的鼠標途徑傳到後台便可以。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved