Silverlight开发历程—(ImageBrush图像画刷和WriteableBitmap绘制位图)

本文介绍了如何在WPF中使用Image元素展示图像,并详细解释了Stretch属性的作用及不同取值的效果。此外,还提供了使用ImageBrush进行背景绘制、BitmapImage实现图像下载缓冲以及WriteableBitmap绘制位图的具体实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

利用Image呈现图像,有一点比较重要,就是Image元素的枚举类型属性Stretch,Stretch主要是来确定Image的填充方式,Stretch枚举类型值分别为:None(原始尺寸)、Fill(填充拉伸)、Uniform(等比例拉伸)、UniformToFill(等比拉伸填充)。

图像画刷

下面代码分别利用XAML代码和C#代码绘制图像画刷:

XAML:

<StackPanel x:Name="LayoutRoot" Background="White" Orientation="Vertical"> <Rectangle RadiusX="30" RadiusY="30" Width="400" Height="250"> <Rectangle.Fill> <ImageBrush ImageSource="../images/Silverlight.jpg" /> </Rectangle.Fill> </Rectangle> <Rectangle RadiusX="30" x:Name="rect_2" RadiusY="30" Width="400" Height="250"> </Rectangle> </StackPanel>


C#:

public partial class FillWithImageBrush : UserControl { public FillWithImageBrush() { InitializeComponent(); BitmapImage images = new BitmapImage(new Uri("../images/Silverlight.jpg", UriKind.Relative)); ImageBrush brush = new ImageBrush(); brush.ImageSource = images; rect_2.Fill = brush; } }


运行结果:

使用BitmapImage下载事件来实现图像下载缓冲效果

在XAML界面使用了一个进度条控件和一个Image对象,通过BitmapImage对象的DownloadProgress事件来获取图片下载进度并将结果呈现给进度条控件的Value值。

使用BitmapImage必须先引入命名空间System.Windows.Media.Imaging命名空间。

代码:

<Grid x:Name="LayoutRoot" Background="White"> <Image x:Name="img_bg" /> <ProgressBar x:Name="progressBar" Width="270" Height="40" /> </Grid>


C#:

public partial class DownProcessBar : UserControl { public DownProcessBar() { InitializeComponent(); progressBar.Value = 0; Uri uri = new Uri("../images/silverlight2.jpg", UriKind.RelativeOrAbsolute); // 创建位图图像 BitmapImage bmap_img = new BitmapImage(); //定义图片下载事件 bmap_img.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bmap_img_DownloadProgress); //指定图片的资源 bmap_img.UriSource = uri; img_bg.Source = bmap_img; } void bmap_img_DownloadProgress(object sender, DownloadProgressEventArgs e) { //将进度呈现到进度控件上 progressBar.Value = (double)e.Progress; } }


使用WriteableBitmap绘制位图

WriteableBitmap位于System.Windows.Media.Imaging命名空间并派生了BitmapSource类,所以它属于图像的范畴。

WriteableBitmap和BitmapImage类似都可以为Image对象提供Source属性值,如下例子:

<Grid x:Name="LayoutRoot" Background="Gray"> </Grid>


C#:

public partial class DrawBitmapImage : UserControl { private Image img = new Image(); private const int IMGWIDTH = 200; private const int IMGHEIGHT = 200; public DrawBitmapImage() { InitializeComponent(); img.Width = IMGWIDTH; img.Height = IMGHEIGHT; LayoutRoot.Children.Add(img); BuildBitmap(); } private void BuildBitmap() { //创建可写位图对象 WriteableBitmap b = new WriteableBitmap(IMGWIDTH, IMGHEIGHT); for (int i = 0; i < IMGWIDTH; i++) { for (int j = 0; j < IMGHEIGHT; j++) { byte[] rgb = new byte[4]; rgb[0] = (byte)(i % 255); rgb[1] = (byte)(j % 255); rgb[2] = (byte)(i * j % 255); rgb[3] = 0; int pixelValue = BitConverter.ToInt32(rgb, 0); b.Pixels[j * IMGWIDTH + i] = pixelValue; } } img.Source = b; } }


运行结果:

后台通过引出两个For循环语句通过程序来设置Pbgra32颜色,通过RGB设置了一个数组,分别执行蓝色,红色,绿肥色的计算。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值