windows 8 - Crop image with dynamic rectangle coordinate -
i have image , want crop using rectangle, code below code put image , draw rectangle @ middle of image:
mainpage.xaml:
<canvas x:name="canvas" horizontalalignment="center" verticalalignment="center" width="340" height="480" background="blue"> <image x:name="photo" horizontalalignment="center" verticalalignment="center" manipulationmode="all"> <image.rendertransform> <compositetransform/> </image.rendertransform> </image> <path stroke="black" strokethickness="1"> <path.data> <rectanglegeometry rect="0,0,340,480"/> </path.data> </path> </canvas>
i able move image. code below transform image, , crop function working. how bind data , set rectangle coordinate dynamic instead of hard code coordinate? thanks.
public sealed partial class mainpage: page { private compositetransform compositetranslation; public mainpage() { this.initializecomponent(); photo.manipulationdelta += composite_manipulationdelta; compositetranslation = new compositetransform(); photo.rendertransform = this.compositetranslation; } void composite_manipulationdelta(object sender, manipulationdeltaroutedeventargs e) { // scale image. compositetranslation.centerx = photo.actualwidth / 2; compositetranslation.centery = photo.actualheight / 2; compositetranslation.scalex *= e.delta.scale; compositetranslation.scaley *= e.delta.scale; compositetranslation.translatex += e.delta.translation.x; compositetranslation.translatey += e.delta.translation.y; } private void btncrop_click(object sender, routedeventargs e) { var _rect = new rectanglegeometry(); _rect.rect = path.data.bounds; photo.clip = _rect; } }
using below code, rectangle in middle of screen , image can panned. content within rectangle clipped.
<page.bottomappbar> <appbar issticky="true" isopen="true"> <button content="crop" click="btncrop_click" /> </appbar> </page.bottomappbar> <grid background="{staticresource applicationpagebackgroundthemebrush}"> <image x:name="photo" horizontalalignment="center" verticalalignment="center" manipulationmode="all" source="http://www.wired.com/reviews/wp-content/uploads/2012/10/windows-8-1.jpg" /> <path x:name="path" stroke="red" strokethickness="3" /> </grid>
private compositetransform compositetranslation; rectanglegeometry rect = new rectanglegeometry(); public blankpage4() { this.initializecomponent(); rect.rect = new rect((window.current.bounds.width - 480) / 2, (window.current.bounds.height - 340) / 2, 480, 340); path.data = rect; } protected override void onnavigatedto(navigationeventargs e) { photo.manipulationdelta += composite_manipulationdelta; compositetranslation = new compositetransform(); photo.rendertransform = this.compositetranslation; } private void btncrop_click(object sender, routedeventargs e) { generaltransform gt = photo.transformtovisual(null); point pt = gt.transformpoint(new point(0, 0)); var _rect = new rectanglegeometry(); _rect.rect = new rect((rect.bounds.x / compositetranslation.scalex) - (pt.x / compositetranslation.scalex), (rect.bounds.y / compositetranslation.scaley) - (pt.y / compositetranslation.scaley), 480 / compositetranslation.scalex, 340 / compositetranslation.scaley); photo.clip = _rect; } void composite_manipulationdelta(object sender, manipulationdeltaroutedeventargs e) { // scale image. compositetranslation.centerx = photo.actualwidth / 2; compositetranslation.centery = photo.actualheight / 2; compositetranslation.scalex *= e.delta.scale; compositetranslation.scaley *= e.delta.scale; compositetranslation.translatex += e.delta.translation.x; compositetranslation.translatey += e.delta.translation.y; }
updated: code updated told howard hee solving issue wherein zooming in/out show black screen
Comments
Post a Comment