Advertisement
Jimi2000

SystemDrawing_BitmapInfo

Mar 28th, 2018
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.98 KB | None | 0 0
  1. using System.IO;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4.  
  5.  
  6.         //Uncomplete update: coming soon
  7.         public class BitmapInfo
  8.         {
  9.             private ImageFlags flags;
  10.             public ImagingBitmapInfo()
  11.             {
  12.                 this.flags = ImageFlags.None;
  13.                 this.BitmapData = new BitmapDataInfo();
  14.             }
  15.  
  16.             public BitmapDataInfo BitmapData  { get; set; }
  17.             public ImageFormat ImageFormat { get; set; }
  18.             public string ImageType { get; set; }
  19.             public string MimeType { get; set; }
  20.             public SizeF ImageSize { get; set; }
  21.             public SizeF Dpi { get; set; }
  22.             public Size PixelSize { get; set; }
  23.             public int PixelFormatSize { get; set; }
  24.             public int BitsPerPixel { get; set; }
  25.             public PixelFormat PixelFormat { get; set; }
  26.             public bool HasPalette { get; set; }
  27.             public ColorPalette Palette { get; set; }
  28.             public bool HasAnimation { get; set; }
  29.             public ImageFlags Flags {
  30.                 get { return this.flags; }
  31.                 set { this.BitmapData.SetValues(value);
  32.                       this.flags = value; }
  33.             }
  34.  
  35.             public bool IsGrayScale()
  36.             { return CheckIfGrayScale(false); }
  37.  
  38.             public bool IsGrayScale(bool DeepScan)
  39.             { return CheckIfGrayScale(DeepScan); }
  40.  
  41.             private bool CheckIfGrayScale(bool DeepScan)
  42.             {
  43.                 bool result = false;
  44.  
  45.                 if ((this.BitmapData.ColorSpaceGRAY == true ) ||
  46.                     (this.PixelFormat == PixelFormat.Format16bppGrayScale))
  47.                     result = true;
  48.                 else if (this.PixelFormat == PixelFormat.Format8bppIndexed ||
  49.                          this.PixelFormat == PixelFormat.Format4bppIndexed ||
  50.                           this.PixelFormat == PixelFormat.Format1bppIndexed)
  51.                         DeepScan = true;
  52.  
  53.                 if (DeepScan & this.Palette != null)
  54.                 {
  55.                     List<Color> IndexedColors = this.Palette.Entries.ToList();
  56.                     result = IndexedColors.All(rgb => (rgb.R == rgb.G && rgb.G == rgb.B && rgb.B == rgb.R));
  57.                 }
  58.                 return result;
  59.             }
  60.  
  61.             public class BitmapDataInfo
  62.             {
  63.                 public bool HasFlags { get; private set; }
  64.                 public bool HasAlpha { get; private set; }
  65.                 public bool IsTranslucent { get; private set; }
  66.                 public bool IsScalable { get; private set; }
  67.                 public bool IsPartiallyScalable { get; private set; }
  68.                 public bool ColorSpaceRGB { get; private set; }
  69.                 public bool ColorSpaceCMYK { get; private set; }
  70.                 public bool ColorSpaceGRAY { get; private set; }
  71.                 public bool ColorSpaceYCBCR { get; private set; }
  72.                 public bool ColorSpaceYCCK { get; private set; }
  73.                 public bool HasRealDPI { get; private set; }
  74.                 public bool HasRealPixelSize { get; private set; }
  75.  
  76.                 internal void SetValues(ImageFlags Flags)
  77.                 {
  78.                     this.HasFlags = (Flags > 0);
  79.                     if (Flags > 0)
  80.                     {
  81.                         this.HasAlpha = ((Flags & ImageFlags.HasAlpha) > 0);
  82.                         this.HasRealDPI = ((Flags & ImageFlags.HasRealDpi) > 0);
  83.                         this.HasRealPixelSize = ((Flags & ImageFlags.HasRealPixelSize) > 0);
  84.                         this.IsTranslucent = ((Flags & ImageFlags.HasTranslucent) > 0);
  85.                         this.IsPartiallyScalable = ((Flags & ImageFlags.PartiallyScalable) > 0);
  86.                         this.IsScalable = ((Flags & ImageFlags.Scalable) > 0);
  87.                         this.ColorSpaceCMYK = ((Flags & ImageFlags.ColorSpaceCmyk) > 0);
  88.                         this.ColorSpaceGRAY = ((Flags & ImageFlags.ColorSpaceGray) > 0);
  89.                         this.ColorSpaceRGB = ((Flags & ImageFlags.ColorSpaceRgb) > 0);
  90.                         this.ColorSpaceYCBCR = ((Flags & ImageFlags.ColorSpaceYcbcr) > 0);
  91.                         this.ColorSpaceYCCK = ((Flags & ImageFlags.ColorSpaceYcck) > 0);
  92.                     }
  93.                 }
  94.             }
  95.         }
  96.  
  97.         public static ImagingBitmapInfo BitmapPixelFormat(string FileName)
  98.         {
  99.             using (FileStream stream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.None))
  100.             {
  101.                 return BitmapPixelFormat(stream);
  102.             }
  103.         }
  104.  
  105.         public static ImagingBitmapInfo BitmapPixelFormat(Stream stream)
  106.         {
  107.             ImagingBitmapInfo imageInfo = new ImagingBitmapInfo();
  108.             Image image = Image.FromStream(stream);
  109.             imageInfo.PixelFormat = image.PixelFormat;
  110.             imageInfo.ImageSize = image.PhysicalDimension;
  111.             imageInfo.HasPalette = (image.Palette.Entries.Length > 0) ? true : false;
  112.             Guid[] FrameList = image.FrameDimensionsList;
  113.             if (FrameList.Length > 0)
  114.                 imageInfo.HasAnimation = FrameList.Any(f => f == FrameDimension.Time.Guid);
  115.             imageInfo.Palette = image.Palette;
  116.             imageInfo.Dpi = new SizeF(image.HorizontalResolution, image.VerticalResolution);
  117.             imageInfo.Flags = (ImageFlags)image.Flags;
  118.             imageInfo.PixelSize = image.Size;
  119.             imageInfo.ImageFormat = image.RawFormat;
  120.             imageInfo.PixelFormatSize = Image.GetPixelFormatSize(image.PixelFormat);
  121.             ImageCodecInfo codec = ImageCodecInfo.GetImageDecoders()
  122.                                                     .FirstOrDefault(enc =>
  123.                                                         enc.FormatID == image.RawFormat.Guid);
  124.             imageInfo.ImageType = codec.FilenameExtension.ToLowerInvariant();
  125.             imageInfo.MimeType = codec.MimeType;
  126.  
  127.             return imageInfo;
  128.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement