/// <summary> /// Reads sprites from Game Maker project file. /// </summary> public static GMList <GMSprite> ReadSpritesGMX(string directory, ref List <string> assets) { // A list of sprites GMList <GMSprite> sprites = new GMList <GMSprite>(); sprites.AutoIncrementIds = false; // Iterate through .gmx files in the directory foreach (string file in Directory.GetFiles(directory, "*.gmx")) { // Set name of the sprite string name = GetResourceName(file); // If the file is not in the asset list, it has been orphaned, continue if (!assets.Contains(name)) { continue; } // Create a dictionary of sprite properties Dictionary <string, string> properties = new Dictionary <string, string>(); foreach (GMXSpriteProperty property in Enum.GetValues(typeof(GMXSpriteProperty))) { properties.Add(GMXEnumString(property), ""); } // Local variables and texture group strings List <GMImage> subImages = new List <GMImage>(); string textureGroup = GMXEnumString(GMXSpriteProperty.TextureGroup); string textureGroup0 = GMXEnumString(GMXSpriteProperty.TextureGroup0); // Create an xml reader using (XmlReader reader = XmlReader.Create(file)) { // Seek to content reader.MoveToContent(); // Read the GMX file while (reader.Read()) { // If the node is not an element, continue if (reader.NodeType != XmlNodeType.Element) { continue; } // Get the element name string nodeName = reader.Name; // Read element reader.Read(); // If the element value is null or empty, continue if (String.IsNullOrEmpty(reader.Value)) { continue; } // If the element is a frame element create subimage, else normal property if (nodeName.ToLower() == GMXEnumString(GMXSpriteProperty.Frame).ToLower()) { // Create a sub image and set the image path GMImage subImage = new GMImage(); subImage.Compressed = false; subImage.FilePath = reader.Value; subImage.Data = GMUtilities.LoadBytesFromBitmap(directory + "\\" + subImage.FilePath); subImages.Add(subImage); } else { // Set the property value properties[nodeName] = reader.Value; } } } // Create a new sprite, set properties GMSprite sprite = new GMSprite(); sprite.Id = GetIdFromName(name); sprite.Name = name; sprite.OriginX = GMXInt(properties[GMXEnumString(GMXSpriteProperty.XOrigin)], sprite.OriginX); sprite.OriginY = GMXInt(properties[GMXEnumString(GMXSpriteProperty.YOrigin)], sprite.OriginY); sprite.ShapeMode = GMXInt(properties[GMXEnumString(GMXSpriteProperty.ColKind)], sprite.ShapeMode); sprite.AlphaTolerance = GMXInt(properties[GMXEnumString(GMXSpriteProperty.ColTolerance)], sprite.AlphaTolerance); sprite.UseSeperateCollisionMasks = GMXBool(properties[GMXEnumString(GMXSpriteProperty.SepMasks)], sprite.UseSeperateCollisionMasks); sprite.BoundingBoxMode = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxMode)], sprite.BoundingBoxMode); sprite.BoundingBoxLeft = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxLeft)], sprite.BoundingBoxLeft); sprite.BoundingBoxRight = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxRight)], sprite.BoundingBoxRight); sprite.BoundingBoxTop = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxTop)], sprite.BoundingBoxTop); sprite.BoundingBoxBottom = GMXInt(properties[GMXEnumString(GMXSpriteProperty.BBoxBottom)], sprite.BoundingBoxBottom); sprite.TileHorizontally = GMXBool(properties[GMXEnumString(GMXSpriteProperty.HTile)], sprite.TileHorizontally); sprite.TileVertically = GMXBool(properties[GMXEnumString(GMXSpriteProperty.VTile)], sprite.TileVertically); sprite.UsedFor3D = GMXBool(properties[GMXEnumString(GMXSpriteProperty.For3D)], sprite.UsedFor3D); sprite.Width = GMXInt(properties[GMXEnumString(GMXSpriteProperty.Width)], sprite.Width); sprite.Height = GMXInt(properties[GMXEnumString(GMXSpriteProperty.Height)], sprite.Height); properties[textureGroup] = properties[textureGroup] == "" ? "0" : properties[textureGroup]; properties[textureGroup0] = properties[textureGroup0] == "" ? "0" : properties[textureGroup0]; // The texture group does not equal zero set texture group 0 to the texture group value if (properties[textureGroup] != "0") { properties[textureGroup0] = properties[textureGroup]; } // The texture group zero does not equal zero set texture group to the texture group 0 value else if (properties[textureGroup0] != "0") { properties[textureGroup] = properties[textureGroup0]; } // Create a list of texture groups List <int> textureGroups = new List <int>(); for (int i = 0; properties.ContainsKey(textureGroup + i); i++) { textureGroups.Add(Convert.ToInt32(properties[textureGroup + i])); } // Set the subimage size for all subimages foreach (GMImage image in subImages) { image.Width = sprite.Width; image.Height = sprite.Height; } sprite.TextureGroups = textureGroups.ToArray(); sprite.SubImages = subImages.ToArray(); // Add the sprite sprites.Add(sprite); } // Return the list of sprites return(sprites); }
/// <summary> /// Reads sprites from Game Maker project file. /// </summary> public static GMList <GMSprite> ReadSprites(GMFileReader reader) { // Get version int version = reader.ReadGMInt(); // Check version if (version != 400 && version != 800) { throw new Exception("Unsupported Pre-Sprite object version."); } // Create a new list of sprites GMList <GMSprite> sprites = new GMList <GMSprite>(); // Amount of sprite ids int num = reader.ReadGMInt(); // Iterate through sprites for (int i = 0; i < num; i++) { // If version is 8.0, start inflate if (version == 800) { reader.Decompress(); } // If the sprite at index does not exists, continue if (reader.ReadGMBool() == false) { sprites.LastId++; reader.EndDecompress(); continue; } // Create a new sprite object GMSprite sprite = new GMSprite(); // Set sprite id sprite.Id = i; // Read sprite data sprite.Name = reader.ReadGMString(); // If version is 8.0, get last changed if (version == 800) { sprite.LastChanged = reader.ReadGMDouble(); } // Get version version = reader.ReadGMInt(); // Check version if (version != 400 && version != 542 && version != 800) { throw new Exception("Unsupported Sprite object version."); } // If less than version 8.0 if (version < 800) { // Read sprite data sprite.Width = reader.ReadGMInt(); sprite.Height = reader.ReadGMInt(); sprite.BoundingBoxLeft = reader.ReadGMInt(); sprite.BoundingBoxRight = reader.ReadGMInt(); sprite.BoundingBoxBottom = reader.ReadGMInt(); sprite.BoundingBoxTop = reader.ReadGMInt(); sprite.Transparent = reader.ReadGMBool(); // Check version if (version > 400) { // Read sprite data sprite.SmoothEdges = reader.ReadGMBool(); sprite.Preload = reader.ReadGMBool(); } // Read sprite data sprite.BoundingBoxMode = reader.ReadGMInt(); sprite.Precise = reader.ReadGMBool(); // Check version if (version == 400) { // Read sprtie data sprite.UseVideoMemory = reader.ReadGMBool(); sprite.LoadOnlyOnUse = reader.ReadGMBool(); } // Read sprite data sprite.OriginX = reader.ReadGMInt(); sprite.OriginY = reader.ReadGMInt(); // Sprite number of sub images sprite.SubImages = new GMImage[reader.ReadGMInt()]; // Iterate through sub-images for (int j = 0; j < sprite.SubImages.Length; j++) { // If the sub-image at index does not exists, continue if (reader.ReadGMInt() == -1) { continue; } // Create a new image object GMImage image = new GMImage(); // Get size of image data int size = reader.ReadGMInt(); // Get image data image.Data = reader.ReadGMBytes(size); // Insert compressed image data sprite.SubImages[j] = image; } } else { // Read sprite data sprite.OriginX = reader.ReadGMInt(); sprite.OriginY = reader.ReadGMInt(); // Sprite number of sub images sprite.SubImages = new GMImage[reader.ReadGMInt()]; // Iterate through sub-images for (int j = 0; j < sprite.SubImages.Length; j++) { // Get version. version = reader.ReadGMInt(); // Check version if (version != 800) { throw new Exception("Unsupported Sprite object version."); } // Get width and height of image int width = reader.ReadGMInt(); int height = reader.ReadGMInt(); // If the sprite size is not zero if (width != 0 && height != 0) { // Create a new image object GMImage image = new GMImage(); image.Compressed = false; // Set image data image.Width = width; image.Height = height; // Get size of image data int size = reader.ReadGMInt(); // Get image data image.Data = reader.ReadGMBytes(size); // Insert compressed image data sprite.SubImages[j] = image; } } // Read sprite data sprite.ShapeMode = reader.ReadGMInt(); sprite.AlphaTolerance = reader.ReadGMInt(); sprite.UseSeperateCollisionMasks = reader.ReadGMBool(); sprite.BoundingBoxMode = reader.ReadGMInt(); sprite.BoundingBoxLeft = reader.ReadGMInt(); sprite.BoundingBoxRight = reader.ReadGMInt(); sprite.BoundingBoxBottom = reader.ReadGMInt(); sprite.BoundingBoxTop = reader.ReadGMInt(); } // End object inflate reader.EndDecompress(); // Add sprite sprites.Add(sprite); } // Return sprites return(sprites); }
/// <summary> /// Reads backgrounds from GM file. /// </summary> private GMList<GMBackground> ReadBackgrounds() { // Get version. int version = ReadInt(); // Check version. if (version != 400 && version != 800) throw new Exception("Unsupported Pre-Background object version."); // Create a new list of backgrounds. GMList<GMBackground> backgrounds = new GMList<GMBackground>(); // Amount of background ids. int num = ReadInt(); // Iterate through backgrounds. for (int i = 0; i < num; i++) { // If version is 8.0, start inflate. if (version == 800) Decompress(); // If the background at index does not exists, continue. if (ReadBool() == false) { backgrounds.LastId++; EndDecompress(); continue; } // Create a new background object. GMBackground background = new GMBackground(); // Set background id background.Id = i; // Get background data. background.Name = ReadString(); // If version is 8.0, get last changed. if (version == 800) background.LastChanged = ReadDouble(); // Get version. version = ReadInt(); // Check version. if (version != 400 && version != 543 && version != 710) throw new Exception("Unsupported Background object version."); // If version is less than 7.1. if (version < 710) { // Background data background.Width = ReadInt(); background.Height = ReadInt(); background.Transparent = ReadBool(); // Check version. if (version > 400) { // Read background data. background.SmoothEdges = ReadBool(); background.Preload = ReadBool(); background.UseAsTileSet = ReadBool(); background.TileWidth = ReadInt(); background.TileHeight = ReadInt(); background.HorizontalOffset = ReadInt(); background.VerticalOffset = ReadInt(); background.HorizontalSeperation = ReadInt(); background.VerticalSeperation = ReadInt(); } else { // Read background data. background.UseVideoMemory = ReadBool(); background.LoadOnlyOnUse = ReadBool(); } // If image data exists. if (ReadBool()) { // If pixel data does not exist. if (ReadInt() == -1) continue; // Create a new image. GMImage image = new GMImage(); // Get size of image data. int size = ReadInt(); // Get compressed image data. image.Data = ReadBytes(size); // Set background image. background.Image = image; } } else { // Get background data. background.UseAsTileSet = ReadBool(); background.TileWidth = ReadInt(); background.TileHeight = ReadInt(); background.HorizontalOffset = ReadInt(); background.VerticalOffset = ReadInt(); background.HorizontalSeperation = ReadInt(); background.VerticalSeperation = ReadInt(); // Get version. version = ReadInt(); // Check version. if (version != 800) throw new Exception("Unsupported Background object version."); // Get image data. background.Width = ReadInt(); background.Height = ReadInt(); // If the sprite size is not zero. if (background.Width != 0 && background.Height != 0) { // Create a new image object. GMImage image = new GMImage(); image.Compressed = false; // Set image data. image.Width = background.Width; image.Height = background.Height; // Get size of image data. int size = ReadInt(); // Get image data. image.Data = ReadBytes(size); // Insert compressed image data. background.Image = image; } } // End object inflate. EndDecompress(); // Add background. backgrounds.Add(background); } // Return backgrounds. return backgrounds; }
/// <summary> /// Reads sprites from Game Maker project file. /// </summary> private GMList<GMSprite> ReadSprites() { // Get version. int version = ReadInt(); // Check version. if (version != 400 && version != 800) throw new Exception("Unsupported Pre-Sprite object version."); // Create a new list of sprites. GMList<GMSprite> sprites = new GMList<GMSprite>(); // Amount of sprite ids. int num = ReadInt(); // Iterate through sprites. for (int i = 0; i < num; i++) { // If version is 8.0, start inflate. if (version == 800) Decompress(); // If the sprite at index does not exists, continue. if (ReadBool() == false) { sprites.LastId++; EndDecompress(); continue; } // Create a new sprite object. GMSprite sprite = new GMSprite(); // Set sprite id. sprite.Id = i; // Read sprite data. sprite.Name = ReadString(); // If version is 8.0, get last changed. if (version == 800) sprite.LastChanged = ReadDouble(); // Get version. version = ReadInt(); // Check version. if (version != 400 && version != 542 && version != 800) throw new Exception("Unsupported Sprite object version."); // If less than version 8.0. if (version < 800) { // Read sprite data sprite.Width = ReadInt(); sprite.Height = ReadInt(); sprite.BoundingBoxLeft = ReadInt(); sprite.BoundingBoxRight = ReadInt(); sprite.BoundingBoxBottom = ReadInt(); sprite.BoundingBoxTop = ReadInt(); sprite.Transparent = ReadBool(); // Check version. if (version > 400) { // Read sprite data sprite.SmoothEdges = ReadBool(); sprite.Preload = ReadBool(); } // Read sprite data. sprite.BoundingBoxMode = (BoundingBoxType)ReadInt(); sprite.Precise = ReadBool(); // Check version. if (version == 400) { // Read sprtie data. sprite.UseVideoMemory = ReadBool(); sprite.LoadOnlyOnUse = ReadBool(); } // Read sprite data. sprite.OriginX = ReadInt(); sprite.OriginY = ReadInt(); // Sprite number of sub images sprite.SubImages = new GMImage[ReadInt()]; // Iterate through sub-images for (int j = 0; j < sprite.SubImages.Length; j++) { // If the sub-image at index does not exists, continue. if (ReadInt() == -1) continue; // Create a new image object. GMImage image = new GMImage(); // Get size of image data. int size = ReadInt(); // Get image data. image.Data = ReadBytes(size); // Insert compressed image data. sprite.SubImages[j] = image; } } else { // Read sprite data. sprite.OriginX = ReadInt(); sprite.OriginY = ReadInt(); // Sprite number of sub images. sprite.SubImages = new GMImage[ReadInt()]; // Iterate through sub-images. for (int j = 0; j < sprite.SubImages.Length; j++) { // Get version. version = ReadInt(); // Check version. if (version != 800) throw new Exception("Unsupported Sprite object version."); // Get width and height of image. int width = ReadInt(); int height = ReadInt(); // If the sprite size is not zero. if (width != 0 && height != 0) { // Create a new image object. GMImage image = new GMImage(); image.Compressed = false; // Set image data. image.Width = width; image.Height = height; // Get size of image data. int size = ReadInt(); // Get image data. image.Data = ReadBytes(size); // Insert compressed image data. sprite.SubImages[j] = image; } } // Read sprite data. sprite.ShapeMode = (ShapeType)ReadInt(); sprite.AlphaTolerance = ReadInt(); sprite.UseSeperateCollisionMasks = ReadBool(); sprite.BoundingBoxMode = (BoundingBoxType)ReadInt(); sprite.BoundingBoxLeft = ReadInt(); sprite.BoundingBoxRight = ReadInt(); sprite.BoundingBoxBottom = ReadInt(); sprite.BoundingBoxTop = ReadInt(); } // End object inflate. EndDecompress(); // Add sprite. sprites.Add(sprite); } // Return sprites. return sprites; }
/// <summary> /// Reads all backgrounds from a GM file reader /// </summary> public static GMList <GMBackground> ReadBackgrounds(GMFileReader reader) { // Get version int version = reader.ReadGMInt(); // Check version if (version != 400 && version != 800) { throw new Exception("Unsupported Pre-Background object version."); } // Create a new list of backgrounds GMList <GMBackground> backgrounds = new GMList <GMBackground>(); // Amount of background ids int num = reader.ReadGMInt(); // Iterate through backgrounds for (int i = 0; i < num; i++) { // If version is 8.0, start inflate if (version == 800) { reader.Decompress(); } // If the background at index does not exists, continue if (reader.ReadGMBool() == false) { backgrounds.LastId++; reader.EndDecompress(); continue; } // Create a new background object GMBackground background = new GMBackground(); // Set background id background.Id = i; // Get background data background.Name = reader.ReadGMString(); // If version is 8.0, get last changed if (version == 800) { background.LastChanged = reader.ReadGMDouble(); } // Get version version = reader.ReadGMInt(); // Check version if (version != 400 && version != 543 && version != 710) { throw new Exception("Unsupported Background object version."); } // If version is less than 7.1 if (version < 710) { // Background data background.Width = reader.ReadGMInt(); background.Height = reader.ReadGMInt(); background.Transparent = reader.ReadGMBool(); // Check version if (version > 400) { // Read background data background.SmoothEdges = reader.ReadGMBool(); background.Preload = reader.ReadGMBool(); background.UseAsTileSet = reader.ReadGMBool(); background.TileWidth = reader.ReadGMInt(); background.TileHeight = reader.ReadGMInt(); background.HorizontalOffset = reader.ReadGMInt(); background.VerticalOffset = reader.ReadGMInt(); background.HorizontalSeperation = reader.ReadGMInt(); background.VerticalSeperation = reader.ReadGMInt(); } else { // Read background data background.UseVideoMemory = reader.ReadGMBool(); background.LoadOnlyOnUse = reader.ReadGMBool(); } // If image data exists if (reader.ReadGMBool()) { // If pixel data does not exist if (reader.ReadGMInt() == -1) { continue; } // Create a new image GMImage image = new GMImage(); // Set image data image.Width = background.Width; image.Height = background.Height; // Get size of image data int size = reader.ReadGMInt(); // Get compressed image data image.Data = reader.ReadGMBytes(size); // Set background image background.Image = image; } } else { // Get background data background.UseAsTileSet = reader.ReadGMBool(); background.TileWidth = reader.ReadGMInt(); background.TileHeight = reader.ReadGMInt(); background.HorizontalOffset = reader.ReadGMInt(); background.VerticalOffset = reader.ReadGMInt(); background.HorizontalSeperation = reader.ReadGMInt(); background.VerticalSeperation = reader.ReadGMInt(); // Get version version = reader.ReadGMInt(); // Check version if (version != 800) { throw new Exception("Unsupported Background object version."); } // Get image data background.Width = reader.ReadGMInt(); background.Height = reader.ReadGMInt(); // If the sprite size is not zero if (background.Width != 0 && background.Height != 0) { // Create a new image object GMImage image = new GMImage(); image.Compressed = false; // Set image data image.Width = background.Width; image.Height = background.Height; // Get size of image data int size = reader.ReadGMInt(); // Get image data image.Data = reader.ReadGMBytes(size); // Insert compressed image data background.Image = image; } } // End object inflate reader.EndDecompress(); // Add background backgrounds.Add(background); } // Return backgrounds return(backgrounds); }
/// <summary> /// Reads all backgrounds from a background XML file /// </summary> /// <param name="directory">The XML (.GMX) file path</param> /// <param name="assets">A list of assets listed in the project GMX</param> /// <returns>A GM background</returns> public static GMList <GMBackground> ReadBackgroundsGMX(string directory, ref List <string> assets) { // A list of backgrounds GMList <GMBackground> backgrounds = new GMList <GMBackground>(); backgrounds.AutoIncrementIds = false; // Iterate through .gmx files in the directory foreach (string file in Directory.GetFiles(directory, "*.gmx")) { // Set name of the background string name = GetResourceName(file); // If the file is not in the asset list, it has been orphaned, continue if (!assets.Contains(name)) { continue; } // Create a dictionary of room properties Dictionary <string, string> properties = new Dictionary <string, string>(); foreach (GMXBackgroundProperty property in Enum.GetValues(typeof(GMXBackgroundProperty))) { properties.Add(GMXEnumString(property), ""); } // Background image and texture group strings GMImage image = null; string textureGroup = GMXEnumString(GMXBackgroundProperty.TextureGroup); string textureGroup0 = GMXEnumString(GMXBackgroundProperty.TextureGroup0); // Create an XMLReader to read in the resource elements using (XmlReader reader = XmlReader.Create(file)) { // Move to a content node reader.MoveToContent(); // Read XML file while (reader.Read()) { // If the node is not an element, continue if (reader.NodeType != XmlNodeType.Element) { continue; } // Get the element name string nodeName = reader.Name; // Read reader.Read(); // If the element value is null or empty, continue if (string.IsNullOrEmpty(reader.Value)) { continue; } // If the element is a frame element create subimage, else normal property if (nodeName.ToLower() == GMXEnumString(GMXBackgroundProperty.Data)) { // Create an image and set the image path image = new GMImage(); image.Compressed = false; image.FilePath = reader.Value; image.Data = GMUtilities.LoadBytesFromBitmap(directory + "\\" + image.FilePath); } else { // Set the property value properties[nodeName] = reader.Value; } } } // Create a new background and set its properties GMBackground background = new GMBackground(); background.Id = GetIdFromName(name); background.Name = name; background.UseAsTileSet = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.IsTileset)], background.UseAsTileSet); background.TileWidth = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileWidth)], background.TileWidth); background.TileHeight = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileHeight)], background.TileHeight); background.HorizontalOffset = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileXOff)], background.HorizontalOffset); background.VerticalOffset = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileYOff)], background.VerticalOffset); background.HorizontalSeperation = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileHSep)], background.HorizontalSeperation); background.VerticalSeperation = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.TileVSep)], background.VerticalSeperation); background.TileHorizontally = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.HTile)], background.TileHorizontally); background.TileVertically = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.VTile)], background.TileVertically); background.UsedFor3D = GMXBool(properties[GMXEnumString(GMXBackgroundProperty.For3D)], background.UsedFor3D); background.Width = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.Width)], background.Width); background.Height = GMXInt(properties[GMXEnumString(GMXBackgroundProperty.Height)], background.Height); properties[textureGroup] = properties[textureGroup] == "" ? "0" : properties[textureGroup]; properties[textureGroup0] = properties[textureGroup0] == "" ? "0" : properties[textureGroup0]; image.Width = background.Width; image.Height = background.Height; background.Image = image; // The texture group does not equal zero set texture group 0 to the texture group value if (properties[textureGroup] != "0") { properties[textureGroup0] = properties[textureGroup]; } // The texture group zero does not equal zero set texture group to the texture group 0 value else if (properties[textureGroup0] != "0") { properties[textureGroup] = properties[textureGroup0]; } // Create a list of texture groups List <int> textureGroups = new List <int>(); for (int i = 0; properties.ContainsKey(string.Concat(textureGroup, i)); i++) { textureGroups.Add(Convert.ToInt32(properties[string.Concat(textureGroup, i)])); } background.TextureGroups = textureGroups.ToArray(); // Add the background backgrounds.Add(background); } // Return the list of backgrounds return(backgrounds); }
/// <summary> /// Makes a GDI+ bitmap from compressed Game Maker image data. /// </summary> /// <param name="imageData">The GM image data to convert.</param> /// <returns>A GDI+ bitmap.</returns> public static Bitmap GetBitmap(GMImage image) { // If not compressed, get bitmap normally. if (image.Compressed == false) return PixelDataToBitmap(image.Data, image.Width, image.Height); // Create new inflater. Inflater inflater = new Inflater(); // Image buffer. byte[] result = new byte[image.Data.Length]; // Set inflater input byte data. inflater.SetInput(image.Data, 0, image.Data.Length); // Using memory stream. using (MemoryStream ms = new MemoryStream()) { // While the decompressor is not finished. while (!inflater.IsFinished) { // Get length of data. int length = inflater.Inflate(result); // Write data result to byte array. ms.Write(result, 0, length); } // Reset position. ms.Position = 0; // Get bitmap from screen Bitmap bitmap = (Bitmap)Bitmap.FromStream(ms); // Create a backbuffer image to convert the bitmap for the screen System.Drawing.Graphics gfx = System.Drawing.Graphics.FromHwnd(IntPtr.Zero); Bitmap buffer = new Bitmap(bitmap.Width, bitmap.Height, gfx); gfx.Dispose(); // Copy original bitmap to new backbuffer gfx = System.Drawing.Graphics.FromImage(buffer); gfx.DrawImageUnscaled(bitmap, new Point(0, 0)); gfx.Dispose(); // Return bitmap return buffer; } }