다음을 통해 공유


ImageAnimator.Animate(Image, EventHandler) 메서드

정의

프레임이 여러 개인 이미지를 애니메이션으로 표시합니다.

public:
 static void Animate(System::Drawing::Image ^ image, EventHandler ^ onFrameChangedHandler);
public static void Animate (System.Drawing.Image image, EventHandler onFrameChangedHandler);
static member Animate : System.Drawing.Image * EventHandler -> unit
Public Shared Sub Animate (image As Image, onFrameChangedHandler As EventHandler)

매개 변수

image
Image

애니메이션 효과를 줄 Image 개체입니다.

onFrameChangedHandler
EventHandler

애니메이션 프레임이 변경될 때 호출하는 방법을 지정하는 EventHandler 개체입니다.

예제

이 Windows Forms 애플리케이션에 화면에 애니메이션이 적용 된 이미지를 그리는 방법을 보여 줍니다. 이미지는 SampleAnimation.gif 애플리케이션과 같은 폴더에 있는 애니메이션된 GIF 파일에서 만들어집니다.

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>

using namespace System;
using namespace System::Drawing;
using namespace System::Windows::Forms;

public ref class AnimateImageForm : public Form 
{

    // Create a Bitmap Object.
private:
    Bitmap^ animatedImage;
private:
    bool currentlyAnimating;

public:
    AnimateImageForm()
    {
        try
        {
            animatedImage = gcnew Bitmap("SampleAnimation.gif");
        }
        catch (ArgumentException^)
        {
            MessageBox::Show("Could not read the image file " +
                "\"SampleAnimation.gif\",\n" +
                "or it is not a valid image file.");
        }
    }

    // This method begins the animation.
public:
    void AnimateImage() 
    {
        // Begin the animation only once.
        // Make sure to animate only if animatedImage was
        // successfully initialised.
        if (!currentlyAnimating && animatedImage != nullptr)
        {
            ImageAnimator::Animate(animatedImage,
                gcnew EventHandler(this, &AnimateImageForm::OnFrameChanged));
            currentlyAnimating = true;
        }
    }

private:
    void OnFrameChanged(Object^ , EventArgs^ ) 
    {
        // Force a call to the Paint event handler.
        this->Invalidate();
    }

protected:
    virtual void OnPaint(PaintEventArgs^ e) override
    {
        // Begin the animation.
        AnimateImage();

        // Get the next frame ready for rendering.
        ImageAnimator::UpdateFrames();

        if (animatedImage != nullptr)
        {
            // Draw the next frame in the animation.
            e->Graphics->DrawImage(this->animatedImage,
                Point(0, 0));
        }
    }
};

[STAThread]
int main()
{
    Application::Run(gcnew AnimateImageForm());
}
using System;
using System.Drawing;
using System.Windows.Forms;

public class animateImage : Form 
{
                     
    //Create a Bitmpap Object.
    Bitmap animatedImage = new Bitmap("SampleAnimation.gif");
    bool currentlyAnimating = false;
                     
    //This method begins the animation.
    public void AnimateImage() 
    {
        if (!currentlyAnimating) 
        {
                     
            //Begin the animation only once.
            ImageAnimator.Animate(animatedImage, new EventHandler(this.OnFrameChanged));
            currentlyAnimating = true;
        }
    }

    private void OnFrameChanged(object o, EventArgs e) 
    {
                     
        //Force a call to the Paint event handler.
        this.Invalidate();
    }

    protected override void OnPaint(PaintEventArgs e) 
    {
                     
        //Begin the animation.
        AnimateImage();
                     
        //Get the next frame ready for rendering.
        ImageAnimator.UpdateFrames();
                     
        //Draw the next frame in the animation.
        e.Graphics.DrawImage(this.animatedImage, new Point(0, 0));
    }

    public static void Main() 
    {
        Application.Run(new animateImage());
    }
}
Imports System.Drawing
Imports System.Windows.Forms

Public Class animateImage
    Inherits Form

    'Create a Bitmpap Object.
    Private animatedImage As New Bitmap("SampleAnimation.gif")
    Private currentlyAnimating As Boolean = False

    'This method begins the animation.
    Public Sub AnimateImage()
        If Not currentlyAnimating Then

            'Begin the animation only once.
            ImageAnimator.Animate(animatedImage, _
            New EventHandler(AddressOf Me.OnFrameChanged))
            currentlyAnimating = True
        End If
    End Sub

    Private Sub OnFrameChanged(ByVal o As Object, ByVal e As EventArgs)

        'Force a call to the Paint event handler.
        Me.Invalidate()
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)

        'Begin the animation.
        AnimateImage()

        'Get the next frame ready for rendering.
        ImageAnimator.UpdateFrames()

        'Draw the next frame in the animation.
        e.Graphics.DrawImage(Me.animatedImage, New Point(0, 0))
    End Sub

    Public Shared Sub Main()
        Application.Run(New AnimateImage)
    End Sub
End Class

설명

참고

.NET 6 이상 버전에서는 이 형식을 포함하는 System.Drawing.Common 패키지가 Windows 운영 체제에서만 지원됩니다. 플랫폼 간 앱에서 이 형식을 사용하면 컴파일 시간 경고 및 런타임 예외가 발생합니다. 자세한 내용은 Windows에서만 지원되는 System.Drawing.Common을 참조하세요.

적용 대상