在WP7上使用ScaleTransform后出现奇怪的行为

时间:2012-08-17 23:07:15

标签: c# silverlight windows-phone-7 image-manipulation

我有两个Image控件,我将一些像素的alpha通道从上面设置为零(这是多彩的)。但在我“缩放”(ScaleTransform的宽度)后,在已设置的像素周围将显示“边框”。这是一个截图:

enter image description here

以下是代码:

        <Grid Name="grdPhotos">
            <Image Stretch="None" Source="picture_grayscale.jpg" Name="photo1" HorizontalAlignment="Left" VerticalAlignment="Top" />
            <Image Stretch="None" Source="picture.jpg" Name="photo2" MouseLeftButtonDown="photo2_MouseLeftButtonDown" HorizontalAlignment="Left" VerticalAlignment="Top" />
        </Grid>

    private void photo2_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        var photo = photo2.Source as WriteableBitmap; // A WriteableBitmap is created before from the Source BitmapImage
        for (int x = 100; x < 200; x++)
        {
            for (int y = 100; y < 200; y++)
            {
                int index = Convert.ToInt32(photo.PixelWidth * y + x);
                if (index > 0 && index < photo.Pixels.Length)
                    SetPixelAlphaChannel(ref photo.Pixels[index], 0);
            }
        }

        var transform = new ScaleTransform { ScaleX = 2, ScaleY = 2 };
        photo1.RenderTransform = photo2.RenderTransform = transform;
    }

    public void SetPixelAlphaChannel(ref int pixel, byte value)
    {
        var color = ColorFromPixel(pixel);
        if (color.A == value)
            return;

        color.A = value;
        pixel = ColorToPixel(color);
    }

    private Color ColorFromPixel(int pixel)
    {
        var argbBytes = BitConverter.GetBytes(pixel);
        return new Color { A = argbBytes[3], R = argbBytes[2], G = argbBytes[1], B = argbBytes[0] };
    }
    private int ColorToPixel(Color color)
    {
        var argbBytes = new byte[] { color.B, color.G, color.R, color.A };
        return BitConverter.ToInt32(argbBytes, 0);
    }

这是为什么?或者如何在没有此“边框”的情况下实现缩放功能?非常感谢。

1 个答案:

答案 0 :(得分:0)

缩放图像时,像素值将为interpolated,这将导致边框中的像素显示您使用非透明邻居插入透明像素的结果。遗憾的是,您无法控制渲染变换的插值行为。您必须自己完成此操作,可能是通过WriteableBitmap

相关问题