当前位置:   article > 正文

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

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

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

图像画刷

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

XAML:

  1. <StackPanel x:Name="LayoutRoot" Background="White" Orientation="Vertical">
  2. <Rectangle RadiusX="30" RadiusY="30" Width="400" Height="250">
  3. <Rectangle.Fill>
  4. <ImageBrush ImageSource="../images/Silverlight.jpg" />
  5. </Rectangle.Fill>
  6. </Rectangle>
  7. <Rectangle RadiusX="30" x:Name="rect_2" RadiusY="30" Width="400" Height="250">
  8. </Rectangle>
  9. </StackPanel>


C#:

  1. public partial class FillWithImageBrush : UserControl
  2. {
  3. public FillWithImageBrush()
  4. {
  5. InitializeComponent();
  6. BitmapImage images = new BitmapImage(new Uri("../images/Silverlight.jpg", UriKind.Relative));
  7. ImageBrush brush = new ImageBrush();
  8. brush.ImageSource = images;
  9. rect_2.Fill = brush;
  10. }
  11. }


运行结果:

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

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

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

代码:

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


C#:

  1. public partial class DownProcessBar : UserControl
  2. {
  3. public DownProcessBar()
  4. {
  5. InitializeComponent();
  6. progressBar.Value = 0;
  7. Uri uri = new Uri("../images/silverlight2.jpg", UriKind.RelativeOrAbsolute);
  8. // 创建位图图像
  9. BitmapImage bmap_img = new BitmapImage();
  10. //定义图片下载事件
  11. bmap_img.DownloadProgress += new EventHandler<DownloadProgressEventArgs>(bmap_img_DownloadProgress);
  12. //指定图片的资源
  13. bmap_img.UriSource = uri;
  14. img_bg.Source = bmap_img;
  15. }
  16. void bmap_img_DownloadProgress(object sender, DownloadProgressEventArgs e)
  17. {
  18. //将进度呈现到进度控件上
  19. progressBar.Value = (double)e.Progress;
  20. }
  21. }


使用WriteableBitmap绘制位图

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

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

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


C#:

  1. public partial class DrawBitmapImage : UserControl
  2. {
  3. private Image img = new Image();
  4. private const int IMGWIDTH = 200;
  5. private const int IMGHEIGHT = 200;
  6. public DrawBitmapImage()
  7. {
  8. InitializeComponent();
  9. img.Width = IMGWIDTH;
  10. img.Height = IMGHEIGHT;
  11. LayoutRoot.Children.Add(img);
  12. BuildBitmap();
  13. }
  14. private void BuildBitmap()
  15. {
  16. //创建可写位图对象
  17. WriteableBitmap b = new WriteableBitmap(IMGWIDTH, IMGHEIGHT);
  18. for (int i = 0; i < IMGWIDTH; i++)
  19. {
  20. for (int j = 0; j < IMGHEIGHT; j++)
  21. {
  22. byte[] rgb = new byte[4];
  23. rgb[0] = (byte)(i % 255);
  24. rgb[1] = (byte)(j % 255);
  25. rgb[2] = (byte)(i * j % 255);
  26. rgb[3] = 0;
  27. int pixelValue = BitConverter.ToInt32(rgb, 0);
  28. b.Pixels[j * IMGWIDTH + i] = pixelValue;
  29. }
  30. }
  31. img.Source = b;
  32. }
  33. }


运行结果:

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号