最近做了一个获取位图(24位位图)数据的程序,代码如下:
Bitmap bmp
=
new
Bitmap(BmpFile);
// 获取位图的数据
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits( new Rectangle( 0 , 0 , bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// 获得位图数据的初始地址
IntPtr ptr = bmpData.Scan0;
// 位图数据
int bytes = bmp.Width * bmp.Height * 3 ;
byte [] bytData = new byte [bytes];
// 拷贝位图数据到字节数组
System.Runtime.InteropServices.Marshal.Copy(ptr, bytData, 0 , bytes);
// 数据处理
// ... ...
// 拷贝字节数组数据到位图
System.Runtime.InteropServices.Marshal.Copy(bytData, 0 , ptr, bytes);
// 更新图片数据
bmp.UnlockBits(bmpData);
// 获取位图的数据
System.Drawing.Imaging.BitmapData bmpData = bmp.LockBits( new Rectangle( 0 , 0 , bmp.Width, bmp.Height), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
// 获得位图数据的初始地址
IntPtr ptr = bmpData.Scan0;
// 位图数据
int bytes = bmp.Width * bmp.Height * 3 ;
byte [] bytData = new byte [bytes];
// 拷贝位图数据到字节数组
System.Runtime.InteropServices.Marshal.Copy(ptr, bytData, 0 , bytes);
// 数据处理
// ... ...
// 拷贝字节数组数据到位图
System.Runtime.InteropServices.Marshal.Copy(bytData, 0 , ptr, bytes);
// 更新图片数据
bmp.UnlockBits(bmpData);
该程序用VS2005+.net精简框架集开发,在PC机Windows平台上运行该程序获取图片数据正常,把同样的程序拷贝到WinCE 4.2(含.net 精简框架集2.0)运行,虽然也获取到位图数据,但数据与上位机的有所不同,RGB颜色被调整,如9会变成8之类。我以为是系统设置为16位色的缘故,我在PC机设为16位色,但取数据正常。
最后我只能用常规读写文件的方法,直接从文件中获取数据,才能保证上下位机系统同时运行正常。