private async void Cancel_Click(object sender, RoutedEventArgs e)
        {
            if (CropToolbar != null && CropToolbar.Visibility == Visibility.Visible)
            {
                // Reset Cropper
                await Cropper.SetSourceAsync(_media.File, _media.EditState.Rotation, _media.EditState.Flip, _media.EditState.Proportions, _media.EditState.Rectangle);

                _rotation      = _media.EditState.Rotation;
                _flip          = _media.EditState.Flip;
                Crop.IsChecked = IsCropped(Cropper.CropRectangle);
                ResetUiVisibility();
            }
            else if (DrawToolbar != null && DrawToolbar.Visibility == Visibility.Visible)
            {
                Canvas.RestoreState();

                ResetUiVisibility();
                SettingsService.Current.Pencil = DrawSlider.GetDefault();
            }
            else
            {
                if (_media is StorageMedia)
                {
                    _media.EditState = _originalMediaEditState; // Reset to state before starting editing
                }
                if (_media is StorageVideo video)
                {
                    video.IsMuted     = _originalVideoIsMuted;
                    video.Compression = _originalVideoCompression;
                }
                ResetUiVisibility();
                Hide(ContentDialogResult.Secondary);
            }
        }
Esempio n. 2
0
        public static async Task <StorageFile> DrawStrokesAsync(StorageFile file, IReadOnlyList <SmoothPathBuilder> strokes, Rect rectangle, BitmapRotation rotation, BitmapFlip flip)
        {
            var device = CanvasDevice.GetSharedDevice();
            var bitmap = await CanvasBitmap.LoadAsync(device, file.Path);

            var canvas1 = new CanvasRenderTarget(device, (float)bitmap.Size.Width, (float)bitmap.Size.Height, bitmap.Dpi);
            var canvas2 = new CanvasRenderTarget(device, (float)bitmap.Size.Width, (float)bitmap.Size.Height, bitmap.Dpi);

            var size       = canvas1.Size.ToVector2();
            var canvasSize = canvas1.Size.ToVector2();

            var scaleX = 1 / (float)rectangle.Width;
            var scaleY = 1 / (float)rectangle.Height;

            var offsetX = (float)rectangle.X * scaleX;
            var offsetY = (float)rectangle.Y * scaleY;

            if (rotation is BitmapRotation.Clockwise270Degrees or BitmapRotation.Clockwise90Degrees)
            {
                size = new Vector2(size.Y, size.X);

                scaleX = scaleY;
                scaleY = 1 * 1 / (float)rectangle.Width;
            }

            using (var session = canvas1.CreateDrawingSession())
            {
                switch (rotation)
                {
                case BitmapRotation.Clockwise90Degrees:
                    var transform1 = Matrix3x2.CreateRotation(MathFEx.ToRadians(90));
                    transform1.Translation = new Vector2(size.Y, 0);
                    session.Transform      = transform1;
                    break;

                case BitmapRotation.Clockwise180Degrees:
                    var transform2 = Matrix3x2.CreateRotation(MathFEx.ToRadians(180));
                    transform2.Translation = new Vector2(size.X, size.Y);
                    session.Transform      = transform2;
                    break;

                case BitmapRotation.Clockwise270Degrees:
                    var transform3 = Matrix3x2.CreateRotation(MathFEx.ToRadians(270));
                    transform3.Translation = new Vector2(0, size.X);
                    session.Transform      = transform3;
                    break;
                }

                switch (flip)
                {
                case BitmapFlip.Horizontal:
                    switch (rotation)
                    {
                    case BitmapRotation.Clockwise90Degrees:
                    case BitmapRotation.Clockwise270Degrees:
                        session.Transform = Matrix3x2.Multiply(session.Transform, Matrix3x2.CreateScale(1, -1, canvasSize / 2));
                        break;

                    default:
                        session.Transform = Matrix3x2.Multiply(session.Transform, Matrix3x2.CreateScale(-1, 1, canvasSize / 2));
                        break;
                    }
                    break;

                case BitmapFlip.Vertical:
                    switch (rotation)
                    {
                    case BitmapRotation.None:
                    case BitmapRotation.Clockwise180Degrees:
                        session.Transform = Matrix3x2.Multiply(session.Transform, Matrix3x2.CreateScale(1, -1, canvasSize / 2));
                        break;

                    default:
                        session.Transform = Matrix3x2.Multiply(session.Transform, Matrix3x2.CreateScale(-1, 1, canvasSize / 2));
                        break;
                    }
                    break;
                }

                session.Transform = Matrix3x2.Multiply(session.Transform, Matrix3x2.CreateScale(scaleX, scaleY));
                session.Transform = Matrix3x2.Multiply(session.Transform, Matrix3x2.CreateTranslation(-(offsetX * size.X), -(offsetY * size.Y)));

                foreach (var builder in strokes)
                {
                    PencilCanvas.DrawPath(session, builder, size);
                }
            }

            using (var session = canvas2.CreateDrawingSession())
            {
                session.DrawImage(bitmap);
                session.DrawImage(canvas1);
            }

            bitmap.Dispose();

            using (var stream = await file.OpenAsync(FileAccessMode.ReadWrite))
            {
                await canvas2.SaveAsync(stream, CanvasBitmapFileFormat.Jpeg /*, 0.77f*/);
            }

            canvas2.Dispose();
            canvas1.Dispose();

            return(file);
        }
Esempio n. 3
0
        public static async Task <StorageFile> CropAsync(StorageFile sourceFile, StorageFile file, Rect cropRectangle, int min = 1280, int max = 0, double quality = 0.77, BitmapRotation rotation = BitmapRotation.None, BitmapFlip flip = BitmapFlip.None)
        {
            if (file == null)
            {
                file = await ApplicationData.Current.TemporaryFolder.CreateFileAsync("crop.jpg", CreationCollisionOption.ReplaceExisting);
            }

            using (var fileStream = await OpenReadAsync(sourceFile))
                using (var outputStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    var decoder = await BitmapDecoder.CreateAsync(fileStream);

                    var cropWidth  = (double)decoder.PixelWidth;
                    var cropHeight = (double)decoder.PixelHeight;

                    if (decoder.PixelWidth > 1280 || decoder.PixelHeight > 1280)
                    {
                        double ratioX = 1280d / cropWidth;
                        double ratioY = 1280d / cropHeight;
                        double ratio  = Math.Min(ratioX, ratioY);

                        cropWidth  *= ratio;
                        cropHeight *= ratio;
                    }

                    cropRectangle = new Rect(
                        cropRectangle.X * decoder.PixelWidth,
                        cropRectangle.Y * decoder.PixelHeight,
                        cropRectangle.Width * decoder.PixelWidth,
                        cropRectangle.Height * decoder.PixelHeight);

                    if (rotation != BitmapRotation.None)
                    {
                        cropRectangle = RotateArea(cropRectangle, decoder.PixelWidth, decoder.PixelHeight, (int)rotation);
                    }

                    if (flip == BitmapFlip.Horizontal)
                    {
                        cropRectangle = FlipArea(cropRectangle, decoder.PixelWidth);
                    }

                    var(scaledCrop, scaledSize) = Scale(cropRectangle, new Size(decoder.PixelWidth, decoder.PixelHeight), new Size(cropWidth, cropHeight), min, max);

                    var bounds = new BitmapBounds();
                    bounds.X      = (uint)scaledCrop.X;
                    bounds.Y      = (uint)scaledCrop.Y;
                    bounds.Width  = (uint)scaledCrop.Width;
                    bounds.Height = (uint)scaledCrop.Height;

                    var transform = new BitmapTransform();
                    transform.ScaledWidth       = (uint)scaledSize.Width;
                    transform.ScaledHeight      = (uint)scaledSize.Height;
                    transform.Bounds            = bounds;
                    transform.InterpolationMode = BitmapInterpolationMode.Linear;
                    transform.Rotation          = rotation;
                    transform.Flip = flip;

                    var pixelData = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, transform, ExifOrientationMode.RespectExifOrientation, ColorManagementMode.DoNotColorManage);

                    var propertySet  = new BitmapPropertySet();
                    var qualityValue = new BitmapTypedValue(quality, PropertyType.Single);
                    propertySet.Add("ImageQuality", qualityValue);

                    var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.JpegEncoderId, outputStream);

                    encoder.SetSoftwareBitmap(pixelData);
                    await encoder.FlushAsync();
                }

            return(file);
        }
        private async void Flip_Click(object sender, RoutedEventArgs e)
        {
            var flip     = _flip;
            var rotation = _rotation;

            var proportions = Cropper.Proportions;
            var rectangle   = FlipArea(Cropper.CropRectangle);

            switch (rotation)
            {
            case BitmapRotation.Clockwise90Degrees:
            case BitmapRotation.Clockwise270Degrees:
                switch (flip)
                {
                case BitmapFlip.None:
                    flip = BitmapFlip.Vertical;
                    break;

                case BitmapFlip.Vertical:
                    flip = BitmapFlip.None;
                    break;

                case BitmapFlip.Horizontal:
                    flip     = BitmapFlip.None;
                    rotation = rotation == BitmapRotation.Clockwise90Degrees
                                ? BitmapRotation.Clockwise270Degrees
                                : BitmapRotation.Clockwise90Degrees;
                    break;
                }
                break;

            case BitmapRotation.None:
            case BitmapRotation.Clockwise180Degrees:
                switch (flip)
                {
                case BitmapFlip.None:
                    flip = BitmapFlip.Horizontal;
                    break;

                case BitmapFlip.Horizontal:
                    flip = BitmapFlip.None;
                    break;

                case BitmapFlip.Vertical:
                    flip     = BitmapFlip.None;
                    rotation = rotation == BitmapRotation.None
                                ? BitmapRotation.Clockwise180Degrees
                                : BitmapRotation.None;
                    break;
                }
                break;
            }

            _flip     = flip;
            _rotation = rotation;
            await Cropper.SetSourceAsync(_file, _rotation, flip, proportions, rectangle);

            //Transform.ScaleX = _flip == BitmapFlip.Horizontal ? -1 : 1;
            //Transform.ScaleY = _flip == BitmapFlip.Vertical ? -1 : 1;

            Flip.IsChecked = _flip != BitmapFlip.None;
            Canvas.Invalidate();
        }
Esempio n. 5
0
 ///<summary>
 ///Draws an unstretched bitmap.
 ///</summary>
 ///<param name="material">
 ///The material used when rendering. 
 ///The material must implement <see cref="ITextureMaterial"/>.
 ///</param>
 ///<param name="position">
 ///Where to draw the texture in 2d coordinates.
 ///</param>
 ///<param name="flipMode">
 ///Any flipping to be done of the source texture.
 ///</param>
 public static void Bitmap(
   RenderMaterial material,
   Vector2 position,
   BitmapFlip flipMode)
 {
     DrawUtil.Bitmap(
         material,
         position + Instance._defaultOffset,
         flipMode);
 }
Esempio n. 6
0
 ///<summary>
 ///Draws a stretched sub-region of a texture.
 ///</summary>
 ///<param name="material">
 ///The material used when rendering. 
 ///The material must implement <see cref="ITextureMaterial"/>.
 ///</param>
 ///<param name="dstRect">
 ///Rectangle where the texture object will be drawn.
 ///</param>
 ///<param name="srcRect">
 ///Sub-region of the texture that will be applied over the
 ///<paramref name="dstRect"/>.
 ///</param>
 ///<param name="flipMode">
 ///Any flipping to be done of the source texture.
 ///</param>
 public static void BitmapStretchSR(
   RenderMaterial material,
   RectangleF dstRect,
   RectangleF srcRect,
   BitmapFlip flipMode)
 {
     dstRect.Point += Instance._defaultOffset;
     DrawUtil.BitmapStretchSR(material, dstRect, srcRect, flipMode);
 }
Esempio n. 7
0
        /// <summary>
        /// Draws a stretched sub-region of a texture.
        /// </summary>
        /// <param name="material">The material used when rendering. The material must implement ITextureMaterial.</param>
        /// <param name="dstRect">Rectangle where the texture object will be drawn.</param>
        /// <param name="srcRect">Sub-region of the texture that will be applied over the <paramref name="dstRect"/>.</param>
        /// <param name="flipMode">Any flipping to be done of the source texture.</param>
        public static void BitmapStretchSR(RenderMaterial material, RectangleF dstRect, RectangleF srcRect, BitmapFlip flipMode)
        {
            // setup render state
            GUICanvas.Instance.RenderState.World.LoadIdentity();
            GUICanvas.Instance.RenderState.View = Matrix.Identity;
            GUICanvas.Instance.RenderState.Projection = _clipMatrix;

            // setup the material
            material.SetupEffect(GUICanvas.Instance.RenderState, null);

            _texLeft = srcRect.X / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Width;
            _texRight = (srcRect.X + srcRect.Width) / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Width;
            _texTop = srcRect.Y / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Height;
            _texBottom = (srcRect.Y + srcRect.Height) / ((Texture2D)((ITextureMaterial)material).Texture.Instance).Height;

            _screenLeft = dstRect.X;
            _screenRight = dstRect.X + dstRect.Width;
            _screenTop = dstRect.Y;
            _screenBottom = dstRect.Y + dstRect.Height;

            // flip x
            if ((flipMode & BitmapFlip.FlipX) != 0)
            {
                float temp = _texLeft;
                _texLeft = _texRight;
                _texRight = temp;
            }

            // flip y
            if ((flipMode & BitmapFlip.FlipY) != 0)
            {
                float temp = _texTop;
                _texTop = _texBottom;
                _texBottom = temp;
            }

            color = _bitmapModulation;

            _vertexSet4[0].Position = new Vector3(_screenLeft - 0.5f, _screenTop - 0.5f, 0.0f);
            _vertexSet4[0].TextureCoordinate = new Vector2(_texLeft, _texTop);
            _vertexSet4[0].Color = color;

            _vertexSet4[1].Position = new Vector3(_screenRight - 0.5f, _screenTop - 0.5f, 0.0f);
            _vertexSet4[1].TextureCoordinate = new Vector2(_texRight, _texTop);
            _vertexSet4[1].Color = color;

            _vertexSet4[2].Position = new Vector3(_screenLeft - 0.5f, _screenBottom - 0.5f, 0.0f);
            _vertexSet4[2].TextureCoordinate = new Vector2(_texLeft, _texBottom);
            _vertexSet4[2].Color = color;

            _vertexSet4[3].Position = new Vector3(_screenRight - 0.5f, _screenBottom - 0.5f, 0.0f);
            _vertexSet4[3].TextureCoordinate = new Vector2(_texRight, _texBottom);
            _vertexSet4[3].Color = color;

            // adltodo: hacks
            _workingRenderInstance = SceneRenderer.RenderManager.AllocateInstance();
            _workingRenderInstance.ObjectTransform = Matrix.Identity;

            GUICanvas.Instance.RenderState.Gfx.Device.VertexDeclaration = GFXVertexFormat.GetVertexDeclaration(GUICanvas.Instance.RenderState.Gfx.Device);

            // draw the vertices
            while (material.SetupPass())
            {
                material.SetupObject(_workingRenderInstance, GUICanvas.Instance.RenderState);

                GUICanvas.Instance.RenderState.Gfx.Device.DrawUserPrimitives<GFXVertexFormat.PCTTBN>(PrimitiveType.TriangleStrip, _vertexSet4, 0, 2);
            }

            // cleanup the material
            material.CleanupEffect();
            SceneRenderer.RenderManager.FreeInstance(_workingRenderInstance);
        }
Esempio n. 8
0
        /// <summary>
        /// Draws a stretched bitmap.
        /// </summary>
        /// <param name="material">The material used when rendering. The material must implement ITextureMaterial.</param>
        /// <param name="dstRect">Rectangle where the texture object will be drawn.</param>
        /// <param name="flipMode">Any flipping to be done of the source texture.</param>
        public static void BitmapStretch(RenderMaterial material, RectangleF dstRect, BitmapFlip flipMode)
        {
            Assert.Fatal(material != null, "No material specified for DrawUtil::BitmapStretch");

            if (((ITextureMaterial)material).Texture.IsNull)
            {
                Texture2D texture = ((Texture2D)ResourceManager.Instance.LoadTexture((material as ITextureMaterial).TextureFilename).Instance);
                subRegion = new RectangleF(0.0f, 0.0f, texture.Width, texture.Height);
            }
            else
            {
                subRegion = new RectangleF(0.0f, 0.0f, ((Texture2D)((ITextureMaterial)material).Texture.Instance).Width, ((Texture2D)((ITextureMaterial)material).Texture.Instance).Height);
            }

            DrawUtil.BitmapStretchSR(material, dstRect, subRegion, flipMode);
        }
Esempio n. 9
0
        /// <summary>
        /// Draws an unstretched sub-region of a texture.
        /// </summary>
        /// <param name="material">The material used when rendering. The material must implement ITextureMaterial.</param>
        /// <param name="position">Where to draw the texture in 2d coordinates.</param>
        /// <param name="srcRect">Sub-region of the texture to be drawn.</param>
        /// <param name="flipMode">Any flipping to be done of the source texture.</param>
        public static void BitmapSR(RenderMaterial material, Vector2 position, RectangleF srcRect, BitmapFlip flipMode)
        {
            Assert.Fatal(material != null, "No texture specified for DrawUtil::BitmapSR");

            RectangleF stretch = new RectangleF(position.X, position.Y, srcRect.Width, srcRect.Height);
            DrawUtil.BitmapStretchSR(material, stretch, srcRect, flipMode);
        }