Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Windows;
- using Microsoft.Phone.Controls;
- using System.Windows.Media.Imaging;
- using BitMiracle.LibTiff.Classic;
- using System.IO;
- using System.IO.IsolatedStorage;
- using System.Windows.Resources;
- namespace PhoneApp11
- {
- public partial class MainPage : PhoneApplicationPage
- {
- List<BitmapSource> _collection = new List<BitmapSource>(); // obrazki
- int _actualIndexInCollection = 0;
- // Constructor
- public MainPage()
- {
- InitializeComponent();
- AddImageToIsolatedStorage("tyff.tif");
- // Załaduj przykładowy plik Tiff
- WriteableBitmap[] BitmapsFromTiff = null;
- BitmapsFromTiff = GenerateImagesFromTiff("tyff.tif", BitmapsFromTiff);
- foreach (var item in BitmapsFromTiff)
- {
- _collection.Add(item);
- }
- // Dodaj png
- //_collection[_actualIndexInCollection++] = GetBitmapSourceFromImageUri("Background.png", UriKind.Relative);
- _collection.Add(GetBitmapSourceFromImageUri("Background.png", UriKind.Relative));
- // Dodaj splashscreenimage
- //_collection[_actualIndexInCollection++] = GetBitmapSourceFromImageUri("SplashScreenImage.jpg", UriKind.Relative);
- _collection.Add(GetBitmapSourceFromImageUri("SplashScreenImage.jpg", UriKind.Relative));
- // Ustaw indeks na 0 i wyświetl pierwszy obrazek
- _actualIndexInCollection = 0;
- image1.Source = _collection[0];
- }
- private void AddImageToIsolatedStorage(string filename)
- {
- // Create virtual store and file stream. Check for duplicate tempJPEG files.
- using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
- {
- if (!storage.FileExists(filename))
- {
- StreamResourceInfo resource = Application.GetResourceStream(new Uri(filename, UriKind.Relative));
- using (IsolatedStorageFileStream file = storage.CreateFile(filename))
- {
- int chunkSize = 4096;
- byte[] bytes = new byte[chunkSize];
- int byteCount;
- while ((byteCount =
- resource.Stream.Read(
- bytes, 0, chunkSize)) > 0)
- {
- file.Write(bytes, 0, byteCount);
- }
- }
- }
- }
- }
- /// <summary>
- /// Zwraca tablicę WriteableBitmap, które zawierają wszystkie klatki (strony) obrazku tiff
- /// </summary>
- /// <param name="fileNameInIsolatedStorage"></param>
- /// <param name="BitmapsFromTiff"></param>
- /// <returns></returns>
- private WriteableBitmap[] GenerateImagesFromTiff(string fileNameInIsolatedStorage, WriteableBitmap[] BitmapsFromTiff)
- {
- BitmapImage bmi = new BitmapImage();
- byte[] buffer = LoadIfExists(fileNameInIsolatedStorage);
- MemoryStream ms = new MemoryStream(buffer);
- using (Tiff tiffWorker = Tiff.ClientOpen("myTiffClass", "r", ms, new TiffStream()))
- {
- short imageDirectories = tiffWorker.NumberOfDirectories();
- if (imageDirectories > 0)
- {
- BitmapsFromTiff = new WriteableBitmap[imageDirectories];
- for (int i = 0; i < imageDirectories; i++)
- {
- if (tiffWorker.SetDirectory((short)i))
- {
- var frameWidthField = tiffWorker.GetField(TiffTag.IMAGEWIDTH);
- var frameHeightField = tiffWorker.GetField(TiffTag.IMAGELENGTH);
- int frameWidth = frameWidthField != null && frameWidthField.Length > 0 ? frameWidthField[0].ToInt() : 0;
- int frameHeight = frameHeightField != null && frameHeightField.Length > 0 ? frameHeightField[0].ToInt() : 0;
- WriteableBitmap bmp = null;
- try
- {
- var buffer1 = new int[frameWidth * frameHeight];
- tiffWorker.ReadRGBAImage(frameWidth, frameHeight, buffer1);
- bmp = new WriteableBitmap(frameWidth, frameHeight);
- for (int y = 0; y < frameHeight; y++)
- {
- var ytif = y * frameWidth;
- var ybmp = (frameHeight - y - 1) * frameWidth;
- for (int x = 0; x < frameWidth; x++)
- {
- var currentValue = buffer1[ytif + x];
- bmp.Pixels[ybmp + x] = Tiff.GetB(currentValue) | Tiff.GetG(currentValue) << 8 | Tiff.GetR(currentValue) << 16 | Tiff.GetA(currentValue) << 24;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("Could not load iamges from Tiff");
- }
- BitmapsFromTiff[i] = bmp;
- }
- }
- }
- }
- return BitmapsFromTiff;
- }
- /// <summary>
- /// Zwraca bitmapSource z Contentu projektu na podstawie podanego Uri
- /// </summary>
- /// <param name="uriName"></param>
- /// <param name="uriKind"></param>
- /// <returns></returns>
- private BitmapSource GetBitmapSourceFromImageUri(string uriName, UriKind uriKind)
- {
- Uri uri = new Uri(uriName, uriKind);
- return new BitmapImage(uri);
- }
- private void button1_Click(object sender, RoutedEventArgs e)
- {
- _actualIndexInCollection--;
- if (_actualIndexInCollection < 0)
- {
- _actualIndexInCollection = _collection.Count - 1;
- }
- image1.Source = _collection[_actualIndexInCollection];
- }
- private void button2_Click(object sender, RoutedEventArgs e)
- {
- _actualIndexInCollection++;
- if (_actualIndexInCollection > _collection.Count - 1)
- {
- _actualIndexInCollection = 0;
- }
- image1.Source = _collection[_actualIndexInCollection];
- }
- private byte[] LoadIfExists(string filename)
- {
- byte[] retVal;
- using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
- {
- if (iso.FileExists(filename))
- {
- using (IsolatedStorageFileStream stream = iso.OpenFile(filename, FileMode.Open))
- {
- retVal = new byte[stream.Length];
- stream.Read(retVal, 0, retVal.Length);
- }
- }
- else
- {
- retVal = new byte[0];
- }
- }
- return retVal;
- }
- }
- }
Add Comment
Please, Sign In to add comment