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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值