【问题标题】:Crop image with rectangle用矩形裁剪图像
【发布时间】:2013-10-07 17:39:12
【问题描述】:

我有一个图像,我想用一个矩形来裁剪它,下面的代码是我放置一个图像并在图像中间绘制一个矩形的代码:

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>

我成功显示图像并绘制了一个矩形。示例图片如下:

现在我想单击一个按钮来裁剪矩形内的图像(不是自动裁剪)。加载图像时会自动添加矩形。所以不能使用“Point Pressed”和“Point Released”。并且也不能使用“rectangle.clip”,因为它会自动剪辑图像。我该如何解决?谢谢

更新: 我可以移动图像,如何绑定数据并将矩形坐标设置为动态?下面的代码是转换图像。谢谢。

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 the 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;
        }
}

【问题讨论】:

    标签: windows-8 windows-runtime microsoft-metro windows-store-apps winrt-xaml


    【解决方案1】:

    我没有使用过 XAML,因为它给我带来了困惑。所以我根据你的问题创建了一个 sn-p。试试看,让我知道结果。我使用了与您发布的相同的图像

    XAML

    <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://i.stack.imgur.com/UIBSp.png" />
        <Path x:Name="path" Stroke="Red" StrokeThickness="3">
            <Path.Data>
                <RectangleGeometry Rect="545,212,440,420"/>
            </Path.Data>
        </Path>
    </Grid>
    

    C#

    private void btnCrop_Click(object sender, RoutedEventArgs e)
    {
        var _rect = new RectangleGeometry();
        _rect.Rect = path.Data.Bounds;
        photo.Clip = _rect;
    }
    

    【讨论】:

    • 感谢您的回复。但是在我点击裁剪按钮后,图像就消失了。
    • 你期望的输出是什么? Crop 只做那件事。
    • 我想要的是裁剪矩形内的图像,但是点击裁剪后,整个图像丢失了。
    • 这些不是你预期的输出i.imgur.com/R5878Rg.png & i.imgur.com/HFqNGfu.png吗?
    • 对不起,是的!它现在正在工作。但是 是硬编码坐标。我的图像可以四处移动,所以即使我移动到其他点,它仍然是在同一个地方裁剪。你知道怎么做吗?谢谢
    猜你喜欢
    • 2021-06-21
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多