星空网 > 软件开发 > 操作系统

windowsphone8.1学习笔记之位图编程

说位图,先把image控件简单过下,Image的Source设置

<Image Name="img" Source="可以是网络图片的Uri、应用文件的Uri或者安装包文件的Uri" />img.Source = new BitmapIamge(new Uri(同上));

Image的Stretch属性指定图像如何填充,枚举定义:

None,图像不拉伸,一合适尺寸显示;

windowsphone8.1学习笔记之位图编程

Uniform,保留图像的纵横比,按控件的大小输出图像;

windowsphone8.1学习笔记之位图编程

UnifToFill,已原始大小比填充;

windowsphone8.1学习笔记之位图编程

Fill,将整个图完全填充到控件中

windowsphone8.1学习笔记之位图编程

 

Clip属性是UIElement的属性,所以所有的UI对象都有,Clip属性可用来剪切图像

    <Grid Background="Black"
          PointerMoved="Grid_PointerMoved">
        <Image Name="ContentPanel" Source="ms-appx:///Assets/Logo.scale-240.png">
            <Image.Clip>
                <RectangleGeometry x:Name="geometry" Rect="0,0,150,150" />
            </Image.Clip>
        </Image>
    </Grid>        private void Grid_PointerMoved(object sender, PointerRoutedEventArgs e)
        {
            Point p = e.GetCurrentPoint(ContentPanel).Position;
            geometry.Rect = new Rect(p.X, p.Y, 150, 150);
        }

效果图,就不切了,自己下来弄下,就知道了。我们截取了图像,如何生成图片呢?使用RenderTragetBitmap类

    //PointerReleased可以捕获单击事件    private async void root_PointerReleased(object sender, PointerRoutedEventArgs e)    {      //创建RenderTargetBitmap对象      RenderTargetBitmap bitmap = new RenderTargetBitmap();      //root为UIElement的Name      await bitmap.RenderAsync(root);      img.Source = bitmap;    }

生成图片之后,就是存储图片了,存储图片就需要获得图像的二进制数据,然后通过BitmapEncoder类实现对图像的创建、编辑和保存的各种方法。示例如下:

  <Grid Name="root">    <Grid.RowDefinitions>      <RowDefinition Height="Auto" />      <RowDefinition Height="*" />      <RowDefinition Height="Auto" />    </Grid.RowDefinitions>    <StackPanel Name="TitlePanel" Margin="10,35,10,10">      <TextBlock Text="我的应用程序" FontSize="20" />      <TextBlock Text="测试" FontSize="60" />    </StackPanel>    <Grid Name="ContentPanel" Grid.Row="1" Margin="10,0,10,10">      <Image Name="img" />    </Grid>    <Grid Grid.Row="2">      <Grid.ColumnDefinitions>        <ColumnDefinition />        <ColumnDefinition />      </Grid.ColumnDefinitions>      <Button VerticalAlignment="Center" HorizontalAlignment="Center" Content="获取图像" Click="Button_Click_1" />      <Button VerticalAlignment="Center" HorizontalAlignment="Center" Content="显示图像" Click="Button_Click" Grid.Column="1" />    </Grid>  </Grid>

    private void Button_Click(object sender, RoutedEventArgs e)    {      img.Source = new BitmapImage(new Uri("ms-appdata:///local/testBitmap.png",UriKind.Absolute));    }    private async void Button_Click_1(object sender, RoutedEventArgs e)    {      //创建要存储的文件,这个不需多说了。      var storageFile = await ApplicationData.Current.LocalFolder.CreateFileAsync("testBitmap.png", CreationCollisionOption.OpenIfExists);      RenderTargetBitmap bitmap = new RenderTargetBitmap();      await bitmap.RenderAsync(root);      //获取图像的二进制数据      var pixelBuffer = await bitmap.GetPixelsAsync();      using (var fileStream = await storageFile.OpenAsync(FileAccessMode.ReadWrite))      {        var encoder = await BitmapEncoder.CreateAsync(BitmapEncoder.PngEncoderId, fileStream);        encoder.SetPixelData(          BitmapPixelFormat.Bgra8,//像素格式          BitmapAlphaMode.Ignore,//像素数据的alpha模式,定义透明的          (uint)bitmap.PixelWidth,//像素宽          (uint)bitmap.PixelHeight,//像素高          DisplayInformation.GetForCurrentView().LogicalDpi,//像素水平分辨率,这个就是获取设备的屏幕分辨率的方法          DisplayInformation.GetForCurrentView().LogicalDpi,//像素垂直分辨率          pixelBuffer.ToArray());//像素数据        await encoder.FlushAsync();      }      await new MessageDialog("获取图像成功并已保存").ShowAsync();    }

好了,位图编程就到这儿。




原标题:windowsphone8.1学习笔记之位图编程

关键词:Windows

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流