如何在图片周围放置边框

时间:2013-11-16 14:58:29

标签: c# image xaml windows-phone-7 windows-phone-8

在页面中,我在View

中显示我从CameraCapureTask收到的图片
<Grid x:Name="EditPageGrid" Margin="{Binding}">
    <Grid Name="ViewportContainer" Margin="12,0,12,24">
        <Image x:Name="Viewport" LayoutUpdated="Viewport_LayoutUpdated" 
               Source="{Binding}"/>
    </Grid>
</Grid>

我希望能够在此图像周围放置边框。怎么可能这样做?我想也许在某种类型的点击事件上可以打开或关闭边框,但实际应用边框是我不知所措。

2 个答案:

答案 0 :(得分:2)

您可以在边框中包含图像,如下所示:

<Grid x:Name="EditPageGrid" Margin="{Binding}">
    <Grid Name="ViewportContainer" Margin="12,0,12,24">
        <Border HorizontalAlignment="Center" BorderThickness="4" BorderBrush="Red">        
           <Image Source="C:\Users\Public\Pictures\Sample Pictures\Jellyfish.jpg"/>
        </Border>
    </Grid>
</Grid>

答案 1 :(得分:0)

我想出了这样一个事件:(可能有更好的方法,但这也有效)

  private void Viewport_Tap(object sender, System.Windows.Input.GestureEventArgs e)
  {
     int imageHeight = (Viewport.Source as BitmapImage).PixelHeight;
     int imageWidth = (Viewport.Source as BitmapImage).PixelWidth;

     Canvas myCanvas = new Canvas();
     Rectangle myBorder = new Rectangle();
     myBorder.Width = imageWidth;
     myBorder.Height = imageHeight;
     myBorder.Stroke = new SolidColorBrush(Colors.Red);
     myBorder.StrokeThickness = 10;

     Image toBorder = new Image();
     toBorder.Source = Viewport.Source as BitmapImage;

     myCanvas.Children.Add(toBorder);
     myCanvas.Children.Add(myBorder);

     WriteableBitmap newImage = new WriteableBitmap(myCanvas, null);

     //Viewport.Source = newImage; - you can use this but watch out that Viewport.Source now is not BitmapImage
     //Below is one method how to make it BitmapImage
     //You can of course save newImage to file or whatever you want
     //You can also unsubscribe this event to prevent it from second tap which will cause Exception at first line (BitmaImage != WriteableBitmap)

     MemoryStream memoryStream = new MemoryStream();
     newImage.SaveJpeg(memoryStream, imageWidth, imageHeight, 0, 100);
     BitmapImage newBitmap = new BitmapImage();
     newBitmap.SetSource(memoryStream);

     Viewport.Source = newBitmap;
  }

使用这个内存流并不好,但我不知道你打算用新的Bitmap做什么。
正如我所说 - 这是唯一的例子,我确信存在更好的方法(我不知道)。希望这会有所帮助。