Guest User

Untitled

a guest
Aug 14th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 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. // Załaduj przykładowy plik Tiff
  24. WriteableBitmap[] BitmapsFromTiff = null;
  25. BitmapsFromTiff = GenerateImagesFromTiff("myTiff.tiff", BitmapsFromTiff);
  26.  
  27. foreach (var item in BitmapsFromTiff)
  28. {
  29. _collection.Add(item);
  30. }
  31.  
  32. // Dodaj png
  33. //_collection[_actualIndexInCollection++] = GetBitmapSourceFromImageUri("Background.png", UriKind.Relative);
  34. _collection.Add(GetBitmapSourceFromImageUri("Background.png", UriKind.Relative));
  35.  
  36.  
  37. // Dodaj splashscreenimage
  38. //_collection[_actualIndexInCollection++] = GetBitmapSourceFromImageUri("SplashScreenImage.jpg", UriKind.Relative);
  39. _collection.Add(GetBitmapSourceFromImageUri("SplashScreenImage.jpg", UriKind.Relative));
  40.  
  41.  
  42. // Ustaw indeks na 0 i wyświetl pierwszy obrazek
  43. _actualIndexInCollection = 0;
  44. image1.Source = BitmapsFromTiff[0];
  45. }
  46.  
  47. /// <summary>
  48. /// Zwraca tablicę WriteableBitmap, które zawierają wszystkie klatki (strony) obrazku tiff
  49. /// </summary>
  50. /// <param name="fileNameInIsolatedStorage"></param>
  51. /// <param name="BitmapsFromTiff"></param>
  52. /// <returns></returns>
  53. private WriteableBitmap[] GenerateImagesFromTiff(string fileNameInIsolatedStorage, WriteableBitmap[] BitmapsFromTiff)
  54. {
  55. BitmapImage bmi = new BitmapImage();
  56. byte[] buffer = LoadIfExists(fileNameInIsolatedStorage);
  57. MemoryStream ms = new MemoryStream(buffer);
  58.  
  59. using (Tiff tiffWorker = Tiff.ClientOpen("myTiffClass", "r", ms, new TiffStream()))
  60. {
  61. short imageDirectories = tiffWorker.NumberOfDirectories();
  62.  
  63. if (imageDirectories > 0)
  64. {
  65. BitmapsFromTiff = new WriteableBitmap[imageDirectories];
  66.  
  67. for (int i = 0; i < imageDirectories; i++)
  68. {
  69. if (tiffWorker.SetDirectory((short)i))
  70. {
  71. int tileCount = tiffWorker.NumberOfTiles();
  72. int stripCount = tiffWorker.NumberOfStrips();
  73.  
  74. var frameWidthField = tiffWorker.GetField(TiffTag.IMAGEWIDTH);
  75. var frameHeightField = tiffWorker.GetField(TiffTag.IMAGELENGTH);
  76. var compressionField = tiffWorker.GetField(TiffTag.COMPRESSION);
  77. var xResolutionField = tiffWorker.GetField(TiffTag.XRESOLUTION);
  78. var yResolutionField = tiffWorker.GetField(TiffTag.YRESOLUTION);
  79. var samplesPerPixelField = tiffWorker.GetField(TiffTag.SAMPLESPERPIXEL);
  80.  
  81. int frameWidth = frameWidthField != null && frameWidthField.Length > 0 ? frameWidthField[0].ToInt() : 0;
  82. int frameHeight = frameHeightField != null && frameHeightField.Length > 0 ? frameHeightField[0].ToInt() : 0;
  83. var compression = compressionField != null && compressionField.Length > 0 ? (Compression)compressionField[0].Value : Compression.NONE;
  84. var xResolution = xResolutionField != null && xResolutionField.Length > 0 ? new double?(xResolutionField[0].ToDouble()) : null;
  85. var yResolution = yResolutionField != null && yResolutionField.Length > 0 ? new double?(yResolutionField[0].ToDouble()) : null;
  86. var samplesPerPixel = samplesPerPixelField != null && samplesPerPixelField.Length > 0 ? samplesPerPixelField[0].ToString() : String.Empty;
  87.  
  88. if (xResolution != null && yResolution == null)
  89. {
  90. yResolution = xResolution;
  91. }
  92.  
  93. WriteableBitmap bmp = null;
  94. try
  95. {
  96. var buffer1 = new int[frameWidth * frameHeight];
  97. tiffWorker.ReadRGBAImage(frameWidth, frameHeight, buffer1);
  98.  
  99. bmp = new WriteableBitmap(frameWidth, frameHeight);
  100. for (int y = 0; y < frameHeight; y++)
  101. {
  102. var ytif = y * frameWidth;
  103. var ybmp = (frameHeight - y - 1) * frameWidth;
  104.  
  105. for (int x = 0; x < frameWidth; x++)
  106. {
  107. var currentValue = buffer1[ytif + x];
  108. bmp.Pixels[ybmp + x] = Tiff.GetB(currentValue) | Tiff.GetG(currentValue) << 8 | Tiff.GetR(currentValue) << 16 | Tiff.GetA(currentValue) << 24;
  109. }
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. MessageBox.Show("Could not load iamges from Tiff");
  115. }
  116.  
  117. BitmapsFromTiff[i] = bmp;
  118. }
  119. }
  120. }
  121. }
  122. return BitmapsFromTiff;
  123. }
  124.  
  125. /// <summary>
  126. /// Zwraca bitmapSource z Contentu projektu na podstawie podanego Uri
  127. /// </summary>
  128. /// <param name="uriName"></param>
  129. /// <param name="uriKind"></param>
  130. /// <returns></returns>
  131. private BitmapSource GetBitmapSourceFromImageUri(string uriName, UriKind uriKind)
  132. {
  133. Uri uri = new Uri(uriName, uriKind);
  134. return new BitmapImage(uri);
  135. }
  136.  
  137. private void button1_Click(object sender, RoutedEventArgs e)
  138. {
  139. _actualIndexInCollection--;
  140. if (_actualIndexInCollection < 0)
  141. {
  142. _actualIndexInCollection = _collection.Count - 1;
  143. }
  144. image1.Source = _collection[_actualIndexInCollection];
  145. }
  146.  
  147. private void button2_Click(object sender, RoutedEventArgs e)
  148. {
  149. _actualIndexInCollection++;
  150. if (_actualIndexInCollection > _collection.Count - 1)
  151. {
  152. _actualIndexInCollection = 0;
  153. }
  154. image1.Source = _collection[_actualIndexInCollection];
  155. }
  156.  
  157. private byte[] LoadIfExists(string filename)
  158. {
  159. byte[] retVal;
  160.  
  161. using (IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication())
  162. {
  163. if (iso.FileExists(filename))
  164. {
  165. using (IsolatedStorageFileStream stream = iso.OpenFile(filename, FileMode.Open))
  166. {
  167. retVal = new byte[stream.Length];
  168. stream.Read(retVal, 0, retVal.Length);
  169. }
  170. }
  171. else
  172. {
  173. retVal = new byte[0];
  174. }
  175. }
  176. return retVal;
  177. }
  178. }
  179. }
Add Comment
Please, Sign In to add comment