BitmapIamge类:位图图像
-
BitmapIamge:是一个非静态类,在运用的时候需要new对象进行实例化:
-
BitmapIamge实现ISupportInitialiit接口,优化多个属性的初始化。只能在对象初始化过程中进行属性更改。
常用的构造函数 -
BitmapIamge()初始化BitmapIamge类的新实例。
-
BitmapIamge(Uri)使用提供的BitmapIamge初始化Uri类的新实例。
常用属性 -
CacheOption ;获取或设置要用于此BitmapCacheOption(相当于枚举)实例的BitmapIamge
bi.CacheOption = BitmapCacheOption.OnDemand;
-
CreateOptions 中字段字段DelayCreation;使 BitmapSource对象的初始化延迟到必要时才执行。在处理图像集合时,这十分有用。
bi.CreateOptions = BitmapCreateOptions.DelayCreation;
-
DecodePixelHeight/Width:获取和设置图像后的高度/宽度
-
lsDownloading:获取一个值,该值指示BitmapIamge是否正在下载内容。
bi.DownloadCompleted += Bi_DownloadCompleted;
常用方法 -
Beginlnit()初始化开始
-
EndInit() 初始化结束
-
在使用 BitmapImage时得用这两个方法才能有效果。
实例:实现图片的放缩旋转
后台代码
/// <summary>
/// 图片的放大
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click(object sender, RoutedEventArgs e) {
BitmapImage bi = new BitmapImage();
bi.BeginInit();//初始化
//bi.CacheOption = BitmapCacheOption.OnLoad;//加载图片
//bi.CreateOptions = BitmapCreateOptions.DelayCreation;//延迟后使用的
//bi.DecodePixelWidth = 1125;
//bi.DecodePixelHeight = 1125;
// bi.Rotation = Rotation.Rotate90;
// MessageBox.Show(bi.IsDownloading.ToString());
bi.UriSource =new Uri("https://th.bing.com/th/id/R68e616ee957d0ca960a003867b4a952d?rik=we8uCxfI%2fkvZgg&riu=http%3a%2f%2fpic24.nipic.com%2f20121020%2f11117676_182836270000_2.jpg");
bi.EndInit();
bi.DownloadCompleted += Bi_DownloadCompleted;
myImage.Source = bi;
//myImage.Stretch = Stretch.Fill;
myImage.Stretch = Stretch.None;
myImage.Margin = new Thickness(5);
}
private void Bi_DownloadCompleted(object sender, EventArgs e) {
MessageBox.Show("下载完成");
}
/// <summary>
/// 图片缩小
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_1(object sender, RoutedEventArgs e) {
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnDemand;
bi.CreateOptions = BitmapCreateOptions.DelayCreation;
bi.DecodePixelWidth = 125;
bi.DecodePixelHeight = 125;
bi.UriSource = new Uri("/img/01.jpg", UriKind.Relative);
bi.EndInit();
myImage.Source = bi;
myImage.Stretch = Stretch.None;//如何拉伸图片
myImage.Margin = new Thickness(10);
}
/// <summary>
/// 旋转
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Button_Click_2(object sender, RoutedEventArgs e) {
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.CacheOption = BitmapCacheOption.OnLoad;
bi.CreateOptions = BitmapCreateOptions.DelayCreation;
bi.Rotation = Rotation.Rotate90;
bi.UriSource = new Uri("/img/01.jpg", UriKind.Relative);//获取Uri
bi.EndInit();
myImage.Source = bi;
}