您的位置:首页 > 其它

WPF- 图像的旋转、转换、裁剪和拉伸

2014-02-11 15:56 267 查看


WPF- 图像的旋转、转换、裁剪和拉伸 (前台)

通过 WPF,用户可以使用 BitmapImage 的属性或使用其他 BitmapSource 对象(如 CroppedBitmap 或 FormatConvertedBitmap)来转换图像。上述图像转换可以缩放或旋转图像、更改图像的像素格式或裁切图像。

第9章习题有关WPF习题中用到的图像源可以从本博客的相册《第9章习题WPF习题图源》得到。

习题9-28 旋转图像

可以使用 BitmapImage的Rotation属性来执行图像旋转,旋转只能以90度的增量来进行。在下面的示例中,图像旋转了90度。

创建一个WPF程序,右击项目名称添加一个文件夹,向文件夹添加现有项,将Win7的示例图片企鹅放入文件夹。

<Window x:Class="BitmapImageRotation.Window1"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

Title="Window1" Height="300" Width="300">

<Image Margin="10">

<Image.Source>

<TransformedBitmap Source="/sampleImages/Penguins.jpg" >

<TransformedBitmap.Transform>

<RotateTransform Angle="90"/>

</TransformedBitmap.Transform>

</TransformedBitmap>

</Image.Source>

</Image>

</Window>



习题9-29 转换图像灰度

可以使用 FormatConvertedBitmap 将图像转换为不同的像素格式,如灰度。在下面的示例中,图像转换为 Gray4。

<Grid>

<!-- Grayscale XAML Image -->

<Image Margin="10" >

<Image.Source>

<FormatConvertedBitmap Source="/sampleImages/Lighthouse.jpg" DestinationFormat="Gray4" />

</Image.Source>

</Image>

</Grid>



习题9-30将多个 BitmapSource 的属性链在一起

此示例演示将多个BitmapSource属性链在一起,来对图像源应用各种效果。下面的示例链接旋转和灰度两个属性。

<Window.Resources>

<!-- 资源定义了BitmapImage的多个属性,包括图像源,DecodePixelWidth宽度, 这个属性设置节省内存 -->

<BitmapImage x:Key="masterImage" DecodePixelWidth="200"

UriSource="/sampleImages/Waterlilies.png"/>

<!-- 下面的属性旋转90度 -->

<TransformedBitmap x:Key="rotatedImage" Source="{StaticResource masterImage}">

<TransformedBitmap.Transform>

<RotateTransform Angle="90" />

</TransformedBitmap.Transform>

</TransformedBitmap>

<!--这个属性用于灰度处理 -->

<FormatConvertedBitmap x:Key="convertFormatImage"

Source="{StaticResource rotatedImage}"

DestinationFormat="Bgra32" />

</Window.Resources>

<StackPanel>

<!-- 引用资源"convertFormatImage"得到宽度、旋转和灰度设置-->

<Image Width="200" Source="{StaticResource convertFormatImage}" />

</StackPanel>



习题9-31 裁剪图像

裁切图像可以使用Image或CroppedBitmap的Clip属性。通常情况下,如果只想调整图像的一部分,则应使用 Clip。如果需要编码和保存裁切过的图像,应使用 CroppedBitmap。下面的示例使用 EllipseGeometry 和 Clip 属性来裁切图像。

<Grid>

<!-- 使用Clip剪裁图像 -->

<Image Width="240" Margin="5" Source="/sampleImages/Waterlilies.png">

<Image.Clip>

<EllipseGeometry Center="120,70" RadiusX="100" RadiusY="50" />

</Image.Clip>

</Image>

</Grid>



习题9-32 拉伸图像

Stretch 属性控制如何拉伸一个图像以使其填充容器。Stretch 属性接受Stretch 枚举定义的以下值:

None,不会拉伸图像以填充输出区域。如果图像比输出区域大,无法容纳的内容将被剪裁掉。

Fill,会拉伸图像以适应输出区域。由于图像的高度和宽度是独立进行缩放的,因此图像的原始长宽比可能不会保留。也就是说,为了完全填充输出容器,图像可能会扭曲。

Uniform,图像进行缩放,以便其完全适应输出区域。图像的长宽比会保留。

UniformToFill,图像会进行缩放,以便在保留图像原始长宽比的同时完全填充输出区域。

下面的图像显示示例的输出,演示了不同的 Stretch 设置在应用到图像时的效果。建立一个WPF项目,添加图像,可用截图办法得到示例的原图形。

<Window.Resources>

<Style TargetType="TextBlock" x:Key="Header1">

<Setter Property="DockPanel.Dock" Value="Top"/>

<Setter Property="FontSize" Value="14"/>

<Setter Property="FontFamily" Value="Trebuchet MS"/>

<Setter Property="Margin" Value="5"/>

</Style>

<Style TargetType="Border" x:Key="borderStyle">

<Setter Property="BorderThickness" Value="1"/>

<Setter Property="BorderBrush" Value="Black"/>

<Setter Property="Margin" Value="5,0,5,0"/>

</Style>

</Window.Resources>

<DockPanel>

<Border DockPanel.Dock="Top" Background="Black">

<TextBlock Foreground="White" HorizontalAlignment="Stretch"

FontSize="20">Stretching an Image</TextBlock>

</Border>

<Grid Name="simpleGrid" Margin="10" ShowGridLines="False"

VerticalAlignment="Center" HorizontalAlignment="Center">

<Grid.ColumnDefinitions>

<ColumnDefinition Width="175" />

<ColumnDefinition Width="175" />

<ColumnDefinition Width="175" />

<ColumnDefinition Width="175" />

</Grid.ColumnDefinitions>

<Grid.RowDefinitions>

<RowDefinition />

<RowDefinition Height="200"/>

</Grid.RowDefinitions>

<!-- 标题 -->

<TextBlock Style="{StaticResource Header1}"

Grid.Column="0" Grid.Row="0">None</TextBlock>

<TextBlock Style="{StaticResource Header1}"

Grid.Column="1" Grid.Row="0">Uniform</TextBlock>

<TextBlock Style="{StaticResource Header1}"

Grid.Column="2" Grid.Row="0">UniformToFill</TextBlock>

<TextBlock Style="{StaticResource Header1}"

Grid.Column="3" Grid.Row="0">Fill</TextBlock>

<Border Style="{StaticResource borderStyle}" Grid.Column="0" Grid.Row="1">

<!-- None:如果图像大于输出区域,图像将裁剪到输出区域大小 -->

<Image Source="imageSamples/gecko.jpg" Stretch="None" />

</Border>

<Border Style="{StaticResource borderStyle}" Grid.Column="1" Grid.Row="1" >

<!-- Uniform: 保持纵横比例填充输出区域 -->

<Image Source="imageSamples/gecko.jpg" Stretch="Uniform" />

</Border>

<Border Style="{StaticResource borderStyle}" Grid.Column="2" Grid.Row="1" BorderThickness="1" BorderBrush="Black">

<!-- UniformToFill: 充满输出区域,保持纵横比例,可能出现复制 -->

<Image Source="imageSamples/gecko.jpg" Stretch="UniformToFill" />

</Border>

<Border Style="{StaticResource borderStyle}" Grid.Column="3" Grid.Row="1" >

<!-- Fill: 充满输出区域,不保持纵横比 -->

<Image Source="imageSamples/gecko.jpg" Stretch="Fill" />

</Border>

</Grid>

</DockPanel>



转载出处:http://danyaody.blog.163.com/blog/static/681203512012319114837552/


WPF- 图像的旋转、转换、裁剪和拉伸 (后台)

TransformedBitmap tb = new TransformedBitmap();

tb.BeginInit();

BitmapSource bs=(BitmapSource)brcapture.Source;

tb.Source = bs;

RotateTransform transform = new RotateTransform(90);

tb.Transform = transform;

tb.EndInit();

brcapture.Source = tb;//brcapture是Image控件
转载出处:http://www.silverlightchina.net/html/study/WPF/2012/0329/14939.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐