System.Drawing.GeomUtilities.CreateRotateFlipTransform C# (CSharp) Method

CreateRotateFlipTransform() static private method

Creates the rotate flip transform given the input parameters
static private CreateRotateFlipTransform ( int &width, int &height, float angle, bool flipX, bool flipY ) : CGAffineTransform
width int Width.
height int Height.
angle float Angle.
flipX bool If set to true flip x.
flipY bool If set to true flip y.
return CGAffineTransform
        internal static CGAffineTransform CreateRotateFlipTransform(ref int width, ref int height, float angle, bool flipX, bool flipY)
        {
            float rotateX    =  (float)Math.Abs ( Math.Cos ( angle.ToRadians() ) );
            float rotateY    =  (float)Math.Abs ( Math.Sin ( angle.ToRadians() ) );

            float deltaWidth    =  width * rotateX + height  * rotateY;
            float deltaHeight    =  width * rotateY + height  * rotateX;

            CGAffineTransform rotateFlipTransform = CGAffineTransform.MakeTranslation(flipX?-deltaWidth:0.0f,flipY?-deltaHeight:0.0f);
            rotateFlipTransform.Multiply(CGAffineTransform.MakeScale(flipX?-1.0f:1.0f,flipY?-1.0f: 1.0f));

            if (0.0f != angle)
            {
                var rot	=  CGAffineTransform.MakeTranslation(-deltaHeight*0.5f,-deltaWidth*0.5f);
                rot.Rotate(angle.ToRadians());
                rot.Translate(deltaWidth*0.5f,deltaHeight*0.5f);

                rotateFlipTransform = CGAffineTransform.Multiply (rot, rotateFlipTransform);
            }

            width = (int)deltaWidth;
            height = (int)deltaHeight;

            return rotateFlipTransform;
        }

Usage Example

示例#1
0
        internal new void RotateFlip(RotateFlipType rotateFlipType)
        {
            CGAffineTransform rotateFlip = CGAffineTransform.MakeIdentity();

            int width, height;

            width  = (int)NativeCGImage.Width;
            height = (int)NativeCGImage.Height;

            switch (rotateFlipType)
            {
            //			case RotateFlipType.RotateNoneFlipNone:
            //			//case RotateFlipType.Rotate180FlipXY:
            //				rotateFlip = GeomUtilities.CreateRotateFlipTransform (b.Width, b.Height, 0, false, false);
            //				break;
            case RotateFlipType.Rotate90FlipNone:
                //case RotateFlipType.Rotate270FlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 90, false, false);
                break;

            case RotateFlipType.Rotate180FlipNone:
                //case RotateFlipType.RotateNoneFlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 0, true, true);
                break;

            case RotateFlipType.Rotate270FlipNone:
                //case RotateFlipType.Rotate90FlipXY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 270, false, false);
                break;

            case RotateFlipType.RotateNoneFlipX:
                //case RotateFlipType.Rotate180FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 0, true, false);
                break;

            case RotateFlipType.Rotate90FlipX:
                //case RotateFlipType.Rotate270FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 90, true, false);
                break;

            case RotateFlipType.Rotate180FlipX:
                //case RotateFlipType.RotateNoneFlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 0, false, true);
                break;

            case RotateFlipType.Rotate270FlipX:
                //case RotateFlipType.Rotate90FlipY:
                rotateFlip = GeomUtilities.CreateRotateFlipTransform(ref width, ref height, 270, true, false);
                break;
            }

            var bytesPerRow      = (width * (int)NativeCGImage.BitsPerPixel + 7) / 8;
            var newBitmapBlock   = Marshal.AllocHGlobal(height * bytesPerRow);
            var newBitmapContext = new CGBitmapContext(newBitmapBlock, width, height, NativeCGImage.BitsPerComponent, bytesPerRow, NativeCGImage.ColorSpace, NativeCGImage.AlphaInfo);

            newBitmapContext.ConcatCTM(rotateFlip);
            newBitmapContext.DrawImage(new CGRect(0, 0, NativeCGImage.Width, NativeCGImage.Height), NativeCGImage);
            newBitmapContext.Flush();

            // If the width or height is not the seme we need to switch the dpiHeight and dpiWidth
            // We should be able to get around this with set resolution later.
            if (NativeCGImage.Width != width || NativeCGImage.Height != height)
            {
                var temp = dpiWidth;
                dpiHeight = dpiWidth;
                dpiWidth  = temp;
            }

            physicalDimension.Width  = (float)width;
            physicalDimension.Height = (float)height;

            physicalSize         = new SizeF(physicalDimension.Width, physicalDimension.Height);
            physicalSize.Width  *= ConversionHelpers.MS_DPI / dpiWidth;
            physicalSize.Height *= ConversionHelpers.MS_DPI / dpiHeight;

            // In windows the RawFormat is changed to MemoryBmp to show that the image has changed.
            rawFormat = ImageFormat.MemoryBmp;

            // Set our transform for this image for the new height
            imageTransform = new CGAffineTransform(1, 0, 0, -1, 0, height);

            // bitmapBlock is owned by dataProvider and freed implicitly
            if (dataProvider != null)
            {
                dataProvider.Dispose();
            }

            if (cachedContext != null)
            {
                cachedContext.Dispose();
            }
            NativeCGImage.Dispose();

            this.bitmapBlock   = newBitmapBlock;
            this.dataProvider  = new CGDataProvider(bitmapBlock, height * bytesPerRow);
            this.NativeCGImage = newBitmapContext.ToImage();
            this.cachedContext = newBitmapContext;
            this.imageSource   = null;

            // update the cached size
            imageSize.Width  = this.Width;
            imageSize.Height = this.Height;
        }