-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDirectBitmapWriter.cs
executable file
·86 lines (74 loc) · 2.72 KB
/
DirectBitmapWriter.cs
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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace _2_convex_hull
{
class DirectBitmapWriter : IDisposable
{
private Bitmap bitmap;
private byte[] imageBuffer;
private BitmapData bitmapData;
private const int pixelWidth = 3;
public DirectBitmapWriter(Bitmap bitmap)
{
this.bitmap = bitmap;
InitializeBuffer();
}
/// <summary>
/// Initializes the bitmap data and imagebuffer.
/// </summary>
private void InitializeBuffer()
{
bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height),
ImageLockMode.WriteOnly, PixelFormat.Format24bppRgb);
imageBuffer = new byte[bitmapData.Stride * bitmap.Height];
}
/// <summary>
/// Sets the pixel in the image buffer.
/// </summary>
/// <param name="point">Coordinates of the pixel</param>
/// <param name="color">The color to be assigned to blue, green, and red.</param>
public void SetPixel(Point point, Color color)
{
SetPixel(point.X, point.Y, color);
}
/// <summary>
/// Sets the pixel in the image buffer.
/// </summary>
/// <param name="x">X-Coordinate of the pixel</param>
/// <param name="y">Y-Coordinate of the pixel</param>
/// <param name="color">The color to be assigned to blue, green, and red.</param>
public void SetPixel(int x, int y, Color color)
{
imageBuffer[bitmapData.Stride * y + x * pixelWidth] = color.R;
imageBuffer[bitmapData.Stride * y + x * pixelWidth + sizeof(byte)] = color.G;
imageBuffer[bitmapData.Stride * y + x * pixelWidth + 2 * sizeof(byte)] = color.B;
}
public void Clear(Color color)
{
for (int y = 0; y < bitmap.Height; ++y)
for (int x = 0; x < bitmap.Width; ++x)
{
SetPixel(x, y, color);
}
}
#region IDisposable Members
public void Dispose()
{
releaseArray();
}
/// <summary>
/// Copy the image buffer back to bitmapdata, then copy it to the original bitmap (image).
/// </summary>
private void releaseArray()
{
Marshal.Copy(imageBuffer, 0, bitmapData.Scan0, bitmapData.Stride * bitmap.Height);
bitmap.UnlockBits(bitmapData);
bitmapData = null;
imageBuffer = null;
}
#endregion
}
}