轉(zhuǎn)帖|其它|編輯:郝浩|2011-03-04 13:55:26.000|閱讀 1471 次
概述:要想實(shí)現(xiàn)Flash的播放支持,需要借助Flash自身的ActiveX控件. 而WPF作為一種展現(xiàn)層的技術(shù),不能自身插入COM組件,必需借助Windows Form引入ActiveX控件. 本文則是介紹通過借助System.Windows.Forms下的WebBrowser實(shí)現(xiàn).
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
要想實(shí)現(xiàn)Flash的播放支持,需要借助Flash自身的ActiveX控件.
而WPF作為一種展現(xiàn)層的技術(shù),不能自身插入COM組件,必需借助Windows Form引入ActiveX控件.
比較標(biāo)準(zhǔn)的實(shí)現(xiàn)方法,可以參考以下鏈接://blogs.msdn.com/b/jijia/archive/2007/06/07/wpf-flash-activex.aspx
而本文則是介紹通過借助System.Windows.Forms下的WebBrowser實(shí)現(xiàn).
但無論是那一種方法,本質(zhì)上都是通過在WPF程序中引用Windows Form窗體,再在其內(nèi)引入ActiveX控件.
實(shí)現(xiàn)對Flash的播放
首先,引入可在WPF上承載 Windows 窗體控件的元素:WindowsFormsHost,及添加對 Windows 窗體的引用.
具體的實(shí)現(xiàn)過程:項(xiàng)目引用--添加引用--選擇 "WindowsFormsIntegration" 及 "System.Windows.Forms" --添加
在WPF頁面分別添加這兩者的引用:‘
1 xmlns:host="clr-namespace:System.Windows.Forms.
Integration;assembly=WindowsFormsIntegration"
2 xmlns:forms="clr-namespace:System.Windows.
Forms;assembly=System.Windows.Forms"
XAML中的所有實(shí)現(xiàn)代碼:
<Window x:Class="Capture.MainWindow"
xmlns="//schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x= "//schemas.microsoft.com/winfx/2006/xaml"
xmlns:host= "clr-namespace:System.Windows.Forms.
Integration;assembly=WindowsFormsIntegration"
xmlns:forms= "clr-namespace:System.Windows.Forms;
assembly=System.Windows.Forms"
Title= "File Capture" Height="600" Width="800"
Unloaded= "Window_Unloaded"
>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="150"></ColumnDefinition>
</Grid.ColumnDefinitions>
<host:WindowsFormsHost x:Name="host">
<forms:WebBrowser x:Name="browser"></forms:WebBrowser>
</host:WindowsFormsHost>
<Grid Background="Gray" Grid.Column="1">
<UniformGrid Rows="5" VerticalAlignment="Top">
<Button x:Name="btnOpen" Width="100" Height="30"
Click= "btnOpen_Click" Margin="0,10">Open File</Button>
<Button x:Name="btnCapture" Width="100" Height="30"
Click= "btnCapture_Click" Margin="0,10">Start Capture</Button>
<Button x:Name="btnStop" Width="100" Height="30"
Click= "btnStop_Click" Margin="0,10">Stop Capture</Button>
<CheckBox x:Name="cboxLoop" IsChecked="True"
HorizontalAlignment="Left" VerticalAlignment="Center"
Margin= "25,10,0,10">Loop Capture</CheckBox>
</UniformGrid>
</Grid>
</Grid>
</Window>
當(dāng)需要把一選中的Flash文件(.swf 文件)添加進(jìn)WebBrowser 中播放:
browser.Navigate(currentPath);
實(shí)現(xiàn)對Flash的截圖
實(shí)現(xiàn)對Flash的截圖,只要使用WebBrowser的基類WebBrowserBase里面的DrawToBitmap方法.
參數(shù)Bitmap為所繪制的位圖(需初始化位圖的大小),Rectangle為截取WebBrowser內(nèi)容的區(qū)域(大小)
1 public void DrawToBitmap(
2 Bitmap bitmap,
3 Rectangle targetBounds
4 )
具體的實(shí)現(xiàn)代碼:
1 private void Capture()
2 {
3 string fileName = System.IO.Path.GetFileNameWithoutExtension(currentPath);
4 string capturePath = string.Format( "{0}\\Capture\\{1}",
System.Environment.CurrentDirectory, fileName);
5
6 if (!Directory.Exists(capturePath))
7 {
8 Directory.CreateDirectory(capturePath);
9 }
10
11 Bitmap myBitmap = new Bitmap((int)host.ActualWidth,
(int)host.ActualHeight);
12 System.Drawing.Rectangle DrawRect =
new System.Drawing.Rectangle(0, 0, (int)host.ActualWidth, (int)host.ActualHeight);
13 browser.DrawToBitmap(myBitmap, DrawRect);
14
15 string timeNow = DateTime.Now.ToString("yyyyMMddHHmmssfff");
16
17 myBitmap.Save(string.Format("{0}\\{1}_{2}.png",
capturePath, fileName, timeNow));
18 }
界面效果:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:網(wǎng)絡(luò)轉(zhuǎn)載