コード例 #1
0
        /// <summary>
        /// Rotates the bitmap in 90° steps clockwise and returns a new rotated WriteableBitmap.
        /// </summary>
        /// <param name="bmp">The WriteableBitmap.</param>
        /// <param name="angle">The angle in degrees the bitmap should be rotated in 90° steps clockwise.</param>
        /// <returns>A new WriteableBitmap that is a rotated version of the input.</returns>
        public static BitmapBuffer Rotate(this BitmapBuffer bmp, FastRotateAngle angle)
        {
            using (BitmapContext context = bmp.GetBitmapContext(ReadWriteMode.ReadOnly))
            {
                // Use refs for faster access (really important!) speeds up a lot!
                int   w = context.Width;
                int   h = context.Height;
                int[] p = context.Pixels;
                int   i = 0;


                switch (angle)
                {
                default:
                {
                    return(bmp.Clone());
                }

                case FastRotateAngle.Rotate90:
                {
                    var result = BitmapBufferFactory.New(h, w);
                    using (BitmapContext destContext = result.GetBitmapContext())
                    {
                        var rp = destContext.Pixels;
                        for (int x = 0; x < w; x++)
                        {
                            for (int y = h - 1; y >= 0; y--)
                            {
                                int srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                    return(result);
                }

                case FastRotateAngle.Rotate180:
                {
                    var result = BitmapBufferFactory.New(w, h);
                    using (BitmapContext destContext = result.GetBitmapContext())
                    {
                        var rp = destContext.Pixels;
                        for (int y = h - 1; y >= 0; y--)
                        {
                            for (int x = w - 1; x >= 0; x--)
                            {
                                int srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                    return(result);
                }

                case FastRotateAngle.Rotate270:
                {
                    var result = BitmapBufferFactory.New(h, w);
                    using (BitmapContext destContext = result.GetBitmapContext())
                    {
                        int[] rp = destContext.Pixels;
                        for (int x = w - 1; x >= 0; x--)
                        {
                            for (int y = 0; y < h; y++)
                            {
                                int srcInd = y * w + x;
                                rp[i] = p[srcInd];
                                i++;
                            }
                        }
                    }
                    return(result);
                }
                }
            }
        }