首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ScaleTransform on WP7后的奇怪行为

使用ScaleTransform on WP7后的奇怪行为
EN

Stack Overflow用户
提问于 2012-08-17 23:07:15
回答 1查看 193关注 0票数 0

我在对方有两个图像控件,我将一些像素的alpha通道设置为零,从上一个(这是彩色的)。但在我“缩放”(宽度为ScaleTransform)之后,“边框”将在设置好的像素周围可见。这是一个截图:

以下是代码:

代码语言:javascript
复制
        <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>
代码语言:javascript
复制
    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);
    }

为什么会这样呢?或者,如果没有这个“边框”,我如何实现缩放功能?非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-08-18 06:35:03

缩放图像时,像素值将为内插,这将导致像素在边框中--您正在观察到的结果是将透明像素与其非透明邻居进行插值的结果。不幸的是,您无法控制渲染转换的内插行为。您将不得不亲自完成此操作,可能通过WriteableBitmap

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12014233

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档