分享一个网络图片控件(WebImage for wp8)
2013-07-07 01:34
323 查看
闲话不多说,直接上代码!!!转载请附上原作者地址哦!!!thx!!!
WebImage.xaml页面代码如下:
用法如下:xmlns:MyControl="clr-namespace:CloudWpDemo.Controls"
我测试过蛮多次。。。。此控件挺好用的!!!希望对你的开发有用!!!!
WebImage.xaml页面代码如下:
<UserControl x:Class="WpDemo.Controls.WebImage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" FontFamily="{StaticResource PhoneFontFamilyNormal}" FontSize="{StaticResource PhoneFontSizeNormal}" Foreground="{StaticResource PhoneForegroundBrush}" d:DesignHeight="480" d:DesignWidth="480"> <Grid x:Name="LayoutRoot"> <Image x:Name="ImgShow" Stretch="UniformToFill" Visibility="Collapsed"/> <Grid x:Name="gdWait"> <Rectangle x:Name="rectProgress" Fill="#FFFFB3E0" Stroke="Black" VerticalAlignment="Bottom"/> <TextBlock x:Name="tbProgress" HorizontalAlignment="Center" TextWrapping="Wrap" VerticalAlignment="Center" Foreground="White" Text="0"/> </Grid> </Grid> </UserControl>WebImage.xaml.cs类如下:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; using System.Windows.Media; using System.Windows.Media.Imaging; using System.ComponentModel; using System.IO; namespace WpDemo.Controls { public partial class WebImage : UserControl { private static String CacheFolder = "cloud_cache_image"; private BitmapImage _imgSource = null; public static readonly DependencyProperty SourceProperty = DependencyProperty.Register("Source", typeof(ImageSource), typeof(WebImage), null); public static readonly DependencyProperty StretchProperty = DependencyProperty.Register("Stretch", typeof(Stretch), typeof(WebImage), new PropertyMetadata(Stretch.UniformToFill)); public static readonly DependencyProperty ImgUriProperty = DependencyProperty.Register("ImgUri", typeof(string), typeof(WebImage), new PropertyMetadata("", OnImgUriChanged)); private string _ImgUri; public ImageSource Source { get { return ImgShow.Source; } set { ImgShow.SetValue(Image.SourceProperty, value); } } public Stretch Stretch { get { return ImgShow.Stretch; } set { ImgShow.SetValue(Image.StretchProperty, value); } } public string ImgUri { get { return _ImgUri; } set { _ImgUri = value; SetImgSource(_ImgUri); } } void SetImgSource(string uri) { string cacheName = Path.Combine(CacheFolder, ParseUri(uri)); _imgSource = new BitmapImage(); _imgSource.ImageOpened += imgSource_ImageOpened; if (IsolatedStorageFileHelper.FileExists(cacheName)) { using (Stream stream = IsolatedStorageFileHelper.OpenFile(cacheName)) { _imgSource.SetSource(stream); ; } ImgShow.Visibility = System.Windows.Visibility.Visible; gdWait.Visibility = System.Windows.Visibility.Collapsed; } else { _imgSource.DownloadProgress += imgSource_DownloadProgress; _imgSource.ImageFailed += imgSource_ImageFailed; _imgSource.UriSource = new Uri(uri, UriKind.Absolute); } ImgShow.Source = _imgSource; } void imgSource_ImageFailed(object sender, ExceptionRoutedEventArgs e) { _imgSource.ImageOpened -= imgSource_ImageOpened; _imgSource.ImageFailed -= imgSource_ImageFailed; _imgSource.DownloadProgress -= imgSource_DownloadProgress; } void imgSource_ImageOpened(object sender, RoutedEventArgs e) { ImgShow.Visibility = System.Windows.Visibility.Visible; gdWait.Visibility = System.Windows.Visibility.Collapsed; BitmapImage img = (BitmapImage)sender; WriteableBitmap wb = new WriteableBitmap(img); using (Stream stream = IsolatedStorageFileHelper.CreateFile(Path.Combine(CacheFolder, ParseUri(img.UriSource.AbsoluteUri)))) { wb.SaveJpeg(stream, img.PixelWidth, img.PixelHeight, 0, 100); } _imgSource.ImageOpened -= imgSource_ImageOpened; _imgSource.ImageFailed -= imgSource_ImageFailed; _imgSource.DownloadProgress -= imgSource_DownloadProgress; } void imgSource_DownloadProgress(object sender, DownloadProgressEventArgs e) { rectProgress.Height = gdWait.ActualHeight * (e.Progress / 100.0); tbProgress.Text = e.Progress.ToString(); _imgSource.DownloadProgress -= imgSource_DownloadProgress; } public WebImage() { InitializeComponent(); } private static void OnImgUriChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e) { WebImage img = obj as WebImage; img.ImgUri = e.NewValue as string; } private string ParseUri(string uri) { return uri.Replace("://", "").Replace("/", "_"); } } }
using System; using System.Collections.Generic; using System.IO.IsolatedStorage; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; namespace WpDemo.Common { public class IsolatedStorageFileHelper { private static IsolatedStorageFile _Store = null; private static IsolatedStorageFile Store { get { if (_Store == null) { _Store = IsolatedStorageFile.GetUserStoreForApplication(); } return _Store; } } public static void CreateFolder(string dir) { if (Store.DirectoryExists(dir)) return; Store.CreateDirectory(dir); } public static void DeleteFolder(string dir) { if (Store.DirectoryExists(dir)) return; Store.DeleteDirectory(dir); } public static void DeleteFile(string path) { if (!Store.FileExists(path)) return; Store.DeleteFile(path); } public static IsolatedStorageFileStream CreateFile(string path) { string[] parts = path.Split('\\'); string dir = ""; for (int i = 0; i < parts.Length - 1; i++) { string part = parts[i]; dir = System.IO.Path.Combine(dir, part); if (!Store.DirectoryExists(dir)) Store.CreateDirectory(dir); } return Store.CreateFile(path); } public static bool FileExists(string path) { return Store.FileExists(path); } public static IsolatedStorageFileStream OpenFile(string path) { return Store.OpenFile(path, System.IO.FileMode.Open); } public static void SerializeObject(object obj, string path) { if (Store.FileExists(path)) Store.DeleteFile(path); XmlSerializer xml = new XmlSerializer(obj.GetType()); using (var stream = CreateFile(path)) { xml.Serialize(stream, obj); } } public static T DeSerializeObject<T>(string path) { if (!Store.FileExists(path)) return default(T); XmlSerializer xml = new XmlSerializer(typeof(T)); object obj = null; using (var stream = OpenFile(path)) { obj = xml.Deserialize(stream); } return (T)obj; } } }
用法如下:xmlns:MyControl="clr-namespace:CloudWpDemo.Controls"
<StackPanel Grid.Column="0" Width="200"> <MyControl:WebImage ImgUri="{Binding Img_url}" Margin="5,5,5,5"></MyControl:WebImage> <!--<Image Source="{Binding Img_url}"></Image> <TextBlock Text="{Binding Name}"></TextBlock>--> </StackPanel>
我测试过蛮多次。。。。此控件挺好用的!!!希望对你的开发有用!!!!
相关文章推荐
- 封装一个图片轮换的web控件
- [第三方]SDWebImage获取网络图片控件的用法
- Best Practices for Speeding Up Your Web Site(网络图片优化规则)
- 近期Responsive web design项目经验分享-高分辨率图片处理篇
- SDWebImage使用——一个可管理远程图片加载的类库
- 网络加载图片缓存与SDWebImage
- 分享一个在XP系统的无线网络问题解决方法
- 想成为一个高效的Web开发者吗?来看看大牛分享的经验吧~
- 封装一个Drop and Drag面板控件,供大家分享
- android一个小网络图片查看器
- MessageDisplayKit 一个类似微信App的IM应用,拥有发送文字、图片、语音、视频、地理位置消息,管理本地通信录、分享朋友圈、漂流交友、摇一摇和更多有趣的功能。
- 给大家分享一个图片双击放大,双击缩小的第三方库
- Android通过请求网络数据实现ListView,ListView的优化、图片的缓存、子控件的点击事件。
- Web图片上传控件(Chrome)升级日志-Xproer.ImageUploader
- Web图片上传控件(x64)升级日志-Xproer.ImageUploader64
- 分享一个精简的vue.js 图片lazyload插件实例
- 记录一个图片处理类,分享给大家参考
- C# 发送HttpWebRequest获得网络图片的大小和尺寸
- 分享一个jQuery的图片预加载插件 - QueryLoader2
- SDWebImage使用——一个可管理远程图片加载的类库