-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitmap32.cs
More file actions
141 lines (128 loc) · 4.2 KB
/
Copy pathBitmap32.cs
File metadata and controls
141 lines (128 loc) · 4.2 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
using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public class Bitmap32
{
public const int PixelSizeBytes = 4; // 4 bytes/pixel.
public const int PixelSizeBits = PixelSizeBytes * 8; // 32 bits per pixel.
// Provide public access to the picture's byte data.
public byte[] ImageBytes;
public int RowSizeBytes;
public const int PixelDataSize = 32;
// A reference to the Bitmap.
private Bitmap m_Bitmap;
// Save a reference to the bitmap.
public Bitmap32(Bitmap bm)
{
m_Bitmap = bm;
}
// Bitmap data.
private BitmapData m_BitmapData;
// Return the image's dimensions.
public int Width
{
get
{
return m_Bitmap.Width;
}
}
public int Height
{
get
{
return m_Bitmap.Height;
}
}
// Provide easy access to the color values.
public void GetPixel(int x, int y, out byte red, out byte green, out byte blue, out byte alpha)
{
int i = y * m_BitmapData.Stride + x * 4;
blue = ImageBytes[i++];
green = ImageBytes[i++];
red = ImageBytes[i++];
alpha = ImageBytes[i];
}
public void SetPixel(int x, int y, byte red, byte green, byte blue, byte alpha)
{
int i = y * m_BitmapData.Stride + x * 4;
ImageBytes[i++] = blue;
ImageBytes[i++] = green;
ImageBytes[i++] = red;
ImageBytes[i] = alpha;
}
public byte GetBlue(int x, int y)
{
int i = y * m_BitmapData.Stride + x * 4;
return ImageBytes[i];
}
public void SetBlue(int x, int y, byte blue)
{
int i = y * m_BitmapData.Stride + x * 4;
ImageBytes[i] = blue;
}
public byte GetGreen(int x, int y)
{
int i = y * m_BitmapData.Stride + x * 4;
return ImageBytes[i + 1];
}
public void SetGreen(int x, int y, byte green)
{
int i = y * m_BitmapData.Stride + x * 4;
ImageBytes[i + 1] = green;
}
public byte GetRed(int x, int y)
{
int i = y * m_BitmapData.Stride + x * 4;
return ImageBytes[i + 2];
}
public void SetRed(int x, int y, byte red)
{
int i = y * m_BitmapData.Stride + x * 4;
ImageBytes[i + 2] = red;
}
public byte GetAlpha(int x, int y)
{
int i = y * m_BitmapData.Stride + x * 4;
return ImageBytes[i + 3];
}
public void SetAlpha(int x, int y, byte alpha)
{
int i = y * m_BitmapData.Stride + x * 4;
ImageBytes[i + 3] = alpha;
}
// Lock the bitmap's data.
public void LockBitmap()
{
// Lock the bitmap data.
Rectangle bounds = new Rectangle(
0, 0, m_Bitmap.Width, m_Bitmap.Height);
m_BitmapData = m_Bitmap.LockBits(bounds,
ImageLockMode.ReadWrite,
PixelFormat.Format32bppArgb);
RowSizeBytes = m_BitmapData.Stride;
// Allocate room for the data.
int total_size = m_BitmapData.Stride * m_BitmapData.Height;
ImageBytes = new byte[total_size];
// Copy the data into the ImageBytes array.
Marshal.Copy(m_BitmapData.Scan0, ImageBytes, 0, total_size);
}
// Copy the data back into the Bitmap
// and release resources.
public void UnlockBitmap()
{
// Copy the data back into the bitmap.
int total_size = m_BitmapData.Stride * m_BitmapData.Height;
Marshal.Copy(ImageBytes, 0, m_BitmapData.Scan0, total_size);
// Unlock the bitmap.
m_Bitmap.UnlockBits(m_BitmapData);
// Release resources.
ImageBytes = null;
m_BitmapData = null;
}
}
}