博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

silverlight显示xml文件中的图片

Posted on 2008-05-29 15:00  cuit  阅读(1087)  评论(1编辑  收藏  举报

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using System.Net;
using System.Windows.Threading;
using System.Xml.Linq;
 
namespace SilverlightEnterCreditList
{
    public partial class slXml : UserControl
    {
        Point mousePoint;
        bool trackingMouseMove = false;
        FrameworkElement _element = null;
        public slXml()
        {
            InitializeComponent();
            this.Loaded += slXml_Loaded;
        }

        void slXml_Loaded(object sender, RoutedEventArgs e)
        {
            xDocTest();
        }
        void xDocTest()
        {

            XElement root = XElement.Load("data.xml");

            var q = from p in root.Elements("ENT_EMPLOYEE")
                    where p.Element("FPImage")!= null && p.Element("FPImage").Value!=string.Empty
                    select p;
            int left = 180;
            int mx = 150;
            foreach (XElement xe in q)
            {
                Byte[] bytes = System.Convert.FromBase64String(xe.Element("FPImage").Value);
                System.IO.Stream stream = new System.IO.MemoryStream(bytes);
                System.Windows.Media.Imaging.BitmapImage map = new System.Windows.Media.Imaging.BitmapImage();
                map.SetSource(stream); 
                Image img = new Image();
               
                
                img.Width = 100;
                img.Height = 100;
                img.Source = map;
                
                LayoutRoot.Children.Add(img);
                img.SetValue(Canvas.LeftProperty, left);
                img.SetValue(Canvas.TopProperty, 200);
               
                EllipseGeometry ellipes = new EllipseGeometry();
                ellipes.RadiusX = 60;
                ellipes.RadiusY = 60;
                ellipes.Center = new Point(60, 60);
                img.Clip = ellipes;
              
                left = left + mx;
                img.MouseLeftButtonDown += img_MouseLeftButtonDown;
                img.MouseMove += img_MouseMove;
                img.MouseLeftButtonUp += new MouseButtonEventHandler(img_MouseLeftButtonUp);
            }
           
}

        void img_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            FrameworkElement element = sender as FrameworkElement;
            trackingMouseMove = false;
            element.ReleaseMouseCapture();

            mousePoint.X = mousePoint.Y = 0;
            element.Cursor = null;
            _element = element;
            element.SetValue(Canvas.ZIndexProperty, 0);
        }
     
        void img_MouseMove(object sender, MouseEventArgs e)
        {
            Image img = sender as Image;
            if (trackingMouseMove)
            {
                double deltaV = e.GetPosition(null).Y - mousePoint.Y;
                double deltaH = e.GetPosition(null).X - mousePoint.X;
                double newTop = deltaV + (double)img.GetValue(Canvas.TopProperty);
                double newLeft = deltaH + (double)img.GetValue(Canvas.LeftProperty);

                img.SetValue(Canvas.TopProperty, newTop);
               img.SetValue(Canvas.LeftProperty, newLeft);

               

                //img.SetValue(Canvas.TopProperty, e.GetPosition(null).Y+mousePoint.Y);
                //img.SetValue(Canvas.LeftProperty, e.GetPosition(null).X + mousePoint.X);

                mousePoint = e.GetPosition(null);
            }
        }

        void img_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            mousePoint = e.GetPosition(null);
            Image img = sender as Image;
            trackingMouseMove = true;
            if (img != null)
            {
                img.CaptureMouse();
                img.Cursor = Cursors.Hand;
                img.SetValue(Canvas.ZIndexProperty, 1);
                //if(_element!=null)
                //_element.SetValue(Canvas.ZIndexProperty, 1);
            }
            
        }

        
    }
}

Xaml文件:
<UserControl x:Class="SilverlightEnterCreditList.slXml"
    xmlns="http://schemas.microsoft.com/client/2007"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"      >
     
    <Canvas x:Name="LayoutRoot" Background="Blue" >
       
        
    </Canvas>
</UserControl>