2012-11-21 15 views
7

मैं .NET Winform में एनिमेटेड gif दिखाना चाहता हूं। यह कैसे करें?क्या एक पिक्चरबॉक्स विंडोज एप्लिकेशन में एनिमेटेड जीआईएफ दिखा सकता है?

मैंने पहले वीबी 6.0 का उपयोग किया था। बहुत बहुत धन्यवाद।

+0

एनिमेटेड छवि के लिए आप इस नियंत्रक का उपयोग कर सकते हैं। http://www.codeproject.com/Tips/1004624/Gif-viewer-Snipper-control – xwpedram

उत्तर

16
  1. एक फॉर्म पर एक चित्र बॉक्स डालें और फिर एक GIF एक्सटेंशन के साथ एक चित्र फ़ाइल निर्दिष्ट करें। या:

  2. प्रोग्राम के कोड के साथ एक PictureBox में एक gif छवि लोड हो रहा है फ्रेम चेतन, यहाँ Gif वर्ग है:

VB.NET

Public Class GifImage 
    Private gifImage As Image 
    Private dimension As FrameDimension 
    Private frameCount As Integer 
    Private currentFrame As Integer = -1 
    Private reverse As Boolean 
    Private [step] As Integer = 1 

    Public Sub New(path As String) 
     gifImage = Image.FromFile(path) 
     'initialize 
     dimension = New FrameDimension(gifImage.FrameDimensionsList(0)) 
     'gets the GUID 
      'total frames in the animation 
     frameCount = gifImage.GetFrameCount(dimension) 
    End Sub 

    Public Property ReverseAtEnd() As Boolean 
     'whether the gif should play backwards when it reaches the end 
     Get 
      Return reverse 
     End Get 
     Set 
      reverse = value 
     End Set 
    End Property 

    Public Function GetNextFrame() As Image 

     currentFrame += [step] 

     'if the animation reaches a boundary... 
     If currentFrame >= frameCount OrElse currentFrame < 1 Then 
      If reverse Then 
       [step] *= -1 
       '...reverse the count 
        'apply it 
       currentFrame += [step] 
      Else 
       currentFrame = 0 
       '...or start over 
      End If 
     End If 
     Return GetFrame(currentFrame) 
    End Function 

    Public Function GetFrame(index As Integer) As Image 
     gifImage.SelectActiveFrame(dimension, index) 
     'find the frame 
     Return DirectCast(gifImage.Clone(), Image) 
     'return a copy of it 
    End Function 
End Class 

सी #

public class GifImage 
{ 
    private Image gifImage; 
    private FrameDimension dimension; 
    private int frameCount; 
    private int currentFrame = -1; 
    private bool reverse; 
    private int step = 1; 

    public GifImage(string path) 
    { 
     gifImage = Image.FromFile(path); 
     //initialize 
     dimension = new FrameDimension(gifImage.FrameDimensionsList[0]); 
     //gets the GUID 
     //total frames in the animation 
     frameCount = gifImage.GetFrameCount(dimension); 
    } 

    public bool ReverseAtEnd { 
     //whether the gif should play backwards when it reaches the end 
     get { return reverse; } 
     set { reverse = value; } 
    } 

    public Image GetNextFrame() 
    { 

     currentFrame += step; 

     //if the animation reaches a boundary... 
     if (currentFrame >= frameCount || currentFrame < 1) { 
      if (reverse) { 
       step *= -1; 
       //...reverse the count 
       //apply it 
       currentFrame += step; 
      } 
      else { 
       currentFrame = 0; 
       //...or start over 
      } 
     } 
     return GetFrame(currentFrame); 
    } 

    public Image GetFrame(int index) 
    { 
     gifImage.SelectActiveFrame(dimension, index); 
     //find the frame 
     return (Image)gifImage.Clone(); 
     //return a copy of it 
    } 
} 

सी # उपयोग:

खुला एक Winform परियोजना एक खींचें और एक PictureBox, एक टाइमर और एक बटन में गिरावट, GifImage.cs वर्ग ऊपर दिखाए गए।

public partial class Form1 : Form 
{ 
    private GifImage gifImage = null; 
    private string filePath = @"C:\Users\Jeremy\Desktop\ExampleAnimation.gif"; 

    public Form1() 
    { 
     InitializeComponent(); 
     //a) Normal way 
     //pictureBox1.Image = Image.FromFile(filePath); 

     //b) We control the animation 
     gifImage = new GifImage(filePath); 
     gifImage.ReverseAtEnd = false; //dont reverse at end 
    } 

    private void button1_Click(object sender, EventArgs e) 
    { 
     //Start the time/animation 
     timer1.Enabled = true; 
    } 

    //The event that is animating the Frames 
    private void timer1_Tick(object sender, EventArgs e) 
    { 
     pictureBox1.Image = gifImage.GetNextFrame(); 
    } 
} 

enter image description here

+0

क्या आप मुझे बता सकते हैं कि स्निपेट का उपयोग कैसे करें? मेरा मतलब है कि उस वर्ग का उपयोग कर एक पिक्चरबॉक्स में एक तस्वीर कैसे लोड करें, मुझे उस कोड का उपयोग करने का तरीका नहीं मिल रहा है, लेकिन दिलचस्प है ... – ElektroStudios

+0

क्या समस्या है? –

+0

उदाहरण के लिए: डिम asdasd नई GifImage = "सी: \ छवि के रूप में।gif " PictureBox1.Image = GifImage (" C: \ image.gif ") समस्या यह है कि मुझे नहीं पता कि इसका उपयोग कैसे करें, क्षमा करें, मैं थोड़ी नौसिखिया हूं, अगर आप मुझे समझा सकते हैं ... I इसे मैन्युअल रूप से लोड करना चाहते हैं – ElektroStudios

9

@ JeremyThompson के जवाब पर विकास मैं एक कोड स्निपेट शामिल करने को दिखाने के लिए कि कैसे आप पहली दृष्टिकोण को लागू कर सकते चाहते हैं, क्योंकि यह बहुत आसान है, और मैन्युअल रूप से चेतन करने के लिए आप की आवश्यकता नहीं है gif, यह देखते हुए कि PictureBox में ऐसी परिदृश्य को संभालने के लिए एक अंतर्निहित सुविधा है। बस मेरी राय में अपने फार्म के लिए एक PictureBox जोड़ने के लिए, और प्रपत्र निर्माता में करने के लिए PictureBox.ImageLocation

सी #

public PictureForm() 
{ 
     InitializeComponent(); 
     pictureBoxGif.ImageLocation = "C:\\throbber.gif"; 
} 

छवि पथ आवंटित VB.Net

Public Sub New() 
    InitializeComponent() 
    pictureBoxGif.ImageLocation = "C:\throbber.gif" 
End Sub 

यह एक बहुत है सरल समाधान, खासकर किसी ऐसे व्यक्ति के लिए जो .NET के लिए नया है।

+2

खेल के लिए +1, ** अगर वीबी 6 ** में केवल चीजें ही आसान थीं। 'नेट की खुशी', वास्तव में, जैसा कि मैं बढ़ता रहता हूं, मुझे लगता है कि एक प्रबंधित ढांचा कितना बेहतर है !!! –

+0

+1 आपके समाधान के लिए प्रासंगिकता प्रदान करने के लिए, सरल और सही। कृपया ध्यान दें कि चित्र बॉक्स (या फॉर्म) को अक्षम करने से gif की एनीमेशन अवरुद्ध हो जाएगी! – Marco

1

मैंने इसके साथ खेला है और एनीमेशन नाटकों प्रदान करता है कि आप एक ही थ्रेड पर एक और लंबा चलने वाला ऑपरेशन नहीं करते हैं। जिस पल में आप एक और लंबे समय तक चल रहे ऑपरेशन करते हैं, आप इसे किसी अन्य थ्रेड में करना चाहते हैं।

ऐसा करने का सबसे आसान तरीका पृष्ठभूमिवर्कर घटक का उपयोग करना है जिसे आप अपने टूलबॉक्स से फ़ॉर्म पर खींच सकते हैं। फिर आप BackgroundWorker के DoWork() ईवेंट में अपना लंबा चलने वाला ऑपरेशन कोड डाल देंगे। अंतिम चरण पृष्ठभूमिवॉर्कर उदाहरण के RunWorkerAsync() विधि को कॉल करके अपने कोड को आमंत्रित करना होगा।

संबंधित मुद्दे