silverlight百叶窗

来源:互联网 发布:手拉鸡专卖网淘宝付款 编辑:程序博客网 时间:2024/04/26 08:11

 

刚学习Silverlight,想实现百叶窗等效果,找了好多资料,没有源码。

我就自己琢磨了一个方法,貌似Silverlight的动画也能实现,可惜我不会啊。

下面是我的方法:

 1ImageClone:复制原图像一块块区域到目标图像

public class ImageClone

    {

        WriteableBitmap gBitmapSoure;

        public WriteableBitmap gBitmapDest;

        public ImageClone(Image imageSoure, Image imageDest,int imageWitdth, int imageHeight)

        {

            gBitmapSoure = new WriteableBitmap(imageSoure, null);

            gBitmapDest = new WriteableBitmap(imageDest, null);

            Width = imageWitdth;

            Height = imageHeight;

        }

        private int width;

        private int height;

        public int Width

        {

            get { return width; }

            set { width = value; }

        }

        public int Height

        {

            get { return height; }

            set { height = value; }

        }

        public void Bitbit(int dX, int dY, int w, int h, int sX, int sY)

        {

            for (int th = dY, j = sY; th < h + dY && j < h + sY; th++, j++)

            {

                for (int tw = dX, i = sX; tw < w + dX && i < w + sX; tw++, i++)

                {

                    gBitmapDest.Pixels[th * width + tw] = gBitmapSoure.Pixels[j * width + i];

                }

            }

        }

2、调用ImageClone的方法实现百叶窗

        int i = 1;

        int count = 1;

        DispatcherTimer myDispatcherTimer;

        ImageClone imageClone;

        public MainPage()

        {

            InitializeComponent();

            ……….

            myDispatcherTimer = new DispatcherTimer();

        }

        private void btnOK_Click(object sender, RoutedEventArgs e)

        {

            string status = ((State)cobState.SelectedItem).Way.ToString();

            imageClone = new ImageClone(imgSoure, imgDest, 200, 130);

            StartTimer(10, status);

        }

        public void StartTimer(int millisecond, string status)

        {

            //创建间隔时

            myDispatcherTimer.Interval = new TimeSpan(0, 0, 0, 0, millisecond);

            //创建到达间隔时间后需执行的函 数

            switch (status)

            {

                case ":

                    myDispatcherTimer.Tick += new EventHandler(DownWindows_Tick);

                    break;

                ……….

            }

            //开启计时器

            myDispatcherTimer.Start();

        }

        public void DownWindows_Tick(object o, EventArgs sender)

        {

            for (int j = 0; j < imageClone.Height/10; j++)

            {

                imageClone.Bitbit(0, j * 10, imageClone.Width, count, 0, j * 10);

                imgDest.Source = imageClone.gBitmapDest;

            }

            count++;

            i++;

            if (i > 10 || count > imageClone.Height)

                myDispatcherTimer.Stop();

        }