LockBitmap.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Imaging;
  5. using System.Linq;
  6. using System.Runtime.InteropServices;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace ShadowMerger
  10. {
  11. public class LockBitmap
  12. {
  13. Bitmap source = null;
  14. IntPtr Iptr = IntPtr.Zero;
  15. BitmapData bitmapData = null;
  16. public byte[] Pixels { get; set; }
  17. public int Depth { get; private set; }
  18. public int Width { get; private set; }
  19. public int Height { get; private set; }
  20. public LockBitmap(Bitmap source)
  21. {
  22. this.source = source;
  23. }
  24. public void LockBits()
  25. {
  26. try
  27. {
  28. Width = source.Width;
  29. Height = source.Height;
  30. int PixelCount = Width * Height;
  31. Rectangle rect = new Rectangle(0, 0, Width, Height);
  32. Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
  33. if (Depth != 8 && Depth != 24 && Depth != 32)
  34. {
  35. throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
  36. }
  37. bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
  38. source.PixelFormat);
  39. int step = Depth / 8;
  40. Pixels = new byte[PixelCount * step];
  41. Iptr = bitmapData.Scan0;
  42. Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
  43. }
  44. catch
  45. {
  46. throw;
  47. }
  48. }
  49. public void UnlockBits()
  50. {
  51. try
  52. {
  53. Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);
  54. source.UnlockBits(bitmapData);
  55. }
  56. catch
  57. {
  58. throw;
  59. }
  60. }
  61. public Color GetPixel(int x, int y)
  62. {
  63. Color clr = Color.Empty;
  64. int
  65. cCount = Depth / 8,
  66. i = ((y * Width) + x) * cCount;
  67. if (i > Pixels.Length - cCount)
  68. throw new IndexOutOfRangeException();
  69. if (Depth == 32)
  70. {
  71. byte
  72. b = Pixels[i],
  73. g = Pixels[i + 1],
  74. r = Pixels[i + 2],
  75. a = Pixels[i + 3];
  76. clr = Color.FromArgb(a, r, g, b);
  77. }
  78. if (Depth == 24)
  79. {
  80. byte b = Pixels[i],
  81. g = Pixels[i + 1],
  82. r = Pixels[i + 2];
  83. clr = Color.FromArgb(r, g, b);
  84. }
  85. if (Depth == 8)
  86. {
  87. byte c = Pixels[i];
  88. clr = Color.FromArgb(c, c, c);
  89. }
  90. return clr;
  91. }
  92. public void SetPixel(int x, int y, Color color)
  93. {
  94. int
  95. cCount = Depth / 8,
  96. i = ((y * Width) + x) * cCount;
  97. if (Depth == 32)
  98. {
  99. Pixels[i] = color.B;
  100. Pixels[i + 1] = color.G;
  101. Pixels[i + 2] = color.R;
  102. Pixels[i + 3] = color.A;
  103. }
  104. if (Depth == 24)
  105. {
  106. Pixels[i] = color.B;
  107. Pixels[i + 1] = color.G;
  108. Pixels[i + 2] = color.R;
  109. }
  110. if (Depth == 8)
  111. {
  112. Pixels[i] = color.B;
  113. }
  114. }
  115. }
  116. }