C# 在WP7上使用ScaleTransform后的奇怪行为
我有两个图像控件,我将一些像素的alpha通道从上一个设置为零(这是彩色的)。但在我“缩放”(缩放变换的宽度)之后,在设置的像素周围会出现一个“边框”。以下是一个屏幕截图: 代码如下:C# 在WP7上使用ScaleTransform后的奇怪行为,c#,silverlight,windows-phone-7,image-manipulation,C#,Silverlight,Windows Phone 7,Image Manipulation,我有两个图像控件,我将一些像素的alpha通道从上一个设置为零(这是彩色的)。但在我“缩放”(缩放变换的宽度)之后,在设置的像素周围会出现一个“边框”。以下是一个屏幕截图: 代码如下: <Grid Name="grdPhotos"> <Image Stretch="None" Source="picture_grayscale.jpg" Name="photo1" HorizontalAlignment="Left" VerticalA
<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(对象发送器,MouseButtonEventArgs e)
{
var photo=photo2.Source as WriteableBitmap;//以前从源位图创建了一个WriteableBitmap
对于(int x=100;x<200;x++)
{
对于(int y=100;y<200;y++)
{
int index=Convert.ToInt32(photo.PixelWidth*y+x);
if(索引>0&&index
为什么会这样?或者,如果没有此“边框”,如何实现缩放功能?非常感谢。当您缩放图像时,像素值将为,这将导致边界中的像素是您正在观察的,这是将透明像素与其非透明邻居进行插值的结果。很遗憾,您无法控制渲染变换的插值行为。您必须自己完成此操作,可能需要通过
WriteableBitmap
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);
}