zoukankan      html  css  js  c++  java
  • C#图片动画效果(旋转360度)异步

      private void Button1_Click(object sender, EventArgs e)
            {
             
                
                Graphics graphics = this.CreateGraphics();
                graphics.Clear(Color.White);
    
                //装入图片 资源
    
                //Bitmap image = new Bitmap(WindowsFormsApplication1.Properties.Resources.image_101);
                Bitmap image = new Bitmap(HZH_Controls.Properties.Resources.alarm);
    
                //获取当前窗口的中心点
    
                Rectangle rect = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                PointF center = new PointF(rect.Width / 2, rect.Height / 2);
    
                float offsetX = 0;
                float offsetY = 0;
                offsetX = center.X - image.Width / 2;
                offsetY = center.Y - image.Height / 2;
                //构造图片显示区域:让图片的中心点与窗口的中心点一致
                RectangleF picRect = new RectangleF(offsetX, offsetY, image.Width, image.Height);
                PointF Pcenter = new PointF(picRect.X + picRect.Width / 2,
                    picRect.Y + picRect.Height / 2);
                Color c = Color.FromArgb(200, 200, 200);
    
    
                //让图片绕中心旋转一周
                for (int i = 360; i > 0; i -= 10)
                {
                    // 绘图平面以图片的中心点旋转
                    graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
    
    
                    graphics.RotateTransform(i);
                    //恢复绘图平面在水平和垂直方向的平移
                    graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
                    //绘制图片并延时
                    graphics.DrawImage(image, picRect);
                    Thread.Sleep(100);
                    graphics.Clear(c);
    
                    //重置绘图平面的所有变换
                    graphics.ResetTransform();
                }
            }

     以上代码是同步的,用户体验不好。

    下面是经过改善的异步方法

      private void Button1_Click(object sender, EventArgs e)
            {
                Graphics graphics = this.CreateGraphics();
                graphics.Clear(Color.White);
    
                //装入图片 资源
    
                //Bitmap image = new Bitmap(WindowsFormsApplication1.Properties.Resources.image_101);
                Bitmap image = new Bitmap(HZH_Controls.Properties.Resources.alarm);
    
                //获取当前窗口的中心点
    
                Rectangle rect = new Rectangle(0, 0, this.ClientSize.Width, this.ClientSize.Height);
                PointF center = new PointF(rect.Width / 2, rect.Height / 2);
    
                float offsetX = 0;
                float offsetY = 0;
                offsetX = center.X - image.Width / 2;
                offsetY = center.Y - image.Height / 2;
                //构造图片显示区域:让图片的中心点与窗口的中心点一致
                RectangleF picRect = new RectangleF(offsetX, offsetY, image.Width, image.Height);
                PointF Pcenter = new PointF(picRect.X + picRect.Width / 2,
                    picRect.Y + picRect.Height / 2);
    
                Action<Graphics, PointF, Bitmap, RectangleF> action = test;//声明委托
                action.BeginInvoke(graphics, Pcenter, image, picRect,null,null);//异步调用
            }
            static void test(Graphics graphics,PointF Pcenter,Bitmap image, RectangleF picRect) {
                //让图片绕中心旋转一周
                Color c = Color.FromArgb(200, 200, 200);
                for (int i = 360; i > 0; i -= 10)
                {
                    // 绘图平面以图片的中心点旋转
                    graphics.TranslateTransform(Pcenter.X, Pcenter.Y);
    
    
                    graphics.RotateTransform(i);
                    //恢复绘图平面在水平和垂直方向的平移
                    graphics.TranslateTransform(-Pcenter.X, -Pcenter.Y);
                    //绘制图片并延时
                    graphics.DrawImage(image, picRect);
                    Thread.Sleep(100);
                    graphics.Clear(c);
    
                    //重置绘图平面的所有变换
                    graphics.ResetTransform();
                }
            }
  • 相关阅读:
    代码阅读之术一:结构与源流
    linux cat /etc/passwd 说明
    Linux软连接
    文件上传文件的权限--lnmp 环境配置,尤其整个项目复制过来
    Redis面试总结
    php上传文件与图片到七牛的实例详解
    在浏览器中打开php文件时,是Linux中的哪个用户执行的?
    Laravel [1045] 解决方法 Access denied for user 'homestead'@'localhost'
    linux中快速清空文件内容的几种方法
    linux如何查看所有的用户和组信息?
  • 原文地址:https://www.cnblogs.com/topsyuan/p/11589030.html
Copyright © 2011-2022 走看看