/// <summary> /// Delete the image and keep the frame data /// </summary> public void DisposeImage() { // do nothing if the frame is already null if (m_image == null || m_image.Disposed) { return; } // delete the image m_image.Dispose(); m_image = null; }
/// <summary> /// Free the memory from the frames images /// </summary> public void DisposeFrames() { // scan all the frames for (int i = 0; i < m_Frames.Count; i++) { // delete the image m_Frames[i].DisposeImage(); } // remove the spritesheet if (m_spriteSheet != null && !m_spriteSheet.Disposed) { m_spriteSheet.Dispose(); m_spriteSheet = null; } }
/// <summary> /// Create the sprite sheet for the current animation /// </summary> /// <param name="h">Hue color to use</param> /// <param name="width">Width to use for the frame cell</param> /// <param name="height">Height to use for the frame cell</param> public void CreateSpriteSheet(Hue h = null, int width = 0, int height = 0) { // calculate the final spritesheet size int spriteSheetW = Math.Max(CellWidth, width) * FramesPerDirection; int spriteSheetH = Math.Max(CellHeight, height) * 5; // create the empty sprite sheet m_spriteSheet = new DirectBitmap(spriteSheetW, spriteSheetH); // current x/y coordinates in the spritesheet int currX = 0; int currY = 0; // current frame int currFrame = 0; // scan all the frames for (int i = 0; i < m_Frames.Count; i++) { // create the frame with the animation (or forced) size DirectBitmap realFrame = CreateRealFrameImage(i, h, width, height); // add the frame to the sheet using (Graphics g = Graphics.FromImage(m_spriteSheet.Bitmap)) g.DrawImage(realFrame.Bitmap, currX, currY, Math.Max(CellWidth, width), Math.Max(CellHeight, height)); // increase the frame counter currFrame++; // move in position for the next frame if (currFrame >= FramesPerDirection) { currX = 0; currY += Math.Max(CellHeight, height); currFrame = 0; } else { currX += Math.Max(CellWidth, width); } // delete the current frame images that we don't use m_Frames[i].DisposeImage(); realFrame.Dispose(); // prevent the app from freezing Application.DoEvents(); } }
/// <summary> /// Protected implementation of Dispose pattern. /// </summary> /// <param name="disposing"></param> protected virtual void Dispose(bool disposing) { // has the frame already been disposed? if (disposed) { return; } // are we disposing of this frame? if (disposing) { // dispose of the memory used by the frame handle.Dispose(); // destroy the image if (m_image != null && !m_image.Disposed) { m_image.Dispose(); } // nullify the image m_image = null; // destroy the original image backup if (m_OriginalImage != null && !m_OriginalImage.Disposed) { m_OriginalImage.Dispose(); } // nullify the original image backup m_OriginalImage = null; } // flag the frame as disposed disposed = true; }