10.2 Application Example--Mobile
The move example is a function that intercepts a portion of a picture, and the user can select the captured image range. That example uses the Manipulationdelta event to make the selection of the Intercept interval. The picture is then partially intercepted using the clip property of the UIElement element.
Here's an example of a move: the example has 3 main logic, namely, the selection of regions, the partial interception of images, and the presentation.
Code Listing 10-3 : Move (Source code: 10th Chapter \examples_10_3)
MainPage.xaml File Main code: On the UI Image1 is the display of the picture, Image2 is the picture after the interception, the grid control named LayoutRoot is the container of the picture and the area. --------------------------------------------------------------------------------------------------------------- --- <ScrollViewer> <StackPanel> <Gridx:name= "LayoutRoot"> <ImageSource= "/test.jpg"Height= "460"Width= "+"Name= "Image1"/> </Grid> <ButtonContent= "Cut"x:name= "button"></Button> <ImageName= "Image2" /> </StackPanel> </ScrollViewer>
Selection of 10.2.1 Area
The choice of area is to animate the way the finger slides to control the position and size, then the logic in this example is the center of the center of the photo, then the user can slide to change the width and height of the rectangle. Then we realize that the idea is to add a rectangle control rectangle above the picture, and then add the Manipulationdelta event to the rectangle control, and monitor the user's slide above the rectangle, Adjusts the size of the rectangle control on handlers for the Manipulationdelta event. The code looks like this:
MainPage.xaml.cs File Section code: Selection of Regions. --------------------------------------------------------------------------------------------------------------- --- PublicMainPage () { This. InitializeComponent (); button. Click+=Button_Click; //set the area above the picturesetpicture (); } //add an area of a picture voidsetpicture () {//Create a rectangle controlRectangle rect =NewRectangle (); Rect. Opacity=0.5; Rect. Fill=NewSolidColorBrush (Colors.white); Rect. Height=Image1. Height; Rect. Width=Image1. Width; Rect. Stroke=NewSolidColorBrush (colors.red); Rect. StrokeThickness=2; Rect. Margin=Image1. Margin; //adding event monitoring to the touch slide processRect. Manipulationmode =Manipulationmodes.all; Rect. Manipulationdelta+=Rect_manipulationdelta; //Add the rectangle control to the LayoutRoot, and the control will appear above the pictureLayoutRoot.Children.Add (rect); Layoutroot.height=Image1. Height; Layoutroot.width=Image1. Width; } //use your finger swipe to change the position and size of the box voidRect_manipulationdelta (Objectsender, Manipulationdeltaroutedeventargs e) { //gets the sender of the event, that is, the zone rectangle controlRectangle Croppingrectangle =(Rectangle) sender; //Change the width and height of the rectangle control by the displacement of the finger//Sliding width to right (x positive), left slide width increased (x is negative) if(Croppingrectangle.width >= (int) e.delta.translation.x) croppingrectangle.width-= (int) e.delta.translation.x; //Downward slide height decrease (x is positive), upward slide height (x is negative) if(Croppingrectangle.height >= (int) e.delta.translation.y) croppingrectangle.height-= (int) E.DELTA.TRANSLATION.Y; }Partial interception of 10.2.2 pictures
After the area has been positioned, the next step is to intercept the image based on the location of the area, so the main logic in this step is to convert the position size information of the rectangle control into the area information of the relative position within the image1 image. The clip property is then intercepted. The code looks like this:
MainPage.xaml.cs file part of the code: the partial interception of the picture. --------------------------------------------------------------------------------------------------------------- ---//the area where the picture is captured voidClipimage () {//Create a rectangular geometry to assign to the clip property, note: The geometry used as a property must be a *geometry type of graphicRectangleGeometry Geo =NewRectangleGeometry (); //gets the rectangle control to look down through the grid containerRectangle r = (Rectangle) ( fromCinchLayoutroot.childrenwhereC.opacity = =0.5 Selectc). First (); //converts the position information of a rectangle control into a position relative to the grid containerGeneraltransform GT =r.transformtovisual (LayoutRoot); //gets the coordinates of the upper-left corner of the area, meaning that the coordinates of the original R's upper-left coordinate (0, 0) are converted on the LayoutRootPoint P = GT. Transformpoint (NewPoint (0,0)); //Create a region relative to the LayoutRootGeo. Rect =NewRect (p.x, P.y, R.width, r.height); Image1. Clip=Geo; //hide the controls.R.visibility =Windows.UI.Xaml.Visibility.Collapsed; }10.2.3 's Show
Display refers to the final interception of the image displayed, through the clip properties of the picture is taken out, in fact, it is not to cut the picture, it just the other parts of the area to block it, then to the real region to get out, you can use the RenderTargetBitmap class to achieve. The RenderTargetBitmap class can convert a UI element object to a bitmap. The code looks like this:
MainPage.xaml.cs file part of the code: the display. --------------------------------------------------------------------------------------------------------------- ---//Cut button Events Async voidButton_Click (Objectsender, RoutedEventArgs e) { //call Clipimage method to implement partial interception of picturesClipimage (); //Create a RenderTargetBitmap object and call the Renderasync method to convert the UI element LayoutRoot into a RenderTargetBitmap object varBitmap =NewRenderTargetBitmap (); awaitbitmap. Renderasync (LayoutRoot); //since the RenderTargetBitmap class is originally derived from the ImageSource class, it can be copied directly to the picture control for displayImage2. Source =bitmap; }
This article is from the "in-depth Windows Phone 8.1 application Development"
WP8.1 Runtime article list: http://www.cnblogs.com/linzheng/p/3998037.html
Source code Download: Http://vdisk.weibo.com/s/zt_pyrfNHb99O
Welcome to follow my Weibo @wp forestry Administration public Number: WP Development (No.: WPKAIFA)
WP8.1 Technology Group: 372552293
[WP8.1 (Runtime)] Application example--Mobile