I. Introduction
– Trong thực tế với những ứng dụng cần lưu hình ảnh thì ngoài việc lưu vào máy thì lưu vào Isolated Storage ứng dụng là một giải pháp rất cần thiết cho các ứng dụng hình ảnh.
– Trong bài này mình sẽ chia sẻ cách đọc và lưu hình ảnh vào Isolated Storage trong Windows Phone
II. Fundamental
1. Lưu hình ảnh vào Isolated Storage
– Trong ví dụ này mình sẽ đọc hình ảnh từ phía Resource file, có nghĩa là đây là những hình ảnh do chính chúng ta thêm vào Project.
– Để bắt đầu các thao tác chúng ta khai báo sử dụng namespace các thư thiện cần thiết trong MainPage.xaml.cs :
1: using System.IO.IsolatedStorage;
2: using System.Windows.Media.Imaging;
3: using System.IO;
4: using System.Windows.Resources;
5: using Microsoft.Xna.Framework.Media;
6: using Microsoft.Phone.Tasks;
– Tiếp theo là thao tác Lưu hình ảnh, ổ đây giả sử minh làm 1 phương thức là SavePhoto() :
1: private void SavePhoto(){
2: // tên file hình
3: String tempJPEG = "logo.jpg";
4:
5: // Khởi tạo đối tượng kết tối tới Isolated Storage
6: using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
7: {
8: // Kiểm tra nếu file tồn tại thì xóa file
9: if (myIsolatedStorage.FileExists(tempJPEG))
10: {
11: myIsolatedStorage.DeleteFile(tempJPEG);
12: }
13: // Dùng Isolated Storage để tạo file tempJPEG
14: IsolatedStorageFileStream fileStream = myIsolatedStorage.CreateFile(tempJPEG);
15:
16: // Dùng StreamResourceInfo để đọc file từ Resource
17: StreamResourceInfo sri = null;
18: Uri uri = new Uri(tempJPEG, UriKind.Relative);
19: sri = Application.GetResourceStream(uri);
20:
21: // Load Resource vào hình ảnh Bitmap
22: BitmapImage bitmap = new BitmapImage();
23: bitmap.SetSource(sri.Stream);
24: // dùng WriableBitmap để ghi file dưới dạng nhị phân vào bộ nhớ
25: WriteableBitmap wb = new WriteableBitmap(bitmap);
26:
27: // Mã hóa và lưu dữ liệu ảnh vào file tempJPG
28: Extensions.SaveJpeg(wb, fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
29:
30: // Cách khác
31: //wb.SaveJpeg(fileStream, wb.PixelWidth, wb.PixelHeight, 0, 85);
32: fileStream.Close();
33: }
Với những thao tác đơn giản là ta đã lưu hình ảnh từ Resource vào Isolated Storage.
Trong trường hợp các bạn muốn chụp hình và lưu vào Isolated Storage thì rất đơn giản (đề nghị bạn đọc bài này trước http://phamnguyen.info/?p=2748) là chỉ cần chụp ảnh và lấy Stream hình ảnh vừa chụp đẻ khởi tạo đối tượng BitmapImage và lưu vào Isolated Storage giống như với file resource .
2. Đọc hình ảnh từ Isolated Storage
Khi đã lưu được hình ảnh vào Isolated Storage thì việc đọc rất đơn giản chúng ta chỉ cần biết tên file hình đã đã lưu :
1: BitmapImage bi = new BitmapImage();
2:
3: using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
4: {
5: using (IsolatedStorageFileStream fileStream = myIsolatedStorage.OpenFile("logo.jpg", FileMode.Open, FileAccess.Read))
6: {
7: bi.SetSource(fileStream);
8: this.img.Height = bi.PixelHeight;
9: this.img.Width = bi.PixelWidth;
10: }
11: }
12: this.img.Source = bi;
Hy vọng bài viết sẽ mang đến thông tin hữu ích cho các bạn