Advertisement
miguelmartins1987

Convert TIFF to RGB24 (BitmapFrame to pixels byte array)

Nov 18th, 2020
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.08 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Windows.Media;
  4. using System.Windows.Media.Imaging;
  5.  
  6. namespace ConsoleApp1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             TiffBitmapDecoder tiffBitmapDecoder = new TiffBitmapDecoder(new Uri(@"D:\sunset.tif"), BitmapCreateOptions.None, BitmapCacheOption.Default);
  13.             BitmapFrame bitmapFrame = tiffBitmapDecoder.Frames[0];
  14.  
  15.             FormatConvertedBitmap formatConvertedBitmap = new FormatConvertedBitmap();
  16.             formatConvertedBitmap.BeginInit();
  17.             formatConvertedBitmap.Source = bitmapFrame;
  18.             formatConvertedBitmap.DestinationFormat = PixelFormats.Rgb24;
  19.             formatConvertedBitmap.EndInit();
  20.  
  21.             int stride = ((formatConvertedBitmap.PixelWidth * formatConvertedBitmap.Format.BitsPerPixel + 31) >> 5) << 2;
  22.             byte[] pixels = new byte[formatConvertedBitmap.PixelHeight * stride];
  23.             formatConvertedBitmap.CopyPixels(pixels, stride, 0);
  24.  
  25.             File.WriteAllBytes(@"D:\sunset_rgb24.raw", pixels);
  26.         }
  27.     }
  28. }
  29.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement