Guest User

Untitled

a guest
Aug 14th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.50 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Windows;
  4. using Microsoft.Phone.Controls;
  5. using System.Windows.Media.Imaging;
  6. using BitMiracle.LibTiff.Classic;
  7. using System.IO;
  8. using System.IO.IsolatedStorage;
  9. using System.Windows.Resources;
  10.  
  11. namespace PhoneApp11
  12. {
  13.     public partial class MainPage : PhoneApplicationPage
  14.     {
  15.         List<BitmapSource> _collection = new List<BitmapSource>();  // obrazki
  16.         int _actualIndexInCollection = 0;
  17.  
  18.         // Constructor
  19.         public MainPage()
  20.         {
  21.             InitializeComponent();
  22.  
  23.             AddImageToIsolatedStorage("tyff.tif");
  24.  
  25.  
  26.             // Załaduj przykładowy plik Tiff
  27.             WriteableBitmap[] BitmapsFromTiff = null;
  28.             BitmapsFromTiff = GenerateImagesFromTiff("tyff.tif", BitmapsFromTiff);
  29.  
  30.             foreach (var item in BitmapsFromTiff)
  31.             {
  32.                 _collection.Add(item);
  33.             }
  34.  
  35.             // Dodaj png
  36.             //_collection[_actualIndexInCollection++] = GetBitmapSourceFromImageUri("Background.png", UriKind.Relative);
  37.             _collection.Add(GetBitmapSourceFromImageUri("Background.png", UriKind.Relative));
  38.  
  39.  
  40.             // Dodaj splashscreenimage
  41.             //_collection[_actualIndexInCollection++] = GetBitmapSourceFromImageUri("SplashScreenImage.jpg", UriKind.Relative);
  42.             _collection.Add(GetBitmapSourceFromImageUri("SplashScreenImage.jpg", UriKind.Relative));
  43.  
  44.  
  45.             // Ustaw indeks na 0 i wyświetl pierwszy obrazek
  46.             _actualIndexInCollection = 0;
  47.             image1.Source = _collection[0];
  48.         }
  49.  
  50.         private void AddImageToIsolatedStorage(string filename)
  51.         {
  52.             // Create virtual store and file stream. Check for duplicate tempJPEG files.
  53.             using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
  54.             {
  55.                 if (!storage.FileExists(filename))
  56.                 {
  57.                     StreamResourceInfo resource = Application.GetResourceStream(new Uri(filename, UriKind.Relative));
  58.  
  59.                     using (IsolatedStorageFileStream file = storage.CreateFile(filename))
  60.                     {
  61.                         int chunkSize = 4096;
  62.                         byte[] bytes = new byte[chunkSize];
  63.                         int byteCount;
  64.  
  65.                         while ((byteCount =
  66.                             resource.Stream.Read(
  67.                             bytes, 0, chunkSize)) > 0)
  68.                         {
  69.                             file.Write(bytes, 0, byteCount);
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.  
  76.         /// <summary>
  77.         /// Zwraca tablicę WriteableBitmap, które zawierają wszystkie klatki (strony) obrazku tiff
  78.         /// </summary>
  79.         /// <param name="fileNameInIsolatedStorage"></param>
  80.         /// <param name="BitmapsFromTiff"></param>
  81.         /// <returns></returns>
  82.         private WriteableBitmap[] GenerateImagesFromTiff(string fileNameInIsolatedStorage, WriteableBitmap[] BitmapsFromTiff)
  83.         {
  84.             BitmapImage bmi = new BitmapImage();
  85.             byte[] buffer = LoadIfExists(fileNameInIsolatedStorage);
  86.             MemoryStream ms = new MemoryStream(buffer);
  87.  
  88.             using (Tiff tiffWorker = Tiff.ClientOpen("myTiffClass", "r", ms, new TiffStream()))
  89.             {
  90.                 short imageDirectories = tiffWorker.NumberOfDirectories();
  91.  
  92.                 if (imageDirectories > 0)
  93.                 {
  94.                     BitmapsFromTiff = new WriteableBitmap[imageDirectories];
  95.  
  96.                     for (int i = 0; i < imageDirectories; i++)
  97.                     {
  98.                         if (tiffWorker.SetDirectory((short)i))
  99.                         {
  100.                             var frameWidthField = tiffWorker.GetField(TiffTag.IMAGEWIDTH);
  101.                             var frameHeightField = tiffWorker.GetField(TiffTag.IMAGELENGTH);
  102.  
  103.                             int frameWidth = frameWidthField != null && frameWidthField.Length > 0 ? frameWidthField[0].ToInt() : 0;
  104.                             int frameHeight = frameHeightField != null && frameHeightField.Length > 0 ? frameHeightField[0].ToInt() : 0;
  105.  
  106.                             WriteableBitmap bmp = null;
  107.                             try
  108.                             {
  109.                                 var buffer1 = new int[frameWidth * frameHeight];
  110.                                 tiffWorker.ReadRGBAImage(frameWidth, frameHeight, buffer1);
  111.  
  112.                                 bmp = new WriteableBitmap(frameWidth, frameHeight);
  113.                                 for (int y = 0; y < frameHeight; y++)
  114.                                 {
  115.                                     var ytif = y * frameWidth;
  116.                                     var ybmp = (frameHeight - y - 1) * frameWidth;
  117.  
  118.                                     for (int x = 0; x < frameWidth; x++)
  119.                                     {
  120.                                         var currentValue = buffer1[ytif + x];
  121.                                         bmp.Pixels[ybmp + x] = Tiff.GetB(currentValue) | Tiff.GetG(currentValue) << 8 | Tiff.GetR(currentValue) << 16 | Tiff.GetA(currentValue) << 24;
  122.                                     }
  123.                                 }
  124.                             }
  125.                             catch (Exception ex)
  126.                             {
  127.                                 MessageBox.Show("Could not load iamges from Tiff");
  128.                             }
  129.  
  130.                             BitmapsFromTiff[i] = bmp;
  131.                         }
  132.                     }
  133.                 }
  134.             }
  135.             return BitmapsFromTiff;
  136.         }
  137.  
  138.         /// <summary>
  139.         /// Zwraca bitmapSource z Contentu projektu na podstawie podanego Uri
  140.         /// </summary>
  141.         /// <param name="uriName"></param>
  142.         /// <param name="uriKind"></param>
  143.         /// <returns></returns>
  144.         private BitmapSource GetBitmapSourceFromImageUri(string uriName, UriKind uriKind)
  145.         {
  146.             Uri uri = new Uri(uriName, uriKind);
  147.             return new BitmapImage(uri);
  148.         }
  149.  
  150.         private void button1_Click(object sender, RoutedEventArgs e)
  151.         {
  152.             _actualIndexInCollection--;
  153.             if (_actualIndexInCollection < 0)
  154.             {
  155.                 _actualIndexInCollection = _collection.Count - 1;
  156.             }
  157.             image1.Source = _collection[_actualIndexInCollection];
  158.         }
  159.  
  160.         private void button2_Click(object sender, RoutedEventArgs e)
  161.         {
  162.             _actualIndexInCollection++;
  163.             if (_actualIndexInCollection > _collection.Count - 1)
  164.             {
  165.                 _actualIndexInCollection = 0;
  166.             }
  167.             image1.Source = _collection[_actualIndexInCollection];
  168.         }
  169.  
  170.         private byte[] LoadIfExists(string filename)
  171.         {
  172.             byte[] retVal;
  173.  
  174.             using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
  175.             {
  176.                 if (iso.FileExists(filename))
  177.                 {
  178.                     using (IsolatedStorageFileStream stream = iso.OpenFile(filename, FileMode.Open))
  179.                     {
  180.                         retVal = new byte[stream.Length];
  181.                         stream.Read(retVal, 0, retVal.Length);
  182.                     }
  183.                 }
  184.                 else
  185.                 {
  186.                     retVal = new byte[0];
  187.                 }
  188.             }
  189.             return retVal;
  190.         }
  191.     }
  192. }
Add Comment
Please, Sign In to add comment