圖像特效---Oilpaint油畫濾鏡

  Oilpaint油畫濾鏡
 核心代碼如下:

        ///

        /// Mosaic filter.

        ///

        /// Source  image.

        /// The size of mosaic effect.

        /// Resullt image.

        public Bitmap OilpaintFilter(Bitmap src, int intensity)

        {

            Bitmap srcBitmap = new Bitmap(src);

            int w = srcBitmap.Width;

            int h = srcBitmap.Height;

            System.Drawing.Imaging.BitmapData srcData = srcBitmap.LockBits(new Rectangle(0, 0, w, h), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            IntPtr ptr = srcData.Scan0;

            int bytes = h * srcData.Stride;

            byte[] srcValues = new byte[bytes];

            System.Runtime.InteropServices.Marshal.Copy(ptr, srcValues, 0, bytes);

            byte[] tempValues = (byte[])srcValues.Clone();

            int stride = srcData.Stride;

            Random ran = new Random();

            int k = 0;

            int dx = 0;

            int dy = 0;

            for (int j = 0; j < h; j++)

            {

                for (int i = 0; i < w; i++)

                {

                    k = ran.Next(intensity);

                    dx = (i + k) >= w ? w - 1 : (i + k);

                    dy = (j + k) >= h ? h - 1 : (j + k);

                    tempValues[i * 4 + j * w * 4] = (byte)srcValues[dx * 4 + dy * w * 4];

                    tempValues[i * 4 + 1 + j * w * 4] = (byte)srcValues[dx * 4 + 1 + dy * w * 4];

                    tempValues[i * 4 + 2 + j * w * 4] = (byte)srcValues[dx * 4 + 2 + dy * w * 4];

                }

            }

            srcValues = (byte[])tempValues.Clone();

            System.Runtime.InteropServices.Marshal.Copy(srcValues, 0, ptr, bytes);

            srcBitmap.UnlockBits(srcData);

            return srcBitmap;

        }


程序demo: http://www.zealfilter.com/forum.php?mod=viewthread&tid=52&extra=page%3D2

發佈了30 篇原創文章 · 獲贊 14 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章