简体   繁体   English

自动裁剪图像并调整其大小

[英]Automatically crop and resize an image

I Need to crop and resize an Image in my WPF application as soon as the Picture is loaded. 图片加载后,我需要在WPF应用程序中裁剪图像并调整其大小。 So, my Basic Image has a VGA size (640x480), and I Need to crop the edges (top by 18 Pixels, bottom by 36 Pixels, left by 48 Pixels, and right by 24 pixels). 因此,我的基本图像具有VGA大小(640x480),我需要裁切边缘(顶部18像素,底部36像素,左侧48像素,右侧24像素)。 The new image (which is 568 x 426 pixels) Need to be refitted into the original size (640 x 480 pixels) - basically it's like a digital zoomc that we're using in photography. 新图像(568 x 426像素)需要重新调整为原始尺寸(640 x 480像素)-基本上就像我们在摄影中使用的数码变焦一样。

I've already found some sample code ( Cropping whitespace from image in C# ) - this is however a Little bit too complicated since I don't Need to detect the Whitespace on the image. 我已经找到了一些示例代码( 在C#中从图像中裁剪空白 )-但这有点太复杂了,因为我不需要检测图像上的空白。 Is there any simple algorithm just by using XAML to do this ? 是否有任何简单的算法通过使用XAML来做到这一点?

Thanks in advance. 提前致谢。

I think that you should be able to do that by using the Viewbox Class . 我认为您应该可以使用Viewbox Class做到这一点。 From the linked page: Defines a content decorator that can stretch and scale a single child to fill the available space . 在链接的页面上: 定义一个内容装饰器,该装饰器可以拉伸和缩放单个孩子来填充可用空间 You literally add one to your Window and set your Image as the contents and then you can set properties to control which part of the image it displays: 您可以将一个字面上添加到Window ,并将Image设置为内容,然后可以设置属性来控制它显示图像的哪一部分:

<ViewBox Width="500" Height="500" Stretch="Uniform">
    <Image Source="Images/SomeImage.jpg" Width="300" Height="300" 
        Margin="-48,-18,-36,-24" />
</ViewBox>

Experiment with the different StretchDirection values and set the Margin to negative values to crop. 试验不同的StretchDirection值,并将“ Margin设置为负值以进行裁剪。 There are examples in the linked page, but let me know if you need more help. 链接页面中有一些示例,但是如果您需要更多帮助,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM