-
Notifications
You must be signed in to change notification settings - Fork 4
/
bitmap_element.cs
218 lines (194 loc) · 7.41 KB
/
bitmap_element.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace audio_test
{
class bitmap_element
{
PictureBox p;
Bitmap bmap;
public byte[] bmapdata;
public int bmapdatawidth;
public int bmapdataheight;
public int bmapdatastrd;
public bitmap_element(PictureBox pb)
{
p = pb;
bmap = new Bitmap(pb.Width, pb.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
//for (int i = 0; i < 1200; i++) for (int j = 0; j < 100; j++) SetPixel(i, j, Color.Black);
pb.Image = bmap;
// bmap.MakeTransparent(Color.FromArgb(127, 127, 127));
System.Drawing.Imaging.BitmapData bmd;
bmd = bmap.LockBits(new Rectangle(0, 0, bmap.Width, bmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
bmapdata = new byte[bmd.Stride * bmap.Height];
bmapdatawidth = bmap.Width;
bmapdataheight = bmap.Height;
bmapdatastrd = bmd.Stride;
bmap.UnlockBits(bmd);
}
public void SetPixel(int x, int y, int r, int g, int b)
{
if ((x >= 0) && (x < bmapdatawidth) && (y >= 0) && (y < bmapdataheight))
{
int os = y * bmapdatastrd + x * 4;
bmapdata[os + 2] = (byte)r;
bmapdata[os + 1] = (byte)g;
bmapdata[os + 0] = (byte)b;
}
}
public void SetPixel(int x, int y, Color c)
{
if ((x >= 0) && (x < bmapdatawidth) && (y >= 0) && (y < bmapdataheight))
{
int os = y * bmapdatastrd + x * 4;
bmapdata[os + 2] = c.R;
bmapdata[os + 1] = c.G;
bmapdata[os + 0] = c.B;
}
}
public void FillRow(int x, int y1, int y2, Color c)
{
if (x < 0) return;
if (x >= bmapdatawidth) return;
if (y2 < y1) { int t = y1; y1 = y2; y2 = t; }
if (y2 < 0) return;
if (y1 >= bmapdataheight) return;
if (y1 < 0) y1 = 0;
if (y2 >= bmapdataheight) y2 = bmapdataheight - 1;
int os = y1 * bmapdatastrd + x*4;
for (int y = y1; y <= y2; y++)
{
bmapdata[os + 2] = c.R;
bmapdata[os + 1] = c.G;
bmapdata[os + 0] = c.B;
os += bmapdatastrd;
}
}
public void FillCol(int x1, int x2, int y, Color c)
{
if (y < 0) return;
if (y >= bmapdataheight) return;
if (x2 < x1) { int t = x1; x1 = x2; x2 = t; }
if (x2 < 0) return;
if (x1 >= bmapdatawidth) return;
if (x1 < 0) x1 = 0;
if (x2 >= bmapdatawidth) x2 = bmapdatawidth - 1;
int os = y * bmapdatastrd + x1 * 4;
for (int x = x1; x <= x2; x++)
{
bmapdata[os++] = c.B;
bmapdata[os++] = c.G;
bmapdata[os++] = c.R;
os++;
}
}
public void SetRed(int x, int y, int v)
{
if ((x >= 0) && (x < bmapdatawidth) && (y >= 0) && (y < bmapdataheight))
bmapdata[y * bmapdatastrd + x * 4 + 2] = (byte)v;
}
public void SetGreen(int x, int y, int v)
{
if ((x >= 0) && (x < bmapdatawidth) && (y >= 0) && (y < bmapdataheight))
bmapdata[y * bmapdatastrd + x * 4 + 1] = (byte)v;
}
public void SetBlue(int x, int y, int v)
{
if ((x >= 0) && (x < bmapdatawidth) && (y >= 0) && (y < bmapdataheight))
bmapdata[y * bmapdatastrd + x * 4 + 0] = (byte)v;
}
public void drawbox(int x1, int y1, int x2, int y2, int cr, int cg, int cb)
{
if (x1 > x2) { int t = x1; x1 = x2; x2 = t; }
if (y1 > y2) { int t = y1; y1 = y2; y2 = t; }
if ((x2 < 0) || (x1 >= bmapdatawidth) || (y2 < 0) || (y1 >= bmapdataheight)) return;
if (x1 < 0) x1 = 0;
if (x2 >= bmapdatawidth) x2 = bmapdatawidth-1;
if (y1 < 0) y1 = 0;
if (y2 >= bmapdataheight) y2 = bmapdataheight-1;
x2=x2-x1;
for (int y=y1;y<=y2;y++) {
int os=y*bmapdatastrd+x1*4;
for (int i=0;i<=x2;i++) {
bmapdata[os]=(byte)cb;os++;
bmapdata[os]=(byte)cg;os++;
bmapdata[os]=(byte)cr;os++;
os++;
}
}
}
public void drawframe(int x1, int y1, int x2, int y2, int ofs, Color c)
{
if (x1 > x2) { int t = x1; x1 = x2; x2 = t; }
if (y1 > y2) { int t = y1; y1 = y2; y2 = t; }
FillCol(x1 - ofs, x2 + ofs, y1 - ofs, c);
FillCol(x1 - ofs, x2 + ofs, y2 + ofs, c);
FillRow(x1 - ofs, y1 - ofs + 1, y2 + ofs - 1, c);
FillRow(x2 + ofs, y1 - ofs + 1, y2 + ofs - 1, c);
}
public void drawframe(Control ctrl, int ofs, Color c)
{
int x1 = ctrl.Location.X;
int y1 = ctrl.Location.Y;
int x2 = x1 + ctrl.Size.Width - 1;
int y2 = y1 + ctrl.Size.Height - 1;
drawframe(x1, y1, x2, y2, ofs, c);
}
public void push()
{
// int sx = bmap.Height;
// int w = 1200 - sx;
System.Buffer.BlockCopy(bmapdata, bmapdatastrd, bmapdata, 0, (bmapdataheight - 1) * bmapdatastrd);
// for (int y = 1; y < 100; y++) System.Buffer.BlockCopy(bmapdata, y * bmapdatastrd + sx * 4, bmapdata, (y - 1) * bmapdatastrd + sx * 4, w * 4);
}
public void actualize()
{
System.Drawing.Imaging.BitmapData bmd;
bmd = bmap.LockBits(new Rectangle(0, 0, bmap.Width, bmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format32bppRgb);
// bmd = bmap.LockBits(new Rectangle(0, 0, bmap.Width, bmap.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, bmap.PixelFormat);
System.Runtime.InteropServices.Marshal.Copy(bmapdata, 0, bmd.Scan0, bmapdata.Length);
bmap.UnlockBits(bmd);
p.Refresh();
}
public void clear()
{
bmapdata.Initialize();
}
public void fill(Color c)
{
int os = 0;
byte r = c.R;
byte g = c.G;
byte b = c.B;
for (int y = 0; y < bmapdataheight; y++)
{
int tos=os;
for (int x = 0; x < bmapdatawidth; x++)
{
bmapdata[tos++] = b;
bmapdata[tos++] = g;
bmapdata[tos++] = r;
tos++;
}
os += bmapdatastrd;
}
}
public void fade()
{
for (int i = 0; i < bmapdataheight; i++)
{
int os = i * bmapdatastrd;
int es = os + 4 * bmapdatawidth;
for (int j = os; j < es; j++)
{
int c = bmapdata[j];
if (c < 2) c = 0; else c = c * 4 / 5;
bmapdata[j] =(byte) c;
}
}
}
}
}