1

I was previosuly using WriteableBitmapEX library for cropping images whereever my mouse moves. It seems to be a bit slow.

I want to crop the image at any random pixels and I want to asssign that cropped region to another image control .

My problem is when I am using the clip property , I am only getting the clipped region left and the whole image is going. I want the image completely to be in the background but the cropped region should be assigned to the image control .

Here's the code .

 private void Image1_Tapped(object sender, TappedRoutedEventArgs e)
        {
            int CropArea = 50;
            int PointShift = CropArea / 2;
            var _rect = new RectangleGeometry();
            Point pt;
            pt = e.GetPosition(Image1);

            _rect.Rect = new Rect(pt.X - PointShift, pt.Y - PointShift, 100, 100);
            Image1.Clip = _rect;
            MagnifyTip.Image1.Source=Image1.clip; //This is what I want to do . Its not happenning. 
        }


<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <Image x:Name="Image1" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="Image1_Tapped" >
            <Image.Source >
                <BitmapImage UriSource="Assets/Jellyfish.png" />
            </Image.Source>
        </Image>
    </Grid>

Any better solutions are welcomed becuase I have to keep on moving my finger around the image and get the updated pixel by pixel cropped image in my imagebox in my user control

Apoorv
  • 2,023
  • 1
  • 19
  • 42

2 Answers2

1

Well you don't want to clip the image you want in the background, you want two separate image controls and only clip the image you are magnifying, here is how I would do it using a Canvas and two image controls.

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Canvas HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Name="canvas">
        <Image Canvas.ZIndex="0"  x:Name="Image1" ImageOpened="Image1_Opened" Stretch="Uniform" HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="Image1_Tapped" >
            <Image.Source>
                <BitmapImage UriSource="Assets/JellyFish.png" />
            </Image.Source>
        </Image>
        <Image Canvas.ZIndex="1" x:Name="MagnifyTip" Visibility="Collapsed">
            <Image.Source >
                <BitmapImage UriSource="Assets/JellyFish.png" />
            </Image.Source>
        </Image>
    </Canvas>
</Grid>

Once your background image is loaded you want to set the sizes for images in the canvas.

    double _scaleX = 5;
    double _scaleY = 5;
    double _croppedImageWidth = 100;
    double _croppedImageHeight = 100;

    private void Image1_Opened(object sender, RoutedEventArgs e)
    {
        this.Image1.MaxHeight = this.canvas.ActualHeight;
        this.Image1.MaxWidth = this.canvas.ActualWidth;
        this.MagnifyTip.MaxHeight = this.canvas.ActualHeight;
        this.MagnifyTip.MaxWidth = this.canvas.ActualWidth;
        this.MagnifyTip.RenderTransform = new ScaleTransform() 
        { 
            ScaleX = _scaleX,
            ScaleY = _scaleY
        };
    }

Then you can set the location and scale of your magnified image in Image1_Tapped handler.

    private void Image1_Tapped(object sender, TappedRoutedEventArgs e)
    {
        Point pt = e.GetPosition(this.canvas);
        this.MagnifyTip.Clip = new RectangleGeometry()
        {
            Rect = new Rect()
            {
                X = pt.X, 
                Y = pt.Y, 
                Width = _croppedImageWidth / _scaleX,
                Height = _croppedImageHeight / _scaleY
            }
        };
        Canvas.SetLeft(this.MagnifyTip, -pt.X * (_scaleX - 1));
        Canvas.SetTop(this.MagnifyTip, -pt.Y * (_scaleY - 1));

        this.MagnifyTip.Visibility = Visibility.Visible;
    }

The important points are

  • Use Canvas Left, Top and ZIndex properties to overlay the magnified image over the background image.
  • Calculate clip size and location for magnified image based off of the click point and size of your scale.
  • Use ScaleTransform to magnify the image.
Bilal Bashir
  • 1,443
  • 14
  • 18
  • how would it work with my usercontrol as there is an image box inside the control . I want to tap my finger on the parent image, my user control appears with the cropped area around my finger , now I keep on moving my finger around the image and whereever my finger is , my user control is getting updated with the new cropped image ! Currently I would like to drop the idea of magnification. – Apoorv Mar 14 '15 at 21:18
  • the movement of the user control is very necessary. he user control can move top , bottom , right , left and that with the finger.so sometime my finger would be on the user control to move it up . check this http://stackoverflow.com/questions/28740086/zoom-cropped-pixels-in-a-user-control/28900396?noredirect=1#comment46134330_28900396 – Apoorv Mar 14 '15 at 21:21
  • Is there a reason you can't put your usercontrol in the canvas as well? – Bilal Bashir Mar 14 '15 at 21:27
  • no specific reason ! but do u really think It would work as efficient as I want it to be? My WriteableBitmap sucks . and how to dynamically assign my user control's imagebox to the image? check my user control XAML . http://stackoverflow.com/questions/28988606/not-getting-the-exact-cropped-region-using-writeablebitmapex-library-in-win-rt-a – Apoorv Mar 14 '15 at 21:30
  • Esentially instead of updating the image itself in the InkCanvas_PointerMoved you would just want to set the transform property ex. MagnifyTip.image1.Transform = new ScaleTransform() { CenterX = xValue1/4, CenterY = yValue1/4, ScaleX = 2, ScaleY = 2 }; – Bilal Bashir Mar 14 '15 at 22:31
  • And this would give me the cropped Image everytime on my pointer moved. But what If I dont want to scale the image. Just show the cropped region ? – Apoorv Mar 15 '15 at 03:56
  • I tried doin that. Don't know whats not working for me. Please look into the project. I have added my user control too. https://onedrive.live.com/redir?resid=70BF0E66B131F8C7!23751&authkey=!ABoARj6lZOGy0Os&ithint=file%2crar – Apoorv Mar 15 '15 at 08:44
  • The image shouldn't be an imagebrush, too many canvases, here take a look at this it isn't perfect but should be enough to get you started, only part seems to be rounding the corners https://onedrive.live.com/redir?resid=C24515A7B235C447%21734 – Bilal Bashir Mar 15 '15 at 16:51
  • Did u modify my code? I am on a mac system. See , I need that user control to show the cropped image and I want the cropping to be awesome! The user control should move with my finger and rotate as per requirenment ! – Apoorv Mar 15 '15 at 17:12
  • rounding at the corners has to be done with border only ! Anyhow , I see that you have put on a tag that seems to be commented ! Will check this solution on tuesday and will get back to u ! – Apoorv Mar 15 '15 at 17:22
  • why cant I put that image control directly inside the user control rather than putting it on MainPage.XAML ? – Apoorv Mar 18 '15 at 05:57
  • Actually its good ! but its not helpig me. I need it to be prefect and I am trying so many things. The image would be dynamic and the user control has to clip ..check the solution I uploaded ! – Apoorv Mar 18 '15 at 06:22
  • That happens because I didn't hide the image at the start, I updated it. – Bilal Bashir Mar 19 '15 at 02:39
  • try to put the image inside the user control . I am having issues with it..the image control is getting bigger and not looking good inside the user control I made – Apoorv Mar 19 '15 at 08:22
  • Bro, I am working on a very big project and this cropping should happen exactly the way it happens in the video. The user loads the image dynamically using file picker and that user control should crop the image . The user control should come just above where I tap and hold for a while and moves with my finger . Same thing i have to develop. No deviations – Apoorv Mar 20 '15 at 03:03
  • I hear you but I have a day job. What you could try to do is make three image controls that fit under the border of your custom control and it will basically do what you want. Basically replicate the same behavior for all the image controls just offset in height or width which ever fits better. – Bilal Bashir Mar 20 '15 at 03:41
  • Thanks for your reply bro ! I understand it !but i have been trying so many solutions and nothing seems to work for me ! If possible please take this weekend and frame the solution you want me to use ! I will be very thankful to you – Apoorv Mar 20 '15 at 04:48
  • @Slowbrochaocho - Link sir ? – Apoorv Mar 22 '15 at 05:18
  • Sir , Thanks! I would see this one !! I am also not on a Windows PC now !! I will give you bounty coz you have been trying so hard ! Can we talk on chat? – Apoorv Mar 22 '15 at 05:24
  • would you be able to initiate the chat in stackoverflow – Apoorv Mar 22 '15 at 05:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/73506/discussion-between-slowbrochacho-and-apoorv). – Bilal Bashir Mar 22 '15 at 05:46
0

A few things to note for cropping

  1. Use a RectangleGeometry for the Clip property of your image (you already do that)
  2. When adjusting the crop geometry - use its Transform property rather than creating a new geometry every time you want to recrop.
  3. Updating a WriteableBitmap is fairly slow, so you might be able to do that once in a while - e.g. when you commit the crop rectangle, but not in real time like on each PointerMove event.
  4. If you want real time updates you can either
    • Use another Image element with another RectangleGeometry Clip with a transform being a scaled version of the original transform. You could also use a copy of the original transform and scale the entire Image instead.
    • Use DirectX to render transformed output in a SwapChainPanel.
  5. For the final high resolution output you can use any method since processing speed is less of a factor
    • You could use RenderTargetBitmap.RenderAsync() to render your cropped image at screen resolution. You might need to wrap the clipped image in another element like a Grid.
    • You can use WriteableBitmap to do the processing - I believe the WriteableBitmapEx project on CodePlex has methods for rotating and cropping. This is CPU based though.
    • You can use DirectX, but that could be an overkill.
    • You can use WIC with BitmapDecoder, though that has limited rotation capabilities: How to resize Image in C# WinRT/winmd?
Community
  • 1
  • 1
Filip Skakun
  • 31,624
  • 6
  • 74
  • 100
  • I know all this theory I want something to be practical for my question . Can u help me out in my code above? I have used WriteableBitmapEx library for cropping but for larger image it sucks. can u show some demo for 4 and 5 points? I need to keep on cropping on pointermove and keep showing the updated image in the imagecontrol – Apoorv Mar 14 '15 at 17:54
  • I want to know about the performance ? I need great performance .it should move and clip with my finger.. no lags ! which method would be the fastest ? – Apoorv Mar 20 '15 at 03:05
  • Transforming clip geometry should feel fast enough, but if you want fastest - you should go the DirectX route. It's a lot of work though. – Filip Skakun Mar 20 '15 at 03:53
  • any sample for the directX clip .. I have specific question would you please be able to help me ? I need exactly like this shown in video. I am developing Windows 8.1 ap p https://www.youtube.com/watch?v=0Ie1Z-vk4aE&feature=youtu.be – Apoorv Mar 20 '15 at 04:45
  • My app code is in C# , want to crop image on the bacground into my user control on mouse move . so it would be cropping for every pixel moved ! Any help ? – Apoorv Mar 20 '15 at 05:14
  • I found a dll for cropping that is a manged code for DirectX . SharpDX http://sharpdx.org/ . It has a cropping function. But I dont know how to use it . Would u be able to help ? – Apoorv Mar 20 '15 at 08:52
  • Sorry, I don't have that much free time. I do recommend learning sharpdx though and if you hit specific problems - you could probably get some help either here or on http://gamedev.stackexchange.com/ – Filip Skakun Mar 20 '15 at 18:03
  • Sorry, I don't have that much free time. – Filip Skakun Mar 20 '15 at 18:08
  • Fine Sir .It seems to me that you are one of the best guys we have here !! Dont know who will help me If u cant ! None of my questions r being answered :( – Apoorv Mar 20 '15 at 18:10
  • The problem is that you would like someone to provide you a complete solution, but that is quite time consuming in the case of your problem. I have solved this problem a few times before, but this is something that takes a few hours and a lot of code. I don't have that much free time. You'd just be best off trying to solve it yourself. You might find some open source for inspiration too. Did you check the Xaml Crop Control on CodePlex? – Filip Skakun Mar 21 '15 at 22:37
  • I understand you sir ! But what If it is possible for you to share your code ! Our I would share mine where I need cropping done ! Anyways , I need complete code coz I am a Big zero in DirectX. And no tutorials too. Need to implement DirectX – Apoorv Mar 22 '15 at 05:16
  • I'm not a DirectX expert either - it takes a lot of time to write and modify DirectX code and I wrote most of my comments here on my phone. That said - if what you posted in the video is what you need - that is not cropping. It's a non-rectangular clipping region. You can do that in XAML fairly easily if you only need to clip an image. You would use an `Ellipse` shape and a `Fill` brush property set to an [`ImageBrush`](https://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.media.imagebrush.aspx) with a custom `Transform` value. – Filip Skakun Mar 22 '15 at 16:32
  • Ellipse doesnt have a Crop property in Windows 8 I think ! Anyways Thanks for your reply ! – Apoorv Mar 22 '15 at 17:59
  • You don't want to crop the ellipse. You want an ellipse filled with an `ImageBrush` - that will give you a zoomed in circle shaped clip of your image. Note that clipping != cropping. Clipping is typically something you do to show a UIElement clipped from its full dimensions with either rectangular or non-rectangular shape. In WinRT/XAML you only get a `Clip` property you can use with `RectangleGeometry`, but you can still fill a non-rect shape like `Ellipse` or `Path` with `ImageBrush`. Cropping is something you typically do with photos and it involves user input and yields a rectangle. – Filip Skakun Mar 22 '15 at 18:06
  • I was cropping the region and getting it circular in my user control using a border ! The performance was slow ! There is actually no zoom in the image.A high defination image looks like zoomed in. But you are telling me to Clip it ! I will try !! – Apoorv Mar 22 '15 at 18:27
  • Any working demo /code samples that have been implemented in WinRT? – Apoorv Mar 22 '15 at 18:35
  • Like I said - `Ellipse`, `Fill` = `ImageBrush`, `ImageBrush.Transform` = your transform to translate to the point being zoomed in and scaled to your zoom level. The exact calculations will depend on your image and layout. – Filip Skakun Mar 22 '15 at 19:31
  • Sure Sir , I will give it a try tomorrow and would get back to you :) Thanks for posting :) – Apoorv Mar 25 '15 at 13:38
  • It works well , But i need to integrate it with my user control . tried alot today but its not working ! My user control has to show the clipped region . see this please ! http://stackoverflow.com/questions/28988606/not-getting-the-exact-cropped-region-using-writeablebitmapex-library-in-win-rt-a – Apoorv Mar 26 '15 at 14:07
  • The other question is about WriteableBitmapEx. I'd try to get as far as you can and then ask a specific question related to what you are trying to do. – Filip Skakun Mar 26 '15 at 17:47
  • No sir , dont get me wrong ! U saw the video which I mentioned? Right? I have to develop the same thing.Exactly same ! So I thought it was cropping , so tried WriteableBitmap but performance was slow. Then you told about Clipping and it works well. My main requirement is to attach the clipped region into the user control. That question has the link of my XAML of user control ! Thats what I want to do – Apoorv Mar 27 '15 at 04:23
  • umm..so I was asking you how to integrate that user control to contain that ellipse clip code ? – Apoorv Mar 27 '15 at 05:17
  • @Frilip Skakun - Sorry to bother you ! I am facing issues in attaching the ellipse into my user control ! Looking for answers from you ! – Apoorv Apr 01 '15 at 17:33
  • What issues are you facing? – Filip Skakun Apr 01 '15 at 18:32
  • @Frilip Skakun - Sir , I want to attach that Ellipse to my user control. If you see the video , the clipping happens inside a user control. I have the user control as a path . The user control And the path is here http://goo.gl/4Qbqbw . The problem is , My user control is appearing but the ellipse is moving out of the user control . I want to attach it with the user control and the user control should move according to the video – Apoorv Apr 01 '15 at 19:17
  • @Frilip Skakun - I tried the code u wrote and uploaded ! took the Ellipse section and wanted it to be the part of the user control. Dont have that project on this PC !If you dont get it , let me know , I would upload the project – Apoorv Apr 01 '15 at 19:19
  • You need to ask a separate question for each problem you want help solving. I think you are talking about another problem right now or have many separate problems that need separate questions on Stack Overflow. Bear in mind that the way Stack Overflow works is not as a free development contracting service, but a question and answer one. Your questions need to be specific and show effort from your side. Comments are not a good place to ask unrelated questions. – Filip Skakun Apr 01 '15 at 21:27
  • @Frilip Skakun - Added sir . please look http://stackoverflow.com/questions/29405356/clipping-using-ellipse-and-binding-it-inside-my-user-control-in-windows-rt – Apoorv Apr 02 '15 at 05:45
  • Were you able to look into the above posted question ? – Apoorv Apr 05 '15 at 18:49
  • @FilipSkakun- I have implemented the transforms in the code but I am stuck up at a place.Will u be able to spare sometime for helping me ? – Apoorv Apr 18 '15 at 07:55
  • We can talk about my consulting rate if you are interested. Otherwise - feel free to ask more questions on SO. – Filip Skakun Apr 18 '15 at 18:35
  • I would love to pay you but you can go ahead and see that I am a student ! I cant afford to pay a Microsoftie ... I have asked on SO but I dont think people are able to help .. Anyways Thanx :) – Apoorv Apr 18 '15 at 18:42
  • Can you link to a question? – Filip Skakun Apr 18 '15 at 18:45
  • http://stackoverflow.com/questions/29703191/clipping-a-low-resolution-image-using-transforms-and-assigning-it-to-my-user-con – Apoorv Apr 18 '15 at 18:45
  • Sir , I can assure you that when this app get launched with the latest windows version , people would love it , I hope you have seen the video..I am facing serious issues..would like to tell you that I started with cropping and tried all code you suggested in your answer, then the performance was slow ..then u suggested clipping, I went ahead with that with your guidance and back again , I need your help.Such a big community and I crave for answers ! – Apoorv Apr 18 '15 at 18:49
  • It's not a problem of the app being great. I really don't have that much time. Gotta move the kids to the next point right now. If I find some time tonight I'll take a look. If the question is well formed - I might be able to help. – Filip Skakun Apr 18 '15 at 18:51
  • I do understand sir ! I have added the code ! you can download the app from there! Please watch the video.. I need the rotation as it is performed in the video and please take your time ! I have 2 weeks...lets both of us try – Apoorv Apr 18 '15 at 19:06
  • Sir , I have updated the wordings in the question's link above ! Thanks :) – Apoorv Apr 19 '15 at 17:53
  • I hope you will be able to look into it this weekend ! – Apoorv Apr 24 '15 at 18:55
  • It can be both !! Anyhow this will also be released as soon as you publish windows 10 app :) – Apoorv Apr 28 '15 at 18:33
  • I saw the demos of the photos app at BUILD(no i wasnt there) and I was so happy that I know the person working on it ..heheheh..told my friends :) It looked awesome ! Wow !! – Apoorv May 01 '15 at 17:28
  • Really, still? And you haven't tried yourself? Can you remind me where I should go, what to look at and what to try to fix? I might have some time today. – Filip Skakun May 09 '15 at 17:13
  • I tried the rotation. I am not getting it ! I shared the video and asked people to help me get the same rotation in my code ! http://stackoverflow.com/questions/29703191/clipping-a-low-resolution-image-using-transforms-and-assigning-it-to-my-user-con this is the question where the whole code file is pasted ! The video address is http://youtu.be/0Ie1Z-vk4aE . I have updated the code https://onedrive.live.com/redir?resid=77EB8DD46E4379D5!93696&authkey=!AD3ll-bKr_ATrGE&ithint=file%2czip – Apoorv May 09 '15 at 17:23
  • I have fixed point 1 in the question I posted !! not getting exactly same region is point 2 but we can look at that later. The only thing that I am unable to perform is point 3 ,and the rotation as shown in the video ! not getting help ! – Apoorv May 09 '15 at 17:24
  • Isn't that a maths question though? I feel like it's more of a question for http://math.stackexchange.com/ – Filip Skakun May 09 '15 at 17:25
  • not getting answers ! The rotation part ! IF you can help !! thats what all I request from you ! Someone is calling it Lerp Interpolation and all .If i put this on maths they will say this is for C# put it on StackOverflow ! If you can help ? one more request , My user control is going outside my image in both orientations , it shouldnt happen ! any fix ? – Apoorv May 09 '15 at 17:28
  • I answered that other question. – Filip Skakun May 11 '15 at 17:04
  • Sir , I am too much obliged ! My Windows PC crashed and HDD gone ! Please give me some time to execute it and see ! I hope you would be available for further help ! God Bless You ! – Apoorv May 11 '15 at 18:25