private void button1_Click(object sender, EventArgs e) { if (selectedFilePath.Length == 0) { MessageBox.Show("You must select a file first!", "No File(s) Selected", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } ImageFacing startingFacing = (ImageFacing)ddlFacing.SelectedItem; ImageFacing inputFacing = (ImageFacing)ddlInputFacing.SelectedIndex; Rotator r = new Rotator(ref lblMessage, ref sfdOutput); if (cbxResize.Checked) { if (string.IsNullOrEmpty(tbxOutputSizeX.Text) || string.IsNullOrEmpty(tbxOutputSizeY.Text)) { MessageBox.Show("You must specify output X & Y first!", "No Output Sizes", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int outX = 0; int outY = 0; if (int.TryParse(tbxOutputSizeX.Text, out outX) && int.TryParse(tbxOutputSizeY.Text, out outY)) { r.CreateRotatedSpriteSheet(selectedFilePath[0], startingFacing, inputFacing, true, outX, outY); } } else { r.CreateRotatedSpriteSheet(selectedFilePath[0], startingFacing, inputFacing, false); } }
internal void CreateRotatedSpriteSheet(string selectedFilePath, ImageFacing startingFacing, ImageFacing inputFacing, bool resize, int outX = 32, int outY = 32) { Bitmap source = null; try { source = new Bitmap(selectedFilePath); } catch (ArgumentException) { MessageBox.Show("Selected file is not an image!", "Invalid File", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } // resize to square source = padImage(source); Bitmap target = new Bitmap(source.Width * 8, source.Height); target.MakeTransparent(target.GetPixel(0, 0)); int degreesRotation = 45 * (startingFacing - inputFacing); using (ImageFactory imgFac = new ImageFactory()) { for (int facingCount = 0; facingCount < 8; facingCount++) { message.Text = string.Format("Updating Facing {0} of 8", facingCount + 1); message.Refresh(); using (Stream ms = new MemoryStream()) { // rotate image imgFac.Load(source).Rotate(degreesRotation).Save(ms); Bitmap currentImage = new Bitmap(ms); currentImage.MakeTransparent(); // add the image at offset int x = source.Width * facingCount; int y = 0; target = ImageHelper.Superimpose(target, padImage(currentImage, target.Height), new Point(x, y)); // change rotation degreesRotation += 45; } } } // check for resize if (resize) { Resizer r = new Resizer(ref message, ref sfdOutput); r.ResizeSpritesheet(target, source.Width, source.Height, outX, outY); } else { // save file if (sfdOutput.ShowDialog() == DialogResult.OK) { target.MakeTransparent(); target.Save(sfdOutput.FileName, System.Drawing.Imaging.ImageFormat.Png); } } }