Guest User

Untitled

a guest
Jul 16th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.67 KB | None | 0 0
  1. Iti trebuie intai cele 2 metode enumerate mai jos (iti zic doar varianta mai rapida dintre multiplele variante):
  2.  
  3. Iti va trebui de asemenea ultimul release BitMiracle.LibTiff
  4.  
  5.         private static WriteableBitmap workingWBM;
  6.         public WriteableBitmap LoadPage(short page, Tiff timage)
  7.         {
  8.             FieldValue[] value = timage.GetField(TiffTag.IMAGEWIDTH);
  9.             int width = value[0].ToInt();
  10.             value = timage.GetField(TiffTag.IMAGELENGTH);
  11.             int height = value[0].ToInt();
  12.  
  13.             value = timage.GetField(TiffTag.BITSPERSAMPLE);
  14.             int bps = value[0].ToShort();
  15.  
  16.             value = timage.GetField(TiffTag.SAMPLESPERPIXEL);
  17.             int spp = value[0].ToShort();
  18.  
  19.             int imageSize = height * width;
  20.             int[] raster = new int[imageSize];
  21.  
  22.             timage.SetDirectory(page);
  23.             //citim in buffer imaginea
  24.             if (!timage.ReadRGBAImage(width, height, raster))
  25.             {
  26.                 System.Console.Error.WriteLine("Eroare la citire (format imagine?)");
  27.                 return null;
  28.             }
  29.  
  30.             workingWBM = new WriteableBitmap(width, height);
  31.             //vertical flip, poveste lunga :)
  32.             int cnt = 0;
  33.             for (int e = height - 1; e != -1; e--)
  34.             {
  35.                 for (int c = 0; c < width; c++)
  36.                 {
  37.                     int red = Tiff.GetR(raster[e * width + c]);
  38.                     int green = Tiff.GetG(raster[e * width + c]);
  39.                     int blue = Tiff.GetB(raster[e * width + c]);
  40.                     //Array.Copy(
  41.                     workingWBM.Pixels[cnt] = (255 << 24) + (red << 16) + (green << 8) + (blue);
  42.                     cnt++;
  43.                 }
  44.             }
  45.         //teste
  46.             //wb.Render(
  47.             //BitmapImage bi = new BitmapImage();
  48.             //bi.
  49.             //wb.Render(this.image, new MatrixTransform());
  50.             //this.image.Source = wb;
  51.             //timage.Close();
  52.             return workingWBM;
  53.         }
  54.  
  55.         public class MemTiffStream : TiffStream
  56.         {
  57.             public MemoryStream memStream;
  58.             public MemTiffStream(MemoryStream ms)
  59.                 : base()
  60.             {
  61.                 memStream = ms;
  62.             }
  63.  
  64.             public override int Read(object clientData, byte[] buffer, int offset, int count)
  65.             {
  66.                 return memStream.Read(buffer, offset, count);
  67.             }
  68.  
  69.             public override void Close(object clientData)
  70.             {
  71.                 memStream.Close();
  72.             }
  73.  
  74.             public override long Seek(object clientData, long offset, System.IO.SeekOrigin origin)
  75.             {
  76.                 return memStream.Seek(offset, origin);
  77.             }
  78.  
  79.             public override long Size(object clientData)
  80.             {
  81.                 return memStream.Length;
  82.             }
  83.         }
  84.  
  85.  
  86.      //exemplu de usage:
  87.      //loadOp.value este un byte[] care contine tiff-ul multi page
  88.      this.imageControl1.Images = null;                                            
  89.      using (var ms = new MemoryStream())
  90.      {
  91.          ms.Write(loadOp.Value, 0, loadOp.Value.Length);
  92.          ms.Position = 0;
  93.          
  94.          var mts = new MemTiffStream(ms);
  95.          var timage = Tiff.ClientOpen("a", "r", null, mts);
  96.          var lstimg = new List<WriteableBitmap>();
  97.          for (short i = 0; i < timage.NumberOfDirectories(); i++)
  98.            {
  99.              try
  100.              {
  101.                var wb = LoadPage(i, timage);
  102.                lstimg.Add(wb);
  103.              }
  104.              catch { }
  105.            }
  106.          this.imageControl1.Images = lstimg;  
  107.      }
Add Comment
Please, Sign In to add comment