首页 技术 正文
技术 2022年11月19日
0 收藏 638 点赞 3,508 浏览 2566 个字

在做UWP的时候,有一个需求,就是点击下载按钮,需要将当前页面中的Image控件中显示的图片保存下来。

既然聊到了下载图片,索性把添加图片也讲一下。

一:给Image控件添加图片

xaml代码:

<Image Source="Assets/icon/logo.png"
    Name="Logo"/>

在xaml里写样式特别简单,只要在Image的Source属性里填上图片的地址就好了。

c#代码:

Logo.Source = new BitmapImage(new Uri("Assets/icon/logo.png"));

通过IDE查看定义可以看到,Image控件的Source属性的类型是ImageSource类型,但是再查看ImageSource的定义发现是空的。

public ImageSource Source { get; set; }

而查看BitmapImage类的定义发现是继承于ImageSource类。 ImageSource是父类而BitmapImage是子类。

说明我们在用c#代码写的时候,不能直接通过父类去定义。例如:

Logo.Source = new ImageSource();  //这种方式是错误的,会报错

而应该用子类去写

Logo.Source = new BitmapImage(new Uri("Assets/icon/logo.png"));

二:BitmapImage 位图类

BitmapImage类 有一个 类型是Uri 类的参数的构造函数

public BitmapImage(Uri uriSource);

通过这个构造函数将图片的地址赋值上去。

三:Uri类

Uri: Uniform Resource identifier 统一资源标识符

简单来理解就是某个东西的标识,是一串字符串,用来标识的。

四:BackgroundDownloader 后台下载器类

如果你要保存的图片很大或者文件很大的时候,建议使用BackgroundDownloader后台下载类。

//定义一个后台下载器
BackgroundDownloader backgroundDownload = new BackgroundDownloader();
//创建一个下载任务对象
DownloadOperation download = backgroundDownload.CreateDownload(uri, newFile);
//开始下载
await download.StartAsync();

其中用调用下载器的CreateDownload()方法,创建一个下载任务

public DownloadOperation CreateDownload(Uri uri, IStorageFile resultFile);

查看定义可以看到有两个参数

第一个参数是需要下载文件的uri地址,第二个参数是IStorageFile 类型的本地文件对象

五:IStorageFile 接口

看到前面带了个I就知道是一个存储文件的接口。

interface 接口

六:StorageFile 存储文件类

该类继承了IStorageFile 接口。

七:StorageFolder 文件夹类

在系统中图片文件夹里创建一个文件夹

//在系统已知的文件夹中找到图片 这个文件夹  然后在里面创建一个名字为ONE的文件夹 
StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("ONE", CreationCollisionOption.OpenIfExists);

第二个参数是指当你在创建文件夹时遇到已有文件夹的时候,该怎么做。

然后可以用StorageFolder里的CreateFileAsync() 方法创建一个StorageFile对象

StorageFile newFile = await folder.CreateFileAsync(imageName, CreationCollisionOption.OpenIfExists);

CreateFileAsync()定义

public IAsyncOperation<StorageFile> CreateFileAsync(string desiredName, CreationCollisionOption options);   //名字  创建时发生文件碰撞怎么办

八:总结一下思路

1.先用StorageFolder创建一个文件夹

2.再用CreateFileAsync() 创建一个文件

3.创建一个后台下载器

4.创建一个后台任务,参数是uri和StorageFile

public async Task SaveImage(string imageName,string imageUri)
{
  BackgroundDownloader backgroundDownload = new BackgroundDownloader();  StorageFolder folder = await KnownFolders.PicturesLibrary.CreateFolderAsync("ONE", CreationCollisionOption.OpenIfExists);
StorageFile newFile = await folder.CreateFileAsync(imageName, CreationCollisionOption.OpenIfExists); Uri uri = new Uri(imageUri);
DownloadOperation download= backgroundDownload.CreateDownload(uri, newFile); await download.StartAsync();}

将iamge控件上的地址和名字传过来就ok了。

——–some words———

1.Source 来源

2.Bit 点

3.map 地图

4.bitmap 位图

5.Uri:Uniform Resource identifier  统一资源标识符

6.Storage 存储

7.Folder 文件夹

8.Interface 接口

9.Collision 碰撞

10. CreationCollisionOption 创建时发生碰撞的操作

11.

——– -the  end————

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898