C#图片动画效果(旋转360度)异步

摘要:
privatevoidButton1_Click(objectsender,EventArgse){Graphicsgraphics=this.CreateGraphics();graphics.Clear(Color.White);//加载图像资源//Bitmapimage=newBitmap(WindowsFormsApplication1.Properties.resources.image
  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();
            }
        }

免责声明:文章转载自《C#图片动画效果(旋转360度)异步》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇剖析IE浏览器子系统的性能权重SpringBoot 数据篇之使用JDBC下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

IOS--CALayer实现,界限、透明度、位置、旋转、缩放组合动画(转)

首先引入框架:QuartzCore.framework在头文件声明:CALayer *logoLayer{//界限CABasicAnimation *boundsAnimation = [CABasicAnimationanimationWithKeyPath:@"bounds"];boundsAnimation.fromValue = [NSValue ...

三维坐标下的旋转

  三维坐标的旋转有以下几种常见的表示形式:旋转向量,旋转矩阵,欧拉角,四元数,下面对这四种表示形式及其之间的转换进行介绍 旋转向量   通常为3x1的列向量,向量方向即为旋转轴,向量的模表示绕轴逆时针旋转的角度,如旋转向量v=[a,b,c],那么旋转角度theta=sqrt(a2+b2+c2),旋转轴z=[a/theta, b/theta, c/thet...

Android Glide加载视频封面

/** *   context 上下文 *   uri 视频地址 *   imageView 设置image *   frameTimeMicros 获取某一时间帧 */ public void loadVideoScreenshot(final Context context, String ur...

C# Winform 窗体美化

1 using System; 2 using System.ComponentModel; 3 using System.Diagnostics; 4 using System.Drawing; 5 using System.Drawing.Drawing2D; 6 using System.Runtime.InteropServ...

Android利用canvas画各种图形(点、直线、弧、圆、椭圆、文字、矩形、多边形、曲线、圆角矩形)

1、首先说一下canvas类: Class OverviewThe Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls...

Android高斯模糊实现方案

1、使用Glide Glide.with(this) .load(service.getImageUri()) .dontAnimate() .error(R.drawable.error_img)...