目的:我想做的效果是点按钮,图像平移一段距离。
现在的方法:
规定必须用处理每个像素点的颜色来进行平移(我知道有现成的平移函数,但是需求必须用每个像素处理)
问题:
效果能实现,但是出现点一次图像是原色(彩色),再点一次变为很浅的颜色(只有两种颜色),依次不断循环
如下情况:
附上代码:
//获取位图
BitmapSource bmp = (BitmapSource)img1.Source;
int length = (bmp.PixelWidth * bmp.Format.BitsPerPixel + 7) / 8;
byte[] rawImage = new byte[length * bmp.PixelHeight];
bmp.CopyPixels(rawImage, length, 0);
//临时数组初始值为255
byte[] tempImage = new byte[length * bmp.PixelHeight];
for (int i = 0; i < tempImage.Length; i++)
{
tempImage[i] = 255;
}
//平移量
int x = 50;
int y = 50;
//平移位图
for (int j = 0; j < bmp.PixelHeight; j++)
{
if ((j + y) > 0 && (j + y) < bmp.PixelHeight)
{
for (int i = 0; i < length; i++)
{
if ((i + x) > 0 && (i + x) < length)
{
tempImage[(i + x) + (j + y) * length] = rawImage[i + j * length];
}
}
}
}
BitmapSource bitmap = BitmapSource.Create(bmp.PixelWidth, bmp.PixelHeight, bmp.DpiX, bmp.DpiY, PixelFormats.Bgr32, bmp.Palette, tempImage, length);
img1.Source = bitmap;