如何从WPF图像控制中捕获图像? [英] How Do I Capture Image From WPF Image Control?
本文介绍了如何从WPF图像控制中捕获图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的页面中有关于图像的代码。
Hi,
I have this code about an image in my page.
<!--<Border BorderThickness="5" BorderBrush="#FF000000">-->
<Image Height="304" HorizontalAlignment="Left" Stretch="Fill" VerticalAlignment="Top" Width="356" Margin="108,435,0,0" />
<!--</Border>-->
在我的图像框中,我有一个图像,但是无论如何都要捕获图像?
需要帮助。在此先感谢。
Inside my image box, I have an image, but is there anyway to capture the image?
Help needed. Thanks in advance.
推荐答案
一种方法是创建RenderBitmapTarget
并将源图像绘制到其上。然后使用它作为另一个图像
的来源。
考虑这个例子;
One approach could be to create aRenderBitmapTarget
and draw the source image onto that. And then use that as the source of anotherImage
.
Consider this example;
<window x:class="WpfApplication9.MainWindow" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MyAwesomeWindow" Height="350" Width="525">
<grid>
<grid.rowdefinitions>
<rowdefinition height="*" />
<rowdefinition height="*" />
<rowdefinition height="Auto" />
</grid.rowdefinitions>
<image x:name="Src" grid.row="0" source="C:\Temp\someimage.png" />
<image x:name="Dst" grid.row="1" />
<button grid.row="2" content="Do it!" click="ButtonBase_OnClick" />
</grid>
</window>
支持代码为;
With the backing code of;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Media;
namespace WpfApplication9 {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e) {
var visual = new DrawingVisual();
var context = visual.RenderOpen();
// Copy source image over
context.DrawImage(Src.Source, new Rect(0, 0, Src.Source.Width, Src.Source.Height));
// You can also draw whatever you like on this!
context.DrawLine(new Pen(Brushes.PeachPuff, 4.0), new Point(0, 0), new Point(100, 100));
context.Close();
var renderTarget = new RenderTargetBitmap((int)Src.Source.Width, (int)Src.Source.Height, 96, 96, PixelFormats.Pbgra32);
renderTarget.Render(visual);
Dst.Source = renderTarget;
}
}
}
在上面的示例中,图像呈现在顶部单击按钮时,图像 Src
将绘制到 Dst
图像上。由于 RenderContext
可用,您可以按照自己喜欢的方式操作图像。
希望这会有所帮助,
Fredrik
In the above example the image, as rendered onto the top image, Src
, is draw onto the Dst
image when the button is clicked. As the RenderContext
is available you can manipulate the image any which way you like.
Hope this helps,
Fredrik
这篇关于如何从WPF图像控制中捕获图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文