C#进阶版 BitmapImage类的认识及运用

BitmapIamge类:位图图像

  • BitmapIamge:是一个非静态类,在运用的时候需要new对象进行实例化:

  • BitmapIamge实现ISupportInitialiit接口,优化多个属性的初始化。只能在对象初始化过程中进行属性更改。
    常用的构造函数

  • BitmapIamge()初始化BitmapIamge类的新实例。

  • BitmapIamge(Uri)使用提供的BitmapIamge初始化Uri类的新实例。
    常用属性

  • CacheOption ;获取或设置要用于此BitmapCacheOption(相当于枚举)实例的BitmapIamgebi.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;

        }
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值