【问题标题】:How to Place Border Around a Picture如何在图片周围放置边框
【发布时间】:2013-11-16 14:58:29
【问题描述】:

在页面中,我在视图中显示从 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>

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

【问题讨论】:

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


【解决方案1】:

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

<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>

【讨论】:

  • 我想我在最初的问题中说错了。我需要在单击事件上的图像周围放置边框,然后我需要将边框和图像实际保存为现在包含边框的新图像。
  • 您可以在单击按钮时将图像包裹在边框中,从父网格中拉出图像,创建新边框,将图像添加到边框内容,然后将边框添加回网格。这篇文章建议有一种方法可以将内容元素转换为图像/位图:stackoverflow.com/questions/15918296/…
【解决方案2】:

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

  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;
  }

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 2015-07-02
    • 2017-02-22
    相关资源
    最近更新 更多