AGS.Editor.Utilities.GetBitmapForSpriteResizedKeepingAspectRatio C# (CSharp) Метод

GetBitmapForSpriteResizedKeepingAspectRatio() публичный статический Метод

public static GetBitmapForSpriteResizedKeepingAspectRatio ( Sprite sprite, int width, int height, bool centreInNewCanvas, bool drawOutline, Color backgroundColour ) : Bitmap
sprite Sprite
width int
height int
centreInNewCanvas bool
drawOutline bool
backgroundColour Color
Результат System.Drawing.Bitmap
        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);
            Graphics g = Graphics.FromImage(newBmp);
            g.Clear(backgroundColour);
            Bitmap bitmapToDraw = Factory.NativeProxy.GetBitmapForSprite(sprite.Number, newWidth, newHeight);

            int x = 0, y = 0;
            if (centreInNewCanvas)
            {
                x = width / 2 - bitmapToDraw.Width / 2;
                y = height - bitmapToDraw.Height;
            }

            g.DrawImage(bitmapToDraw, x, y, bitmapToDraw.Width, bitmapToDraw.Height);

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

            g.Dispose();
            return newBmp;
        }

Usage Example

Пример #1
0
        private void previewPanel_Paint(object sender, PaintEventArgs e)
        {
            if ((_view != null) && (udLoop.Value < _view.Loops.Count) &&
                (udFrame.Value < _view.Loops[(int)udLoop.Value].Frames.Count))
            {
                ViewFrame  thisFrame    = _view.Loops[(int)udLoop.Value].Frames[(int)udFrame.Value];
                int        spriteNum    = thisFrame.Image;
                SpriteInfo info         = Factory.NativeProxy.GetSpriteInfo(spriteNum);
                int        spriteWidth  = info.Width;
                int        spriteHeight = info.Height;
                // Draw low-res sprites larger (TODO: find out why, perhaps just for the better looks?)
                if (info.Resolution == SpriteImportResolution.LowRes)
                {
                    spriteWidth  *= 2;
                    spriteHeight *= 2;
                }
                int x = 0, y;
                y = previewPanel.ClientSize.Height - spriteHeight;
                if (chkCentrePivot.Checked)
                {
                    x = previewPanel.ClientSize.Width / 2 - spriteWidth / 2;
                }
                if ((spriteWidth <= previewPanel.ClientSize.Width) &&
                    (spriteHeight <= previewPanel.ClientSize.Height))
                {
                    IntPtr hdc = e.Graphics.GetHdc();
                    Factory.NativeProxy.DrawSprite(hdc, x, y, spriteNum, thisFrame.Flipped);
                    e.Graphics.ReleaseHdc();
                }
                else
                {
                    Bitmap bmp = Utilities.GetBitmapForSpriteResizedKeepingAspectRatio(new Sprite(spriteNum, spriteWidth, spriteHeight), previewPanel.ClientSize.Width, previewPanel.ClientSize.Height, chkCentrePivot.Checked, false, SystemColors.Control);

                    if (thisFrame.Flipped)
                    {
                        Point   urCorner = new Point(0, 0);
                        Point   ulCorner = new Point(bmp.Width, 0);
                        Point   llCorner = new Point(bmp.Width, bmp.Height);
                        Point[] destPara = { ulCorner, urCorner, llCorner };
                        e.Graphics.DrawImage(bmp, destPara);
                    }
                    else
                    {
                        e.Graphics.DrawImage(bmp, 1, 1);
                    }

                    bmp.Dispose();
                }
            }
        }
All Usage Examples Of AGS.Editor.Utilities::GetBitmapForSpriteResizedKeepingAspectRatio