A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

     刚接触WPF,很多地方都不懂,就把自己遇到的一些问题记录下来,以便日后回过头来看看。
       开始使用Image控件,不知道怎么去显示图片,在同事指导下知道了BitmapImage 类,用来显示在Image中显示图片,按F1进入帮助文档,找到相关的定义:

       


  •         // Create the image element.



  •         Image simpleImage = new Image();   



  •         simpleImage.Width = 200;



  •         simpleImage.Margin = new Thickness(5);







  •         // Create source.



  •         BitmapImage bi = new BitmapImage();



  •         // BitmapImage.UriSource must be in a BeginInit/EndInit block.



  •         bi.BeginInit();



  •         bi.UriSource = new Uri(@"/sampleImages/cherries_larger.jpg",UriKind.RelativeOrAbsolute);



  •         bi.EndInit();



  •         // Set the image source.



  •         simpleImage.Source = bi;



  •     建立一个WPF项目,在工具箱中拖出一个Imag




e控件        选择一个合适的图片,按照主窗口Load事件中写自己的代码   


  •         private void Window_Loaded(object sender, RoutedEventArgs e)



  •         {



  •                 BitmapImage bi = new BitmapImage();



  •                 // BitmapImage.UriSource must be in a BeginInit/EndInit block.



  •                 bi.BeginInit();



  •                 bi.UriSource = new Uri(@"F:\1.jpg",UriKind.RelativeOrAbsolute);



  •                 bi.EndInit();



  •                 image1.Source = bi;



  •         }




    图片如下:            看到图片整个人都不好了。    后面看到了一篇文章,觉得说的挺好的。http://www.cnblogs.com/zydf/p/3141735.html    的确处理单张图片挺好用的,担当处理多张的时候,就要考虑内存的问题了。先把图片缓存成二进制,这样可以释放对图片文件资源的占用,后面代码执行效率高;    就自己做出了下面的修改:      


  •         private string path = @"F:\1.jpg";



  •         private void Window_Loaded(object sender, RoutedEventArgs e)



  •         {



  •             using (BinaryReader loader = new BinaryReader(File.Open(path, FileMode.Open)))



  •             {



  •                 FileInfo fd = new FileInfo(path);



  •                 int Length = (int)fd.Length;



  •                 byte[] buf = new byte[Length];



  •                 buf = loader.ReadBytes((int)fd.Length);



  •                 loader.Dispose();



  •                 loader.Close();











  •                 //开始加载图像



  •                 BitmapImage bim = new BitmapImage();



  •                 bim.BeginInit();



  •                 bim.StreamSource = new MemoryStream(buf);



  •                 bim.EndInit();



  •                 image1.Source = bim;



  •                 GC.Collect(); //强制回收资源



  •             }











  •             //BitmapImage bi = new BitmapImage();



  •             //// BitmapImage.UriSource must be in a BeginInit/EndInit block.



  •             //bi.BeginInit();



  •             //bi.UriSource = new Uri(@"F:\1.jpg",UriKind.RelativeOrAbsolute);



  •             //bi.EndInit();



  •             //image1.Source = bi;



  •         }




        图片效果:                它又出来了!        当然,后面就可以做更多的事情啦。---------------------本文来自 chuangand 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/chuangand/ ... 271?utm_source=copy

1 个回复

倒序浏览
奈斯
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马