TizzyT

LockBitmap

Apr 19th, 2019
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.39 KB | None | 0 0
  1.     using System;
  2.     using System.Drawing;
  3.     using System.Drawing.Imaging;
  4.     using System.Runtime.InteropServices;
  5.  
  6.     public class LockBitmap
  7.     {
  8.         private Bitmap source = null;
  9.         private IntPtr Iptr = IntPtr.Zero;
  10.         private BitmapData bitmapData = null;
  11.         public byte[] Pixels { get; set; }
  12.         public int Depth { get; private set; }
  13.         public int Width { get; private set; }
  14.         public int Height { get; private set; }
  15.  
  16.         public LockBitmap(Bitmap source)
  17.         {
  18.             this.source = source;
  19.         }
  20.  
  21.         public void LockBits()
  22.         {
  23.             try
  24.             {
  25.                 Width = source.Width;
  26.                 Height = source.Height;
  27.                 int PixelCount = Width * Height;
  28.                 Rectangle rect = new Rectangle(0, 0, Width, Height);
  29.                 Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
  30.                 if (Depth != 8 && Depth != 24 && Depth != 32)
  31.                     throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
  32.                 bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite, source.PixelFormat);
  33.                 int step = (int)(Depth / 8.0);
  34.                 Pixels = new byte[PixelCount * step - 1 + 1];
  35.                 Iptr = bitmapData.Scan0;
  36.                 Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
  37.             }
  38.             catch (Exception ex)
  39.             {
  40.                 throw ex;
  41.             }
  42.         }
  43.  
  44.         public void UnlockBits()
  45.         {
  46.             try
  47.             {
  48.                 Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);
  49.                 source.UnlockBits(bitmapData);
  50.             }
  51.             catch (Exception ex)
  52.             {
  53.                 throw ex;
  54.             }
  55.         }
  56.  
  57.         public Color GetPixel(int x, int y)
  58.         {
  59.             Color clr = Color.Empty;
  60.             int cCount = (int)(Depth / 8.0);
  61.             int i = ((y * Width) + x) * cCount;
  62.             if (i > Pixels.Length - cCount)
  63.                 throw new IndexOutOfRangeException();
  64.             if (Depth == 32)
  65.             {
  66.                 byte b = Pixels[i];
  67.                 byte g = Pixels[i + 1];
  68.                 byte r = Pixels[i + 2];
  69.                 byte a = Pixels[i + 3];
  70.                 clr = Color.FromArgb(a, r, g, b);
  71.             }
  72.             if (Depth == 24)
  73.             {
  74.                 byte b = Pixels[i];
  75.                 byte g = Pixels[i + 1];
  76.                 byte r = Pixels[i + 2];
  77.                 clr = Color.FromArgb(r, g, b);
  78.             }
  79.             if (Depth == 8)
  80.             {
  81.                 byte c = Pixels[i];
  82.                 clr = Color.FromArgb(c, c, c);
  83.             }
  84.             return clr;
  85.         }
  86.  
  87.         public void SetPixel(int x, int y, Color color)
  88.         {
  89.             int cCount = (int)(Depth / 8.0);
  90.             int i = ((y * Width) + x) * cCount;
  91.             if (Depth == 32)
  92.             {
  93.                 Pixels[i] = color.B;
  94.                 Pixels[i + 1] = color.G;
  95.                 Pixels[i + 2] = color.R;
  96.                 Pixels[i + 3] = color.A;
  97.             }
  98.             if (Depth == 24)
  99.             {
  100.                 Pixels[i] = color.B;
  101.                 Pixels[i + 1] = color.G;
  102.                 Pixels[i + 2] = color.R;
  103.             }
  104.             if (Depth == 8)
  105.                 Pixels[i] = color.B;
  106.         }
  107.     }
Advertisement
Add Comment
Please, Sign In to add comment