zoukankan      html  css  js  c++  java
  • Silverlight开发历程—(ImageBrush图像画刷和WriteableBitmap绘制位图)

     利用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设置了一个数组,分别执行蓝色,红色,绿肥色的计算。

  • 相关阅读:
    04-Bootstrap的插件
    03-Bootstrap学习
    02-移动端单位介绍
    01 响应式页面-@media介绍,
    14-jQuery补充
    13-jQuery的ajax
    12-事件委托(事件代理)
    11-jQuery的事件绑定和解绑
    10-事件对象
    09-JS的事件流的概念(重点)
  • 原文地址:https://www.cnblogs.com/raphael5200/p/5114886.html
Copyright © 2011-2022 走看看