daniel-luberda / DLToolkit.Forms.Controls

Xamarin.Forms Custom Controls
Apache License 2.0
394 stars 182 forks source link

ImageCrop not working with zoomFactor #235

Open JoacimWall opened 5 years ago

JoacimWall commented 5 years ago

When i use the ImageCropControl and zoom. The image is saved without zoom.

Viewmodel:Code

var model = modelIn as ImageCropModel; HeightRequest = model.HeightRequest; WidthRequest = model.WidthRequest; CompletionMessage = model.CompletionMessage; CancelMessage = model.CancelMessage; if (model.IsCircle) { PreviewTransformations = new List() { new CircleTransformation() }; } else {

                PreviewTransformations = new List<ITransformation>() { new CropTransformation() };
            }
            SavedImage = ImageSource.FromStream(() =>
            {
                var stream = model.Media.GetStream();
                return stream;
            });

xaml:Code

    <imgcrop:ImageCropView Margin="3" Grid.Row="1"  HeightRequest="{Binding HeightRequest}" WidthRequest="{Binding WidthRequest}" x:Name="cropView"
                    ManualOffsetX="{Binding XOffset}" ManualOffsetY="{Binding YOffset}" ManualZoom="{Binding Zoom}" Aspect="AspectFill"
                    PreviewTransformations="{Binding PreviewTransformations}"
                    ImageRotation="{Binding Rotation}" 
                    Source="{Binding SavedImage}"/>

CodeBehind on save var result = await cropView.GetImageAsJpegAsync(); byte[] bytes = null;

            using (MemoryStream ms = new MemoryStream())
            {
                result.CopyTo(ms);
                bytes = ms.ToArray();
            }

            var imageSource = ImageSource.FromStream(() =>
            {
                return new MemoryStream(bytes);
            });
            //Console.WriteLine(Preview.Width);
            var cropViewModel = BindingContext as ImageCropViewModel;
            if (cropViewModel != null)
            {
                cropViewModel.SavedImage = imageSource;
                MessagingCenter.Send<byte[]>(bytes, cropViewModel.CompletionMessage);
            }

            await Navigation.PopModalAsync();

And i see in your function GetImageAsJpegAsync() that the zoomFactor is set for example 1.7.

One other question is that there is some sort of padding 10% orund the border is not showing in the preview but is there on export of the picture.

Regards Joacim

JoacimWall commented 5 years ago

Hi If i use CircleTransformation then it works with ZoomFator. One other issue is that the hole picture is not visible inside the crop canvas. We try to crop image with HeightRequest 180 and WidthRequest 370. if i use standard picture taken with Iphone 8 an portrait then i am not able to select left or rigt side of the picture.