-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLockBitmap.cs
More file actions
191 lines (163 loc) · 4.59 KB
/
LockBitmap.cs
File metadata and controls
191 lines (163 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace FastBitmap
{
// Source: http://www.codeproject.com/Tips/240428/Work-with-bitmap-faster-with-Csharp
// http://stackoverflow.com/questions/24701703/c-sharp-faster-alternatives-to-setpixel-and-getpixel-for-bitmaps-for-windows-f
// License: http://www.codeproject.com/info/cpol10.aspx
public unsafe class LockBitmap : IDisposable
{
public int Depth { get; private set; }
public int Width { get; private set; }
public int Height { get; private set; }
public Bitmap Source { get { return source; }}
Bitmap source = null;
byte* Iptr = null;
BitmapData bitmapData = null;
bool isLocked = false;
long Length = -1;
public LockBitmap(Bitmap source)
{
if (source == null) {
throw new ArgumentNullException("source");
}
this.source = source;
}
/// <summary>
/// Lock bitmap data
/// </summary>
public void LockBits()
{
// Get width and height of bitmap
Width = source.Width;
Height = source.Height;
// get total locked pixels count
long pixelCount = Width * Height;
// Create rectangle to lock
Rectangle rect = new Rectangle(0, 0, Width, Height);
// get source bitmap pixel format size
Depth = Image.GetPixelFormatSize(source.PixelFormat);
//set the length in bytes (total pixels * bytes/pixel)
Length = pixelCount * Depth / 8;
// Check if bpp (Bits Per Pixel) is 8, 24, or 32
if (Depth != 8 && Depth != 24 && Depth != 32)
{
throw new ArgumentException("Only 8, 24 and 32 bpp images are supported.");
}
// Lock bitmap and return bitmap data
isLocked = true;
bitmapData = source.LockBits(rect, ImageLockMode.ReadWrite,source.PixelFormat);
// create byte array to copy pixel values
//int step = Depth / 8;
//Pixels = new byte[PixelCount * step];
Iptr = (byte*)bitmapData.Scan0;
// Copy data from pointer to array
//Marshal.Copy(Iptr, Pixels, 0, Pixels.Length);
}
/// <summary>
/// Unlock bitmap data
/// </summary>
public void UnlockBits()
{
// Copy data from byte array to pointer
//Marshal.Copy(Pixels, 0, Iptr, Pixels.Length);
// Unlock bitmap data
source.UnlockBits(bitmapData);
isLocked = false;
}
/// <summary>
/// Get the color of the specified pixel
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public Color GetPixel(int x, int y)
{
if (!isLocked) {
throw new BadStateException("Bitmap must be locked first");
}
Color clr = Color.Empty;
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
int i = ((y * Width) + x) * cCount;
if (i > Length - cCount) {
throw new IndexOutOfRangeException();
}
if (Depth == 32) // For 32 bpp get Red, Green, Blue and Alpha
{
byte b = Iptr[i];
byte g = Iptr[i + 1];
byte r = Iptr[i + 2];
byte a = Iptr[i + 3]; // a
clr = Color.FromArgb(a, r, g, b);
}
if (Depth == 24) // For 24 bpp get Red, Green and Blue
{
byte b = Iptr[i];
byte g = Iptr[i + 1];
byte r = Iptr[i + 2];
clr = Color.FromArgb(r, g, b);
}
if (Depth == 8) // For 8 bpp get color value (Red, Green and Blue values are the same)
{
byte c = Iptr[i];
clr = Color.FromArgb(c, c, c);
}
return clr;
}
/// <summary>
/// Set the color of the specified pixel
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="color"></param>
public void SetPixel(int x, int y, Color color)
{
if (!isLocked) {
throw new BadStateException("Bitmap must be locked first");
}
// Get color components count
int cCount = Depth / 8;
// Get start index of the specified pixel
int i = ((y * Width) + x) * cCount;
if (i > Length - cCount) {
throw new IndexOutOfRangeException();
}
if (Depth == 32) // For 32 bpp set Red, Green, Blue and Alpha
{
Iptr[i] = color.B;
Iptr[i + 1] = color.G;
Iptr[i + 2] = color.R;
Iptr[i + 3] = color.A;
}
if (Depth == 24) // For 24 bpp set Red, Green and Blue
{
Iptr[i] = color.B;
Iptr[i + 1] = color.G;
Iptr[i + 2] = color.R;
}
if (Depth == 8) // For 8 bpp set color value (Red, Green and Blue values are the same)
{
Iptr[i] = color.B;
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (isLocked) { this.UnlockBits(); }
}
}
}
}