MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
LockBitmap.cs
1using System;
2using System.Collections.Generic;
3using System.Drawing;
4using System.Drawing.Imaging;
5using System.Linq;
6using System.Runtime.InteropServices;
7using System.Text;
8using System.Threading.Tasks;
9
10namespace MyCaffe.basecode
11{
20 public class LockBitmap
21 {
22 Bitmap source = null;
23 IntPtr Iptr = IntPtr.Zero;
24 BitmapData bitmapData = null;
25
29 public byte[] Pixels { get; set; }
33 public int Depth { get; private set; }
37 public int Width { get; private set; }
41 public int Height { get; private set; }
42
47 public LockBitmap(Bitmap source)
48 {
49 this.source = source;
50 }
51
55 public void LockBits()
56 {
57 try
58 {
59 // Get width and height of bitmap
60 Width = source.Width;
61 Height = source.Height;
62
63 // get total locked pixels count
64 int PixelCount = Width * Height;
65
66 // Create rectangle to lock
67 Rectangle rect = new Rectangle(0, 0, Width, Height);
68
69 // get source bitmap pixel format size
70 Depth = System.Drawing.Bitmap.GetPixelFormatSize(source.PixelFormat);
71
72 // Check if bpp (Bits Per Pixel) is 8, 24, or 32
73 if (Depth != 8 && Depth != 24 && Depth != 32)
74 {
75 throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
76 }
77
78 // Lock bitmap and return bitmap data
79 bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,
80 source.PixelFormat);
81
82 // create byte array to copy pixel values
83 int step = Depth / 8;
84 Pixels = new byte[PixelCount * step];
85 Iptr = bitmapData.Scan0;
86
87 // Copy data from pointer to array
88 Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
89 }
90 catch (Exception ex)
91 {
92 throw ex;
93 }
94 }
95
99 public void UnlockBits()
100 {
101 try
102 {
103 // Copy data from byte array to pointer
104 Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);
105
106 // Unlock bitmap data
107 source.UnlockBits(bitmapData);
108 }
109 catch (Exception ex)
110 {
111 throw ex;
112 }
113 }
114
121 public Color GetPixel(int x, int y)
122 {
123 Color clr = Color.Empty;
124
125 // Get color components count
126 int cCount = Depth / 8;
127
128 // Get start index of the specified pixel
129 int i = ((y * Width) + x) * cCount;
130
131 if (i > Pixels.Length - cCount)
132 throw new IndexOutOfRangeException();
133
134 if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
135 {
136 byte b = Pixels[i];
137 byte g = Pixels[i + 1];
138 byte r = Pixels[i + 2];
139 byte a = Pixels[i + 3]; // a
140 clr = Color.FromArgb(a, r, g, b);
141 }
142 if (Depth == 24) // For 24 bpp get Red, Green and Blue
143 {
144 byte b = Pixels[i];
145 byte g = Pixels[i + 1];
146 byte r = Pixels[i + 2];
147 clr = Color.FromArgb(r, g, b);
148 }
149 if (Depth == 8)
150 // For 8 bpp get color value (Red, Green and Blue values are the same)
151 {
152 byte c = Pixels[i];
153 clr = Color.FromArgb(c, c, c);
154 }
155 return clr;
156 }
157
164 public void SetPixel(int x, int y, Color color)
165 {
166 // Get color components count
167 int cCount = Depth / 8;
168
169 // Get start index of the specified pixel
170 int i = ((y * Width) + x) * cCount;
171
172 if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha
173 {
174 Pixels[i] = color.B;
175 Pixels[i + 1] = color.G;
176 Pixels[i + 2] = color.R;
177 Pixels[i + 3] = color.A;
178 }
179 if (Depth == 24) // For 24 bpp set Red, Green and Blue
180 {
181 Pixels[i] = color.B;
182 Pixels[i + 1] = color.G;
183 Pixels[i + 2] = color.R;
184 }
185 if (Depth == 8)
186 // For 8 bpp set color value (Red, Green and Blue values are the same)
187 {
188 Pixels[i] = color.B;
189 }
190 }
191 }
192}
The LockBitmap class provides very efficient SetPixel and GetPixel functionality of a bitmap by using...
Definition: LockBitmap.cs:21
Color GetPixel(int x, int y)
Get the color of the specified pixel
Definition: LockBitmap.cs:121
void SetPixel(int x, int y, Color color)
Set the color of the specified pixel
Definition: LockBitmap.cs:164
LockBitmap(Bitmap source)
Sets the source bitmap.
Definition: LockBitmap.cs:47
void UnlockBits()
Unlock bitmap data, releasing its underlying data.
Definition: LockBitmap.cs:99
int Width
Returns the width of the bitmap.
Definition: LockBitmap.cs:37
byte[] Pixels
Get/set the raw pixel byte data.
Definition: LockBitmap.cs:29
int Depth
Returns the depth of the bitmap.
Definition: LockBitmap.cs:33
int Height
Returns the height of the bitmap.
Definition: LockBitmap.cs:41
void LockBits()
Lock bitmap data to access its underlying raw data.
Definition: LockBitmap.cs:55
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12