qq_14919529 2015-04-06 06:56 采纳率: 0%
浏览 1431

wpf图片处理问题求大神指教

目的:我想做的效果是点按钮,图像平移一段距离。
现在的方法:
规定必须用处理每个像素点的颜色来进行平移(我知道有现成的平移函数,但是需求必须用每个像素处理)
问题:
效果能实现,但是出现点一次图像是原色(彩色),再点一次变为很浅的颜色(只有两种颜色),依次不断循环
如下情况:
图片说明
图片说明

附上代码:
//获取位图
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;
  • 写回答

3条回答 默认 最新

  • qq_14919529 2015-04-06 06:58
    关注

    代码发不完整 补全:
    //获取位图
    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分