Example #1
0
        public static Bitmap GetBitmapForSpriteResizedKeepingAspectRatio(Sprite sprite, int width, int height, bool centreInNewCanvas, bool drawOutline, Color backgroundColour)
        {
            float targetWidthHeightRatio = (float)width / (float)height;
            float spriteWidthHeightRatio = (float)sprite.Width / (float)sprite.Height;
            int   newWidth, newHeight;

            if ((sprite.Width < width / 2) && (sprite.Height < height / 2))
            {
                newWidth  = sprite.Width * 2;
                newHeight = sprite.Height * 2;
            }
            else if (spriteWidthHeightRatio > targetWidthHeightRatio)
            {
                newWidth  = width;
                newHeight = (int)(((float)width / (float)sprite.Width) * (float)sprite.Height);
            }
            else
            {
                newHeight = height;
                newWidth  = (int)(((float)height / (float)sprite.Height) * (float)sprite.Width);
            }

            // correct size if a very wide/tall image (eg. 400x2) is shrunk
            if (newWidth < 1)
            {
                newWidth = 1;
            }
            if (newHeight < 1)
            {
                newHeight = 1;
            }

            Bitmap newBmp = new Bitmap(width, height, PixelFormat.Format32bppRgb);

            using (Graphics g = Graphics.FromImage(newBmp))
            {
                g.Clear(backgroundColour);
                int x = 0, y = 0;

                using (Bitmap bitmapToDraw = Factory.NativeProxy.GetBitmapForSprite(sprite.Number, newWidth, newHeight))
                {
                    Bitmap bmp = bitmapToDraw ?? SpriteTools.GetPlaceHolder();
                    if (centreInNewCanvas)
                    {
                        x = width / 2 - bmp.Width / 2;
                        y = height - bmp.Height;
                    }

                    g.DrawImage(bmp, x, y, bmp.Width, bmp.Height);
                }

                if (drawOutline)
                {
                    g.DrawRectangle(Pens.Brown, x, y, newWidth - 1, newHeight - 1);
                }
            }

            return(newBmp);
        }
Example #2
0
        private void cmbFilenames_SelectedIndexChanged(object sender, EventArgs e)
        {
            string filename = imageLookup[cmbFilenames.SelectedIndex];

            try
            {
                image = SpriteTools.LoadFirstImageFromFile(filename);
            }
            catch (Types.InvalidDataException ex)
            {
                // use a placeholder in-case of bad data
                Factory.GUIController.ShowMessage(ex.Message, MessageBoxIconType.Error);
                image = SpriteTools.GetPlaceHolder();
            }

            PostImageLoad();
        }