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();
- // Załaduj przykładowy plik Tiff
- WriteableBitmap[] BitmapsFromTiff = null;
- BitmapsFromTiff = GenerateImagesFromTiff("myTiff.tiff", 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 = BitmapsFromTiff[0];
- }
- /// <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))
- {
- int tileCount = tiffWorker.NumberOfTiles();
- int stripCount = tiffWorker.NumberOfStrips();
- var frameWidthField = tiffWorker.GetField(TiffTag.IMAGEWIDTH);
- var frameHeightField = tiffWorker.GetField(TiffTag.IMAGELENGTH);
- var compressionField = tiffWorker.GetField(TiffTag.COMPRESSION);
- var xResolutionField = tiffWorker.GetField(TiffTag.XRESOLUTION);
- var yResolutionField = tiffWorker.GetField(TiffTag.YRESOLUTION);
- var samplesPerPixelField = tiffWorker.GetField(TiffTag.SAMPLESPERPIXEL);
- int frameWidth = frameWidthField != null && frameWidthField.Length > 0 ? frameWidthField[0].ToInt() : 0;
- int frameHeight = frameHeightField != null && frameHeightField.Length > 0 ? frameHeightField[0].ToInt() : 0;
- var compression = compressionField != null && compressionField.Length > 0 ? (Compression)compressionField[0].Value : Compression.NONE;
- var xResolution = xResolutionField != null && xResolutionField.Length > 0 ? new double?(xResolutionField[0].ToDouble()) : null;
- var yResolution = yResolutionField != null && yResolutionField.Length > 0 ? new double?(yResolutionField[0].ToDouble()) : null;
- var samplesPerPixel = samplesPerPixelField != null && samplesPerPixelField.Length > 0 ? samplesPerPixelField[0].ToString() : String.Empty;
- if (xResolution != null && yResolution == null)
- {
- yResolution = xResolution;
- }
- 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