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);
}
}
}
使用BitmapData实现图像的高速处理
最新推荐文章于 2021-04-09 16:05:05 发布