c#指针进行图像操作
2010-05-28 08:55
411 查看
提及.NET的指针操作,很多人并不是很了解,甚至还不知道有这么个东东
由于C#的指针操作属于unsafe操作,所以很多人对unsafe使用起来都很谨慎
其实所谓不安全代码,其实是不受控于CLR控制下的托管执行,相当于CLR领导下的部分区域自治,
当然CLR也不会去关心不安全代码的内存分配和回收
费话少说,先体验一下适当的指针操作带来的性能提升。
平时我们通过GDI+操作稍大点图片象素时,都会有一种濒临崩溃的感觉,我们转下用指针操作
1 Bitmap map = new Bitmap(path);
2 // 将图片位图区域进行锁定
3 BitmapData mapdata = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
4 unsafe
5 {
6 byte* pixpoint;
7 int newpoint;
8
9 for (int i = 0; i < map.Width; i++)
10 {
11 for (int j = 0; j < map.Height; j++)
12 {
13 // 位图结构存在一个字节对齐问题。
14 pixpoint = (byte*)mapdata.Scan0; +i * 3 + j * mapdata.Stride;
15 newpoint = (*pixpoint * 11 + *(pixpoint + 1) * 59 + *(pixpoint + 2) * 30) / 100;
16 *(pixpoint++) = (byte)newpoint;
17 *(pixpoint++) = (byte)newpoint;
18 *(pixpoint++) = (byte)newpoint;
19
20 // 非指针操作
21 // Color pxcl=map.GetPixel(i, j);
22 // map.SetPixel(i, j, (pxcl.R * 11 + pxcl.G * 59 + pxcl * 30) / 100);
23 }
24 }
25 }
26 // 将位图内存区域进行解锁
27 map.UnlockBits(mapdata);
28 map.Dispose();
比较GDI+使用的GetPixel/SetPixel,让你立马感到这个世界多么美妙
再来一个中值滤波的操作
1 /// <summary>
2 /// 中值滤波
3 /// </summary>
4 /// <param name="dgGrayValue"></param>
5 public void MedianFilter(int dgGrayValue)
6 {
7 // 100M 双线空间 + 50M数据库 28元/年
8 // 1G 双线空间 + 200M数据库 100元/年
9 // QQ:70975363
10 byte s;
11 byte[] p = new byte[9];
12 int i, j;
13 int x, y;
14 int Stride;
15 unsafe
16 {
17 byte* point = (byte*)this.ImageData.Scan0;
18 Stride = this.ImageData.Stride;
19 point = point + 3 + this.ImageData.Stride;
20 for (i = 0; i < this.ImageData.Height - 1; i++)
21 {
22 for (j = 0; j < this.ImageData.Width - 1; j++)
23 {
24 p[0] = *(point - Stride - 1);
25 p[1] = *(point - Stride + 2);
26 p[2] = *(point - Stride + 5);
27 p[3] = *(point - 1);
28 p[4] = *(point + 2);
29 p[5] = *(point + 5);
30 p[6] = *(point + Stride - 1);
31 p[7] = *(point + Stride + 2);
32 p[8] = *(point + Stride + 5);
33 for (x = 0; x < 5; x++)
34 {
35 for (y = x + 1; y < 9; y++)
36 {
37 if (p[x] > p[y])
38 {
39 s = p[x];
40 p[x] = p[y];
41 p[y] = s;
42 }
43 }
44 }
45 *point = p[4];
46 *(point - 1) = p[4];
47 *(point - 2) = p[4];
48 point += 3;
49
50 }
51 point += Stride - this.ImageData.Width * 3;
52 }
53 }
54 }
55
由于C#的指针操作属于unsafe操作,所以很多人对unsafe使用起来都很谨慎
其实所谓不安全代码,其实是不受控于CLR控制下的托管执行,相当于CLR领导下的部分区域自治,
当然CLR也不会去关心不安全代码的内存分配和回收
费话少说,先体验一下适当的指针操作带来的性能提升。
平时我们通过GDI+操作稍大点图片象素时,都会有一种濒临崩溃的感觉,我们转下用指针操作
1 Bitmap map = new Bitmap(path);
2 // 将图片位图区域进行锁定
3 BitmapData mapdata = map.LockBits(new Rectangle(0, 0, map.Width, map.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
4 unsafe
5 {
6 byte* pixpoint;
7 int newpoint;
8
9 for (int i = 0; i < map.Width; i++)
10 {
11 for (int j = 0; j < map.Height; j++)
12 {
13 // 位图结构存在一个字节对齐问题。
14 pixpoint = (byte*)mapdata.Scan0; +i * 3 + j * mapdata.Stride;
15 newpoint = (*pixpoint * 11 + *(pixpoint + 1) * 59 + *(pixpoint + 2) * 30) / 100;
16 *(pixpoint++) = (byte)newpoint;
17 *(pixpoint++) = (byte)newpoint;
18 *(pixpoint++) = (byte)newpoint;
19
20 // 非指针操作
21 // Color pxcl=map.GetPixel(i, j);
22 // map.SetPixel(i, j, (pxcl.R * 11 + pxcl.G * 59 + pxcl * 30) / 100);
23 }
24 }
25 }
26 // 将位图内存区域进行解锁
27 map.UnlockBits(mapdata);
28 map.Dispose();
比较GDI+使用的GetPixel/SetPixel,让你立马感到这个世界多么美妙
再来一个中值滤波的操作
1 /// <summary>
2 /// 中值滤波
3 /// </summary>
4 /// <param name="dgGrayValue"></param>
5 public void MedianFilter(int dgGrayValue)
6 {
7 // 100M 双线空间 + 50M数据库 28元/年
8 // 1G 双线空间 + 200M数据库 100元/年
9 // QQ:70975363
10 byte s;
11 byte[] p = new byte[9];
12 int i, j;
13 int x, y;
14 int Stride;
15 unsafe
16 {
17 byte* point = (byte*)this.ImageData.Scan0;
18 Stride = this.ImageData.Stride;
19 point = point + 3 + this.ImageData.Stride;
20 for (i = 0; i < this.ImageData.Height - 1; i++)
21 {
22 for (j = 0; j < this.ImageData.Width - 1; j++)
23 {
24 p[0] = *(point - Stride - 1);
25 p[1] = *(point - Stride + 2);
26 p[2] = *(point - Stride + 5);
27 p[3] = *(point - 1);
28 p[4] = *(point + 2);
29 p[5] = *(point + 5);
30 p[6] = *(point + Stride - 1);
31 p[7] = *(point + Stride + 2);
32 p[8] = *(point + Stride + 5);
33 for (x = 0; x < 5; x++)
34 {
35 for (y = x + 1; y < 9; y++)
36 {
37 if (p[x] > p[y])
38 {
39 s = p[x];
40 p[x] = p[y];
41 p[y] = s;
42 }
43 }
44 }
45 *point = p[4];
46 *(point - 1) = p[4];
47 *(point - 2) = p[4];
48 point += 3;
49
50 }
51 point += Stride - this.ImageData.Width * 3;
52 }
53 }
54 }
55
相关文章推荐
- c#指针进行图像操作
- c#指针进行图像操作
- C# 指针操作图像 二值化处理
- C# 指针操作图像 细化处理
- C# 中使用Word文档对图像进行操作
- C# 中使用Word文档对图像进行操作
- C# 对MongoDB 进行增删改查的简单操作实例
- C#动态生成WORD文档并进行操作。
- C#调用Geos的C函数接口进行空间拓扑操作
- C# 对数据进行异或操作
- iOS 使用手势UIGestureRecognizer对图像进行缩放、移动、旋转等操作
- 使用blur函数进行基于均值滤波的图像模糊操作
- C#使用自定义算法对数组进行反转操作的方法
- 快速对图像的像素进行操作 opencv 实战
- C#代码操作XML进行增、删、改操作
- 怎样在C#里对xml文件进行修改,新增,删除,插入操作?
- 使用VS(C#) 对Mysql 进行操作
- 使用C#和Excel进行报表开发(二)-操作统计图(Chart)
- 使用OpenCV读、操作、写图像并与bash合作对某个目录下所有图像进行类似处理
- IT民工系列——c#控制Excel的类库。对com操作进行了封装