Image
灰度图像
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Page.Resources> <BitmapImage x:Key="masterImage" DecodePixelWidth="200" UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Forest.jpg"/> <FormatConvertedBitmap x:Key="convertFormatImage" Source="{StaticResource masterImage}" DestinationFormat="Gray32Float" /> Page.Resources> <StackPanel> <Image Width="200" Source="{StaticResource convertFormatImage}" /> StackPanel> Page>
裁剪图像
<Page.Resources> <BitmapImage x:Key="masterImage" UriSource="/sampleImages/gecko.jpg" /> <CroppedBitmap x:Key="croppedImage" Source="{StaticResource masterImage}" SourceRect="30 20 105 50"/> Page.Resources> <Image Width="200" Source="{StaticResource croppedImage}" Margin="5" Grid.Column="0" Grid.Row="1" />
CroppedBitmap还可以用作另一个源CroppedBitmap,链接裁剪。 请注意,SourceRect使用相对于裁剪位图并不是初始的图像的源的值。
<Image Width="200" Grid.Column="0" Grid.Row="3" Margin="5"> <Image.Source> <CroppedBitmap Source="{StaticResource croppedImage}" SourceRect="30 0 75 50"/> Image.Source> Image>
旋转图像
<Image Width="150" Margin="5" Grid.Column="0" Grid.Row="1"> <Image.Source> <BitmapImage UriSource="/sampleImages/watermelon.jpg" Rotation="Rotate90" /> Image.Source> Image>
变换
<BitmapImage x:Key="masterImage" DecodePixelWidth="200"
UriSource="C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Forest.jpg"/>
<TransformedBitmap x:Key="rotatedImage" Source="{StaticResource masterImage}">
<TransformedBitmap.Transform>
<RotateTransform Angle="90" />
TransformedBitmap.Transform>
TransformedBitmap>
掩码
圆角图片
<Grid> <Border Name="myBorder" CornerRadius="40" Background="White"/> <Image Source="D:\Pictures\Animals\041.jpg"> <Image.OpacityMask> <VisualBrush Visual="{Binding ElementName=myBorder}"/> Image.OpacityMask> Image> Grid>
注意Border的Background需要设置,因为OpacityMask属性仅使用 alpha 通道值为提供的 Brush
或者使用clip
<Image Source="D:\Pictures\Animals\041.jpg" Width="200" Height="150"> <Image.Clip> <EllipseGeometry RadiusX="100" RadiusY="75" Center="100,75"/> Image.Clip> Image>
BitmapFrame用于存储图像格式的实际位图数据。 演示如何BitmapFrame从 创建BitmapSource,然后添加到 TIFF 映像。
BitmapSource image5 = BitmapSource.Create( width, height, 96, 96, PixelFormats.Indexed1, BitmapPalettes.WebPalette, pixels, stride); FileStream stream5 = new FileStream("palette.tif", FileMode.Create); TiffBitmapEncoder encoder5 = new TiffBitmapEncoder(); encoder5.Frames.Add(BitmapFrame.Create(image5)); encoder5.Save(stream5);
BitmapImage是专为加载BitmapSource而可扩展应用程序标记语言 (XAML)优化Source的专用函数,是作为Image控件显示图像的简便方法。
// Create Image Element Image myImage = new Image(); myImage.Width = 200; // Create source BitmapImage myBitmapImage = new BitmapImage(); // BitmapImage.UriSource must be in a BeginInit/EndInit block myBitmapImage.BeginInit(); myBitmapImage.UriSource = new Uri(@"C:\Documents and Settings\All Users\Documents\My Pictures\Sample Pictures\Water Lilies.jpg"); // To save significant application memory, set the DecodePixelWidth or // DecodePixelHeight of the BitmapImage value of the image source to the desired // height or width of the rendered image. If you don't do this, the application will // cache the image as though it were rendered as its normal size rather then just // the size that is displayed. // Note: In order to preserve aspect ratio, set DecodePixelWidth // or DecodePixelHeight but not both. myBitmapImage.DecodePixelWidth = 200; myBitmapImage.EndInit(); //set image source myImage.Source = myBitmapImage;