http://blog.csdn.net/leons77/article/details/8085662web
讀取圖片
圖片的來源通常有3個地方:網絡、獨立存儲和程序包。網絡
從網絡讀取圖片
- string uri = "http://avatar.csdn.net/2/C/D/3_leons77.jpg";
-
- BitmapImage bitmapImage = new BitmapImage();
- bitmapImage.UriSource = new Uri(uri, UriKind.Absolute);
-
- image.Source = bitmapImage;
從網絡保存圖片到獨立存儲
- string uri = "http://avatar.csdn.net/2/C/D/3_leons77.jpg";
-
- BitmapImage bitmapImage = new BitmapImage();
- bitmapImage.UriSource = new Uri(uri, UriKind.Absolute);
- bitmapImage.ImageOpened += new EventHandler<RoutedEventArgs>(bitmapImage_ImageOpened);
-
- image.Source = bitmapImage;
- void bitmapImage_ImageOpened(object sender, RoutedEventArgs e)
- {
-
- using (MemoryStream memoryStream = new MemoryStream())
- {
- WriteableBitmap writeableBitmap = new WriteableBitmap(sender as BitmapImage);
- Extensions.SaveJpeg(writeableBitmap, memoryStream, writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);
-
-
- string fileName = "image.jpg";
-
- using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
- {
-
- if (store.FileExists(fileName))
- {
- store.DeleteFile(fileName);
- }
-
- using (IsolatedStorageFileStream fileStream = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write))
- {
- using (BinaryWriter writer = new BinaryWriter(fileStream))
- {
- writer.Write(memoryStream.GetBuffer());
- }
- }
- }
- }
- }
注意:只有當Image控件的Source使用BitmapImage時,BitmapImage纔會去下載圖片,纔會執行它的ImageOpened委託。異步
從獨立存儲裏讀取圖片
經過上面的「從網絡保存圖片到獨立存儲」,獨立存儲裏面已經有一張image.jpg圖片文件了。post
- string fileName = "image.jpg";
-
- using (IsolatedStorageFileStream stream = IsolatedStorageFile.GetUserStoreForApplication().OpenFile(fileName, FileMode.Open, FileAccess.Read))
- {
- BitmapImage bitmapImage = new BitmapImage();
- bitmapImage.SetSource(stream);
-
-
- image.Source = bitmapImage;
- }
從程序包裏讀取圖片
兩種方法:編碼
第一種最簡單,用Image控件直接在XAML裏面設置它的Source屬性就行了。注意:圖片的生成操做必定要是Resource。spa
第二種:.net
- string fileName = "/SaveLoadImage;component/y.jpg";
-
- using (Stream stream = Application.GetResourceStream(new Uri(fileName, UriKind.Relative)).Stream)
- {
- BitmapImage bitmapImage = new BitmapImage();
- bitmapImage.SetSource(stream);
-
-
- image.Source = bitmapImage;
- }
保存圖片
上面已經介紹了用Image控件+BitmapImage保存圖片的方法。下面是介紹直接下載圖片到獨立存儲:component
- string uri = "http://avatar.csdn.net/2/C/D/3_leons77.jpg";
-
- WebClient webClient = new WebClient();
- webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
- webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute));
- void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
- {
- if (e.Error == null && !e.Cancelled)
- {
- using (BinaryReader reader = new BinaryReader(e.Result))
- {
- using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
- {
-
- string fileName = "image.jpg";
-
-
- if (store.FileExists(fileName))
- {
- store.DeleteFile(fileName);
- }
-
- using (IsolatedStorageFileStream fileStream = store.OpenFile(fileName, FileMode.OpenOrCreate, FileAccess.Write))
- {
- using (BinaryWriter writer = new BinaryWriter(fileStream))
- {
- writer.Write(reader.ReadBytes(Convert.ToInt32(e.Result.Length)));
- }
- }
- }
- }
- }
- }
注意:這裏用的是WebClient下載圖片,若是圖片過大的話,下載的時候UI就會被卡死。最好用HttpWebRequst異步下載。我以前的博客《WP7(Windows Phone)開發遇到的事:使用get、post請求》有寫怎麼使用HttpWebRequst對象