private int GetInvalidFrame(Pak.Sprite sprite) { GraphicsUnit graphicsUnit = GraphicsUnit.Pixel; for (int i = 0; i < sprite.frames.Count; i++) { if (!sprite.image.GetBounds(ref graphicsUnit).Contains(sprite.frames[i].rect) || sprite.frames[i].rect.Width == 0 || sprite.frames[i].rect.Height == 0) { return(i); } } return(-1); }
private bool AddSprite() { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"; if (fileDialog.ShowDialog() == DialogResult.OK) { Pak.Sprite newSprite = new Pak.Sprite(); var stream = File.OpenRead(fileDialog.FileName); newSprite.image = (Bitmap)Bitmap.FromStream(stream); stream.Close(); Paks[curPak].sprites.Add(newSprite); ChangeSprite(Paks[curPak].sprites.Count - 1); RefreshSpriteTree(); UpdateControls(); return(true); } return(false); }
private void RefreshSpriteTree() { spriteTree.BeginUpdate(); spriteTree.Nodes.Clear(); for (int sprCount = 0; sprCount < Paks[curPak].sprites.Count; sprCount++) { TreeNode treeN = new TreeNode("Sprite " + sprCount); Pak.Sprite spr = Paks[curPak].sprites[sprCount]; for (int frameCount = 0; frameCount < spr.frames.Count; frameCount++) { treeN.Nodes.Add("Frame " + frameCount); } spriteTree.Nodes.Add(treeN); } spriteTree.EndUpdate(); spriteTree.Refresh(); if (curSprite != -1) { spriteTree.Nodes[curSprite].Expand(); } }
private void generateSpriteSheet_Click(object sender, EventArgs e) { OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Filter = "Bitmap files (*.bmp)|*.bmp|All files (*.*)|*.*"; fileDialog.Multiselect = true; if (fileDialog.ShowDialog() == DialogResult.OK) { List <Image> loadedImages = new List <Image>(); List <Rectangle> frameBounds = new List <Rectangle>(); String [] splitFile = fileDialog.FileName.Split('.'); int start; if (int.TryParse(splitFile[splitFile.Length - 2], out start) == false) { MessageBox.Show("Unable to parse file name: " + Path.GetFileName(fileDialog.FileName), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } splitFile[splitFile.Length - 2] = "{0:D2}"; String pathFormat = String.Join(".", splitFile); for (int i = start; i < 100; i++) { String fileName = String.Format(pathFormat, i); try { var stream = File.OpenRead(fileName); loadedImages.Add((Bitmap)Bitmap.FromStream(stream)); stream.Close(); } catch (FileNotFoundException) { break; } catch (Exception ex) { MessageBox.Show(String.Format("Error occoured while loading image ({0}) : {1}", Path.GetFileName(fileName), ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)); if (Paks[curPak].sprites.Count == 0) { ClosePak(curPak); } return; } } int totalWidth = 0, maxHeight = 0; foreach (Image frame in loadedImages) { Rectangle bounds = getFrameBounds((Bitmap)frame); if (bounds.Width < 1 || bounds.Height < 1) { MessageBox.Show("Could not find frame within image", "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); return; } frameBounds.Add(bounds); totalWidth += bounds.Width; if (bounds.Height > maxHeight) { maxHeight = bounds.Height; } } Bitmap generatedImage = new Bitmap(totalWidth, maxHeight, loadedImages[0].PixelFormat); generatedImage.SetResolution(loadedImages[0].HorizontalResolution, loadedImages[0].VerticalResolution); Pak.Sprite newSprite = new Pak.Sprite(); using (Graphics g = Graphics.FromImage((Image)generatedImage)) { GraphicsUnit gUnit = GraphicsUnit.Pixel; Brush keyBrush = new SolidBrush(((Bitmap)loadedImages[0]).GetPixel(0, 0)); g.FillRectangle(keyBrush, generatedImage.GetBounds(ref gUnit)); for (int i = 0, x = 0; i < loadedImages.Count; i++) { g.DrawImage(loadedImages[i], x, 0, frameBounds[i], GraphicsUnit.Pixel); Pak.FrameInfo frmInfo = new Pak.FrameInfo(); Rectangle frameRect = frameBounds[i]; frameRect.Y = 0; frameRect.X = x; frmInfo.rect = frameRect; frmInfo.fixX = (short)(loadedImages[i].Width / 2 + frameBounds[i].Left); frmInfo.fixY = (short)(loadedImages[i].Height / 2 + frameBounds[i].Top); newSprite.frames.Add(frmInfo); x += frameBounds[i].Width; } } newSprite.image = (Bitmap)generatedImage; Paks[curPak].sprites.Add(newSprite); ChangeSprite(Paks[curPak].sprites.Count - 1); RefreshSpriteTree(); UpdateControls(); } }