LockBitmap.cs 3.3 KB

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