c#高效处理图片,核心!!! 继续改版中

 

ContractedBlock.gif ExpandedBlockStart.gif 代码
 
   
1 using System;
2   using System.IO;
3   using System.Windows.Forms;
4   using System.Drawing;
5
6
7
8   namespace AA
9 {
10
11 using WORD = UInt16;
12 using DWORD = UInt32;
13 using LONG = Int32;
14
15 /*
16 WORD为无符号16位整数,DWORD为无符号32位整数,LONG为32位整数
17 */
18 class BASE
19 {
20
21 /*
22 * 位图文件头(Bitmap File Header)
23 * 此块信息大小固定 为14个字节
24 */
25 public struct FileHeader
26 {
27 public WORD type; // 位图文件类型,必须是0x424D,即“BM”
28 public DWORD size; // 位图文件大小,包括头部的14个字节
29 public WORD reserved1; // 保留字1
30 public WORD reserved2; // 保留字2
31 public DWORD offBits; // 从文件头到实际的位图数据的偏移字节数
32
33
34 public FileHeader( byte [] bs, int i)
35 {
36 type = Helper.toWORD(bs[i ++ ], bs[i ++ ]);
37 size = Helper.toDWORD(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
38 reserved1 = Helper.toWORD(bs[i ++ ], bs[i ++ ]);
39 reserved2 = Helper.toWORD(bs[i ++ ], bs[i ++ ]);
40 offBits = Helper.toDWORD(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
41 }
42 }
43
44 // 第2部分为位图信息头,定长40字节
45 public struct InfoHeader
46 {
47 public DWORD size; // 本结构长度
48 public LONG width; // 位图宽度
49 public LONG height; // 位图高度
50 public WORD planes; // 目标设备级别,必须是1
51
52 public WORD bitCount; // 每个像素所占的位数(bit),其值必须为1(黑白图像)、4(16色图)、8(256色)、24(真彩色图),新的BMP格式支持32位色。
53 public DWORD compression; // 位图的压缩类型,有效值为BI_RGB(未经压缩)、BI_RLE8、BI_RLE4、BI_BITFILEDS(均为Windows定义常量)。
54 public DWORD sizeImage; // 实际的位图数据占用的字节数,该值的大小在第4部分位图数据中有具体解释。
55 public LONG xPelsPerMeter; // 指定目标设备的水平分辨率,单位是像素/米。
56
57 public LONG yPelsPerMeter; // 指定目标设备的垂直分辨率,单位是像素/米。
58 public DWORD clrUsed; // 位图实际用到的颜色数,如果该值为零,则用到的颜色数为2的biBitCount次幂。
59 public DWORD clrImportant; // 位图显示过程中重要的颜色数,如果该值为零,则认为所有的颜色都是重要的。
60
61
62 public InfoHeader( byte [] bs, int i)
63 {
64 size = Helper.toDWORD(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
65 width = Helper.toLONG(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
66 height = Helper.toLONG(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
67 planes = Helper.toWORD(bs[i ++ ], bs[i ++ ]);
68
69 bitCount = Helper.toWORD(bs[i ++ ], bs[i ++ ]);
70 compression = Helper.toDWORD(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
71 sizeImage = Helper.toDWORD(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
72 xPelsPerMeter = Helper.toLONG(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
73
74 yPelsPerMeter = Helper.toLONG(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
75 clrUsed = Helper.toDWORD(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
76 clrImportant = Helper.toDWORD(bs[i ++ ], bs[i ++ ], bs[i ++ ], bs[i ++ ]);
77 }
78
79 }
80
81 // 第三部分
82 public struct RGBQUAD
83 {
84 byte B; // 蓝色分量
85 byte G; // 绿色分量
86 byte R; // 红色分量
87 byte O; // 保留字,暂时不用
88 }
89
90
91 }
92
93 class BMP : BASE
94 {
95 FileHeader fileHeader; // 文件头
96 InfoHeader infoHeader; // 信息头
97 RGBQUAD[] GRBTable; // 颜色表
98
99 public BMP( string filename)
100 {
101 byte [] bs = File.ReadAllBytes(filename);
102
103 fileHeader = new FileHeader(bs, 0 ); // 初始化文件头
104 infoHeader = new InfoHeader(bs, 14 ); // 初始化信息头
105 GRBTable = null ; // 暂时不动
106
107 test(bs, Convert.ToInt32(fileHeader.offBits));
108 }
109
110
111 public void test( byte [] bs, int offset)
112 {
113 int w = Screen.PrimaryScreen.Bounds.Width;
114 int h = Screen.PrimaryScreen.Bounds.Height;
115
116
117 Bitmap bmp = new Bitmap(w, h);
118
119
120 for ( int y = 0 ; y < h - 2 ; y ++ )
121 {
122 for ( int x = 0 ; x < w; x ++ )
123 {
124 try
125 {
126 int i = bs[offset + 2 ];
127
128 Color c = Color.FromArgb(( int )(bs[offset + 2 ]), ( int )(bs[offset + 1 ]), ( int )(bs[offset + 0 ]));
129
130 bmp.SetPixel(x, h - 1 - y, c);
131 offset = offset + 4 ;
132 }
133 catch
134 {
135 break ;
136 }
137 }
138 }
139
140
141 bmp.Save( " haha.bmp " , System.Drawing.Imaging.ImageFormat.Bmp);
142 }
143
144 static void Main( string [] args)
145 {
146 Bitmap bmp = new Bitmap( 1366 , 768 );
147 Graphics g = Graphics.FromImage(bmp);
148 g.CopyFromScreen( 0 , 0 , 0 , 0 , bmp.Size);
149 bmp.Save( " 1.bmp " ,System.Drawing.Imaging.ImageFormat.Bmp);
150
151 BMP b = new BMP( " 1.bmp " );
152
153 }
154 }
155
156 public class Helper
157 {
158 public static WORD toWORD( byte b1, byte b2)
159 {
160
161 // return new string(new char[]{(char)b1, (char)b2});
162
163
164 return BitConverter.ToUInt16( new byte [] { b1, b2 }, 0 );
165 }
166 public static DWORD toDWORD( byte b1, byte b2, byte b3, byte b4)
167 {
168 // return new string(new char[] { (char)b1, (char)b2, (char)b3, (char)b4 });
169
170
171 return BitConverter.ToUInt32( new byte [] { b1, b2, b3, b4 }, 0 );
172 }
173
174 public static LONG toLONG( byte b1, byte b2, byte b3, byte b4)
175 {
176 return BitConverter.ToInt32( new byte [] { b1, b2, b3, b4 }, 0 );
177 }
178
179 }
180
181 }
182

 

 

 

转载于:https://www.cnblogs.com/igrl/archive/2010/03/23/1692588.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值