silverlight 类似百度的图片浏览器,视频播放,附件下载功能,全部后台程序。
#region =========图片点击按钮================ String[] nr = new String[7] { "房屋照片", "房屋所有权证", "国有土地使用证", "土地权属界线图", "规划定位测量图", "房屋测绘技术报告", "历史遗留权属确认申请审批表" }; void showTP_Click(object sender, RoutedEventArgs e) { stp1.Children.Clear(); ListBox litp = new ListBox(); litp.Width = 340; litp.MaxHeight = 300; litp.Background = new SolidColorBrush(Colors.Transparent);//背景透明 litp.BorderBrush = new SolidColorBrush(Colors.Transparent);//去除边框 foreach (var fjxx in nr) { HyperlinkButton hp = new HyperlinkButton(); hp.Click += new RoutedEventHandler(hp_Click); hp.Tag = fjxx; hp.Content = fjxx;//HyperlinkButton显示的名字 hp.FontSize = 15; litp.Items.Add(hp); } stp1.Children.Add(litp); //LoadImages(); //MessageBox.Show("此功能正在维护中。。。。。!"); } void hp_Click(object sender, RoutedEventArgs e) { //FJXZDatasLoadComplete += new DataLoadCompleteEventHandle(DetailList_FJXZDatasLoadComplete); HyperlinkButton nhp = sender as HyperlinkButton;//获取一些数据 string name = nhp.Tag.ToString(); string str = string.Empty; dj = "点击图片"; if (name.Equals(nr[0])) { str = " and (FJHZ=upper('.jpg') or FJHZ='.jpg') and FJZL='0' "; GetFJDatas(str); } } public class Picture { public ImageSource Href { get; set; } } ListBox lbImage = new ListBox(); private void LoadImages() { stp1.Children.Clear(); //stp1.VerticalAlignment = System.Windows.VerticalAlignment.Bottom; lbImage.SelectionChanged -= lbImage_SelectionChanged;//先释放事件 lbImage.Items.Clear(); lbImage.SelectionChanged += lbImage_SelectionChanged;//在注册事件 和上面的是一对 lbImage.Style = Application.Current.Resources["horizontalListBoxStyle"] as Style;//获取样式 lbImage.Background = new SolidColorBrush(Colors.Transparent);//背景 lbImage.VerticalAlignment = VerticalAlignment.Bottom; lbImage.HorizontalAlignment = HorizontalAlignment.Center; lbImage.VerticalAlignment = VerticalAlignment.Center; lbImage.Height = 90; lbImage.Width = 350; List<Picture> coll = new List<Picture>(); coll.Clear(); string tpqz = Config.Instance.HousePictureAddress;//获取地址 foreach (var imgs in FJXZDatas) { string DZ = String.Format(tpqz, imgs.FJQM) + "$" + imgs.FJMC; //获取图片的地址+附件名称(用于鼠标放上去显示图片名称) coll.Add(AddPicture(DZ)); } //coll.Add(AddPicture("http://192.168.3.222/FWTP/03f9a0ce-14fe-42c9-9d32-fe57182d2cd8.jpg")); //coll.Add(AddPicture("http://192.168.3.222/FWTP/03f9a0ce-14fe-42c9-9d32-fe57182d2cd8.jpg")); //coll.Add(AddPicture("http://192.168.3.222/FWTP/03f9a0ce-14fe-42c9-9d32-fe57182d2cd8.jpg")); //coll.Add(AddPicture("http://192.168.3.222/FWTP/03f9a0ce-14fe-42c9-9d32-fe57182d2cd8.jpg")); //coll.Add(AddPicture("http://fc06.deviantart.net/fs70/i/2010/132/8/6/Me_heart_by_Skategirl.jpg")); //coll.Add(AddPicture("http://fc01.deviantart.net/fs70/f/2010/132/4/7/Of_Day_and_Night_by_SomaKun.jpg")); //coll.Add(AddPicture("http://fc09.deviantart.net/fs70/f/2010/132/e/0/Asya___Interface_by_MelihOG.jpg")); //coll.Add(AddPicture("http://fc01.deviantart.net/fs70/f/2010/132/3/2/Betray_the_Oracle___NrNsNfNc__by_BassOvercast.jpg")); //coll.Add(AddPicture("http://fc05.deviantart.net/fs70/i/2010/132/5/4/CHILI__S_BROCHURE_by_kungfuat.jpg")); //coll.Add(AddPicture("http://fc04.deviantart.net/fs17/f/2007/202/f/a/Harry_Potter___Nostalgia_by_kurot.jpg")); //coll.Add(AddPicture("http://fc07.deviantart.net/fs22/f/2007/337/5/7/578e0cea2cfa380e.jpg")); //coll.Add(AddPicture("http://fc05.deviantart.net/fs71/f/2010/091/b/2/Princess_Mononoke_by_syncaidia.jpg")); //coll.Add(AddPicture("http://fc09.deviantart.net/fs71/f/2010/062/6/b/__Naruto___Open_Your_Eyes___by_orin.jpg")); //coll.Add(AddPicture("http://fc04.deviantart.net/fs27/f/2008/144/8/7/87002714fcfb3cad81b29e950c6fc1cd.jpg")); //coll.Add(AddPicture("http://fc06.deviantart.net/fs51/f/2009/322/4/e/4e52300ac447c9619eb6e010d363a8ac.jpg")); //coll.Add(AddPicture("http://fc03.deviantart.net/fs17/f/2007/176/b/a/Forgotten_Fairytales_by_zemotion.jpg")); //coll.Add(AddPicture("http://fc00.deviantart.net/fs31/f/2008/190/e/d/rays_of_reflection_by_ssilence.jpg")); //coll.Add(AddPicture("http://fc09.deviantart.net/fs31/i/2008/233/3/d/to_the_end_of_the_world_by_foureyes.jpg")); //coll.Add(AddPicture("http://fc07.deviantart.net/fs31/i/2008/233/5/5/li_river_by_foureyes.jpg")); //coll.Add(AddPicture("http://fc05.deviantart.net/fs33/f/2008/301/5/a/5a09315ee42ac66a4f6e86c69d255544.jpg")); foreach (var item in coll) { //listbox内的image Image nimg = new Image(); string[] URI = ((BitmapImage)item.Href).UriSource.AbsoluteUri.Split('$');//把获取到的uri进行处理。地址和图片名称分开 ToolTipService.SetToolTip(nimg, URI[1]); //给image设置ToolTip nimg.Height = 58; nimg.Source = new BitmapImage(new Uri(URI[0], UriKind.RelativeOrAbsolute));//根据绝对地址给image source Preview.Source = new BitmapImage(new Uri(URI[0], UriKind.RelativeOrAbsolute)); ;//大图默认显示一张,为了布局好看 nimg.Cursor = Cursors.Hand; lbImage.Items.Add(nimg); } stp1.Children.Add(Preview); stp1.Children.Add(lbImage); //lbImage.ItemsSource = coll; } private Picture AddPicture(string path) { return new Picture { Href = new BitmapImage( new Uri( path, UriKind.Absolute)) }; } Image Preview = new Image();//点击小图片显示大图片的image private void lbImage_SelectionChanged(object sender, SelectionChangedEventArgs e) { var imlis = stp1.Children.Where(u => u is Image); foreach (var item in imlis) { stp1.Children.Remove(item); break; } ListBox getdource = sender as ListBox;//获取点击的对象 Preview.Width = 350; Preview.Height = 230; Preview.Stretch = Stretch.Fill; Preview.HorizontalAlignment = System.Windows.HorizontalAlignment.Center; Preview.Source = (getdource.SelectedItem as Image).Source;//获取listbox中image对象 stp1.Children.Insert(0, Preview);//控制位置在上面 } #endregion #region =======视频点击播放========== /// <summary> /// 视频点击按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> MediaElement mediaElement1 = new MediaElement(); void showSP_Click(object sender, RoutedEventArgs e) { stp1.Children.Clear(); mediaElement1.MouseLeftButtonDown += new MouseButtonEventHandler(mediaElement1_MouseLeftButtonDown); //mediaElement1.Source = new Uri("http://192.168.3.222/FWTP/2.wmv", UriKind.RelativeOrAbsolute); // mediaElement1.Source = new Uri("http://192.168.3.222/FWTP/1.flv", UriKind.RelativeOrAbsolute); //文件地址 if (!String.IsNullOrEmpty(_fwjcxxData.SPMC)) { string Url = String.Format(Config.Instance.HousePictureAddress, _fwjcxxData.SPMC);//获取视频的地址 Uri fileUri = new Uri(Url, UriKind.Absolute); HttpWebRequest request = WebRequest.Create(fileUri) as HttpWebRequest; request.Method = "GET"; request.BeginGetResponse(new AsyncCallback(ResponseCallback), request); mediaElement1.Width = 350; mediaElement1.Height = 320; mediaElement1.Stretch = Stretch.Fill; mediaElement1.AutoPlay = true; mediaElement1.Volume = 2; stp1.Children.Add(mediaElement1); //MessageBox.Show("此功能正在维护中。。。。。!"); } else { MessageBox.Show("暂无视频!"); } } private void ResponseCallback(IAsyncResult result) { // IAsyncResult.AsyncState - AsyncCallback 传过来的对象 HttpWebRequest request = result.AsyncState as HttpWebRequest; WebResponse response = null; try { // HttpWebRequest.EndGetResponse(IAsyncResult) - 结束对指定 URI 资源做异步请求 // 返回值为 WebResponse 对象 response = request.EndGetResponse(result) as HttpWebResponse; Stream responseStream = response.GetResponseStream(); MediaStreamSource mediaStreamSource = null; try { mediaStreamSource = new FlvMediaStreamSource(responseStream); //实行线程调用 Dispatcher.BeginInvoke(new Action<MediaStreamSource>((mStream) => { this.mediaElement1.SetSource(mStream); }), mediaStreamSource); } catch (Exception ex) { MessageBox.Show(ex.Message); } } catch (Exception exx) { MessageBox.Show(exx.Message); } } /// <summary> /// 视频停止 /// </summary> void mediaElement1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { mediaElement1.Stop(); } #endregion #region =========附件下载功能================ /// <summary> /// 附件点击按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void showFJ_Click(object sender, RoutedEventArgs e) { dj = "点击附件"; GetFJDatas(null); } /// <summary> /// 附件下载完毕 /// </summary> void DetailList_FJXZDatasLoadComplete() { //FrameworkElement element = null; stp1.Children.Clear(); if (dj == "点击附件") { string tpqz = Config.Instance.HousePictureAddress; if (FJXZDatas.Count > 0) { ListBox lid = new ListBox(); lid.Width = 340; lid.MaxHeight = 300; lid.BorderBrush = new SolidColorBrush(Colors.Transparent);//去除边框 lid.Background = new SolidColorBrush(Colors.Transparent);//背景透明 foreach (var fjxx in FJXZDatas) { string DZ = String.Format(tpqz, fjxx.FJQM); //获取图片的地址 HyperlinkButton item = new HyperlinkButton(); item.Click += new RoutedEventHandler(item_Click); item.NavigateUri = new Uri(DZ, UriKind.RelativeOrAbsolute); item.FontSize = 13; // item.TargetName = "_bank"; item.Content = fjxx.FJMC;//HyperlinkButton显示的名字 lid.Items.Add(item); } stp1.Children.Add(lid); } } if (dj == "点击图片") { LoadImages(); } } /// <summary> /// 点击HyperlinkButton后实现下载功能 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> void item_Click(object sender, RoutedEventArgs e) { HyperlinkButton button = sender as HyperlinkButton;//获取按钮对象 //_file 要下载的文件路径 // string _file = "http://192.168.3.222/FWTP/1.flv"; //这里改成你网站里的文件路径 string _file = button.NavigateUri.ToString();//获取文件下载的路径 string Hz = _file.Substring(_file.Length - 4, 4).ToLower();//获取后缀,转换为小写的 SaveFileDialog sf = new SaveFileDialog(); sf.DefaultExt = Hz; sf.Filter = "jpg图像文件 (*.jpg)|*.jpg|flv格式视频 (*.flv)|*.flv|所有文件 (*.*)|*.*"; switch (Hz)//判断为哪个类型的文件,并默认是哪个。 { case ".jpg": sf.FilterIndex = 1; break; case ".flv": sf.FilterIndex = 2; break; default: break; } //sf.SafeFileName = "DefaultImg"; if (sf.ShowDialog() != true) return; Stream clientStream = sf.OpenFile(); Uri _uri = new Uri(_file, UriKind.Absolute); WebClient client = new WebClient(); if (client.IsBusy) { client.CancelAsync(); } client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted); client.OpenReadAsync(_uri, clientStream); } #region 异步文件下载 void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) { if (e.Error != null) { //txtinfo.Text += e.Error.Message; //MessageBox.Show(e.Error.ToString()); MessageBox.Show("下载文件时出错,可能是文件已经不存在!"); return; } if (e.Cancelled != true) { Stream clientStream = e.UserState as Stream; Stream serverStream = (Stream)e.Result; byte[] buffer = new byte[serverStream.Length]; serverStream.Read(buffer, 0, buffer.Length); clientStream.Write(buffer, 0, buffer.Length); clientStream.Close(); serverStream.Close(); MessageBox.Show("下载成功!"); } } #endregion /// <summary> /// 根据zcid和cqid进行查找附件 /// </summary> public void GetFJDatas(string str) { ServiceClient sc = new ServiceClient(); sc.GetDataSetStringCompleted += (glps, glpe) => { //通常在设计器加载时候回去执行构造函数代码, //如果在构造行数里有wcf服务请求,则会执行这 //个请求,但是这个请求肯定是不成功的,这时候 //如果调用e.Result则会引发异常,导致设计器 //不可视. try { // 如果返回的结果为空,什么都不做 if (string.IsNullOrEmpty(glpe.Result)) { return; } // 创建Silverlight DataSet从xml字符串 DataSet dataSet = new DataSet(); dataSet.FromXml(glpe.Result); //将dataset转换为ObservableCollection FJXZDatas = FJXZWraper.CreateObjectObservableCollection(dataSet); //标记已加载 FJXZDatasCOMP = true; OnFJXZDatasLoadComplete(); } catch (Exception e) { LoadingWindow.HideWaiting(); MessageBox.Show("数据异常:" + e.Message.ToString()); Debug.WriteLine(e.InnerException); Debug.WriteLine(e.Message); Debug.WriteLine(e.StackTrace); } }; string sql = @"select FJ_CQ_ID,ZCBM,FWZC_ID,FWBM,FWCQ_ID,FJMC,FJHZ,FJQM,FJPX, FJLX,FJZL,BZ,BY1,BY2,BY3,BY4,RQ from t_fj_cq where fwzc_id ='{0}' and fwcq_id = '{1}' " + str + ""; sql = String.Format(sql, _fwjcxxData.fwzc_id, _fwjcxxData.fwcq_id); sc.GetDataSetStringAsync(sql); } #endregion
附件 :
视频:
图片: