using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Drawing; using System.IO; namespace AnimationTime { class AnimatedPicture : IDisposable { public Image ImageToAnimate; public Rectangle CurrentCell; public Rectangle SizeRect; public int CellSize; private int CurrentFrame; private int TotalFrames; private MemoryStream FileData; public int Top { set { SizeRect.Y = value; } } public int Left { set { SizeRect.X = value; } } public AnimatedPicture(string ImageFile) { using (FileStream Read = new FileStream(ImageFile, FileMode.Open, FileAccess.Read, FileShare.Read)) { // Read the source file into a byte array. byte[] bytes = new byte[Read.Length]; int numBytesToRead = (int)Read.Length; int numBytesRead = 0; while (numBytesToRead > 0) { // Read may return anything from 0 to numBytesToRead. int n = Read.Read(bytes, numBytesRead, numBytesToRead); // Break when the end of the file is reached. if (n == 0) break; numBytesRead += n; numBytesToRead -= n; } // Wrap the byte array in a memory stream, so the image class can load it. FileData = new MemoryStream(bytes); // Close the file, so editing programs can save to it. Read.Close(); } ImageToAnimate = Image.FromStream(FileData); CellSize = ImageToAnimate.Height; CurrentCell = new Rectangle(0, 0, CellSize, CellSize); SizeRect = new Rectangle(0, 0, CellSize, CellSize); CurrentFrame = 0; TotalFrames = ImageToAnimate.Width / CellSize; } public void Update() { // Prepare for the next render CurrentFrame++; if (CurrentFrame >= TotalFrames) CurrentFrame = 0; CurrentCell.X = CurrentFrame * CellSize; } public void Dispose() { // Call dispose on resources we are done with. ImageToAnimate.Dispose(); FileData.Dispose(); } } }