if (r < 0) r = 0;
if (r > 255) r = 255;
if (g < 0) g = 0;
if (g > 255) g = 255;
if (b < 0) b = 0;
if (b > 255) b = 255;
int off2 = (j * Width + i) * 4;
pout[off2 + 0] = (byte)r;
pout[off2 + 1] = (byte)g;
pout[off2 + 2] = (byte)b;
}
}
bitmap.UnlockBits(newData);
MyBitmap.UnlockBits(oldData);
this.pbshowbox.Image = bitmap;
}
}
else
{
MessageBox.Show("请先打开一张图片!");
}
}
四、浮雕
对图像像素点的像素值分别与相邻像素点的像素值相减后加上128, 然后将其作为新的像素点的值。
/// <summary>
/// 浮雕实现方法
/// </summary>
void Image_Relief()
{
if (this.pbshowbox.Image != null)
{
int Height = this.pbshowbox.Image.Height;
int Width = this.pbshowbox.Image.Width;
Bitmap bitmap = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);
Bitmap MyBitmap = (Bitmap)this.pbshowbox.Image;
BitmapData oldData = MyBitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
BitmapData newData = bitmap.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte* pin_1 = (byte*)(oldData.Scan0.ToPointer());
byte* pin_2 = pin_1 + (oldData.Stride);
byte* pout = (byte*)(newData.Scan0.ToPointer());
for (int y = 0; y < oldData.Height - 1; y++)
{
for (int x = 0; x < oldData.Width; x++)
{
int b = (int)pin_1[0] - (int)pin_2[0] + 128;
int g = (int)pin_1[1] - (int)pin_2[1] + 128;
int r = (int)pin_1[2] - (int)pin_2[2] + 128;