在GDI +中制作动画的最佳方法是什么? [英] What is the best way to make animation in GDI+?

查看:487
本文介绍了在GDI +中制作动画的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为是:

1.以字节存储。

2.创建计时器。

3.使用Marshal.Copy通过计时器间隔复制字节来制作动画。



我猜对了,可能有更好的方法吗?

我说的是Windows窗体中的GDI +,而不是WPF,3d派对库或平台调用。

I think it is:
1. Store in bytes.
2. Create timer.
3. Use Marshal.Copy to copy bytes by timer interval to make animation.

I just guessed it, may be there is a better way?
I am talking about GDI+ in Windows Forms, not WPF, 3d party libraries or Platform Invoke.

推荐答案

在我对解决方案的评论中简要描述了解决方案。

链接到我过去的答案:

如何做图像动画 [ ^ ],

任何在运行时更改图片的方法动画时间 [ ^ ],

什么样的俏皮方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

如何加速我的vb.net应用程序? [ ^ ],

在面板上捕获图纸 [ ^ ],

mdi子窗体之间的绘制线 [ ^ ]。



-SA
The solution is briefly described in my comment to your "Solution".
Links to my past answers:
how to do image animation[^],
any way to change a picture at run time with animation[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
How to speed up my vb.net application?[^],
capture the drawing on a panel[^],
Drawing Lines between mdi child forms[^].

—SA


代码:

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Threading;
using System.Diagnostics;

namespace _123
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            bitmap = new Bitmap(500, 500);
            g = Graphics.FromImage(bitmap);
            stopwatch = new Stopwatch();

            this.DoubleBuffered = true;
            this.Paint += Draw;

            stopwatch.Start();
            (new Thread(new ThreadStart(Drawing_Thread))).Start();
        }

        Stopwatch stopwatch;

        Bitmap bitmap;
        Graphics g;

        int x = 20;
        int add = 1;

        private void Drawing_Thread()
        {
            while (true)
                if (stopwatch.Elapsed.Milliseconds >= 1)
                {
                    stopwatch.Stop();
                    if (x == 300) add *= -1;
                    if (x == 0) add *= -1;
                    g.Clear(Color.Transparent);
                    g.FillRectangle(Brushes.Red, x += add, 50, 100, 100);

                    this.Invoke(new Action(this.Invalidate));
                    stopwatch.Restart();
                }
        }

        private void Draw(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawImage(bitmap, 0, 0);
        }
    }
}


这篇关于在GDI +中制作动画的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆