您的位置:首页 > 其它

使用BitmapData实现图像的高速处理

2014-05-19 23:15 351 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;

namespace 浮雕
{
    public class PhotoEmboss
    {
        public Bitmap Emboss(Bitmap _bmp, int pix, int angle)
        {
            Bitmap bmp = _bmp;
            Rectangle rect = new Rectangle(0, 0, bmp.Width, bmp.Height);
            BitmapData bmd = bmp.LockBits(rect, ImageLockMode.ReadWrite, bmp.PixelFormat);
            IntPtr ptr = bmd.Scan0;
            int w = bmd.Stride;
            int h = bmd.Height;
            int bytes = w * h;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            byte colorTemp;
            byte colorNext;
            for (int i = 0; i < w - 4; i += 3)
            {
                for (int j = 0; j < h - 1; j++)
                {
                    colorTemp = (byte)(rgbValues[j * w + i] * 0.299 + rgbValues[j * w + i + 1] * 0.587 + rgbValues[j * w + i + 2] * 0.114);
                    colorNext = (byte)(rgbValues[(j + 1) * w + i + 3] * 0.299 + rgbValues[(j + 1) * w + i + 4] * 0.587 + rgbValues[(j + 1) * w + i + 5] * 0.114);
                    int colorDif = colorTemp - colorNext + 128;
                    colorDif = (colorDif < 0 || colorDif > 255) ? 0 : colorDif;
                    rgbValues[j * w + i] = (byte)colorDif;
                    rgbValues[j * w + i + 1] = (byte)colorDif;
                    rgbValues[j * w + i + 2] = (byte)colorDif;
                }
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            bmp.UnlockBits(bmd);
            return bmp;
        }

        public Bitmap Emboss(Bitmap _bmp)
        {
            return Emboss(_bmp, 1, 135);
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: