自定的在.Net Compact Framework中进行截屏的函数,运行一段时间后,程序死了.

benjiamen benjiamen 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
最近由于项目需要写了一个在.Net Compact Framework中进行截屏的函数,主程序定时的去调用这个函数.函数调用可获得位图数据,只是每次程序运行一段时间后,程序就死了.多谢大家帮我看看这个函数,是不是那有问题,或用内存泄漏的地方.因为当中使用了非托管代码.

函数代码如下 .


/// <summary>
///
/// </summary>
/// <param name="hReal">窗口句柄</param>
/// <param name="rect">窗口坐标区域</param>
/// <returns></returns>
public static byte [] SnapshotToBit(IntPtr hReal, Rectangle rect)
{
byte[] data = null;
try
{
int nWidth = rect.Width;
int nHeight = rect.Height;

IntPtr hDCInk = IntPtr.Zero, hMemDC = IntPtr.Zero;
//通过窗口句柄获得窗口所属的DC句柄
hDCInk = GetDC(hReal);
//用CreateCompatibleDC建了一个Memory DC
hMemDC = CreateCompatibleDC(hDCInk);
try
{
BITMAPINFOHEADER bi = new BITMAPINFOHEADER();
bi.biSize = (uint)Marshal.SizeOf(bi);
//bi.biBitCount = 24; // Creating RGB bitmap. The following three members don't matter
bi.biBitCount = 16; // Creating RGB bitmap. The following three members don't matter
bi.biClrUsed = 0;
bi.biClrImportant = 0;
bi.biCompression = 0;
bi.biHeight = nHeight;
bi.biWidth = nWidth;
bi.biPlanes = 1;
int cb = (int)(bi.biHeight * bi.biWidth * bi.biBitCount / 8); //8 is bits per byte
bi.biSizeImage = (uint)cb;
bi.biXPelsPerMeter = 0xb12; // 72 ppi, 96 would work well too
bi.biYPelsPerMeter = 0xb12; // 72 ppi

IntPtr pBits = IntPtr.Zero;
//Allocate memory for bitmap bits
IntPtr pBI = LocalAlloc(GPTR, (int)bi.biSize);
// Not sure if this needed - simply trying to keep marshaller happy
Marshal.StructureToPtr(bi, pBI, false);
//This will return IntPtr to actual DIB bits in pBits
IntPtr hBmp = CreateDIBSection(hDCInk, pBI, 0, ref pBits, IntPtr.Zero, 0);
//Marshall back - now we have BITMAPINFOHEADER correctly filled in
Marshal.PtrToStructure(pBI, bi);
BITMAPINFOHEADER biNew = (BITMAPINFOHEADER)Marshal.PtrToStructure(pBI, typeof( BITMAPINFOHEADER ));
LocalFree(pBI);

//Usual stuff
IntPtr hOldBitmap = SelectObject(hMemDC, hBmp);
try
{

//Grab bitmap
int nRet = BitBlt(hMemDC, 0, 0, nWidth, nHeight, hDCInk, 0, 0, SRCCOPY);
// Allocate memory for a copy of bitmap bits
byte[] RealBits = new byte[cb];
// And grab bits from DIBSestion data
Marshal.Copy(pBits, RealBits, 0, cb);

// This simply creates valid bitmap file header, so it can be saved to disk
BITMAPFILEHEADER bfh = new BITMAPFILEHEADER();
bfh.bfSize = (uint)cb + 0x36; // Size of header + size of BITMAPINFOHEADER size of bitmap bits
bfh.bfType = 0x4d42; //BM
bfh.bfOffBits = 0x36; //
int HdrSize = 14;
byte[] header = new byte[HdrSize];
BitConverter.GetBytes(bfh.bfType).CopyTo(header, 0);
BitConverter.GetBytes(bfh.bfSize).CopyTo(header, 2);
BitConverter.GetBytes(bfh.bfOffBits).CopyTo(header, 10);

//Allocate enough memory for complete bitmap file
data = new byte[cb+0x36];
//BITMAPFILEHEADER
header.CopyTo(data, 0);

//BITMAPINFOHEADER
header = new byte[Marshal.SizeOf(bi)];
IntPtr pHeader = LocalAlloc(GPTR, Marshal.SizeOf(bi));
Marshal.StructureToPtr(biNew, pHeader, false);
Marshal.Copy(pHeader, header, 0, Marshal.SizeOf(bi));
LocalFree(pHeader);
header.CopyTo(data, HdrSize);

//Bitmap bits
RealBits.CopyTo(data, 0x36);
//data = null;
}
catch(Exception exc)
{

}
finally
{
DeleteObject(SelectObject(hMemDC, hOldBitmap));
LocalFree(pBI);
}

}
catch(Exception exc)
{

}
finally
{
if (hMemDC != IntPtr.Zero)
{
DeleteDC(hMemDC);
}
if(hDCInk != IntPtr.Zero)
{
ReleaseDC(hDCInk);
}
}
}
catch(Exception exc)
{

}
finally
{
}
return data;
}
...全文

相关推荐

SSM 技术在儿童福利院管理系统构建中的全面应用与架构优化策略探究

该系统精准聚焦福利院管理事务的核心痛点,集成了丰富多样的功能模块。首页提供便捷入口与整体展示;个人中心方便工作人员个性化设置;爱心人士管理、员工管理、后勤人员管理确保人员信息有序统筹;儿童信息管理涵盖福利院儿童全方位资料;院所风采展示福利院风貌与文化;活动管理、食谱管理丰富儿童生活;领养流程管理、领养申请管理、领养后续管理规范领养工作;政策法规管理提供政策依据;楼栋与宿舍管理、宿舍分配管理优化居住环境;义工申请管理、捐赠信息管理拓展外部支持资源;宿舍物品管理、物资管理、物资采购管理保障物资供应;报修申请与状态管理及时维护设施;在线留言促进沟通交流;系统管理则保障系统稳定运行。 系统基于 Java 的 SSM 框架构建,以 MySQL 数据库存储数据,借助 Django - Admin 系统进行高效管理,其强大功能与直观界面便于操作,有效降低了管理门槛。该系统不仅易用性佳,安全性也有充分保障,功能完备,全面满足儿童福利院信息管理需求,有力推动福利院管理的信息化、规范化进程,为儿童福利院的高效运转与儿童的健康成长奠定坚实基础,也为儿童福利事业的发展提供了有力的技术支撑与创新范例。

2401_85743969

C# 日期格式转换方法详解

本文介绍了在 C# 中进行日期格式转换的几种常见方法。你可以使用 DateTime.ToString() 方法将 DateTime 对象转换为指定格式的字符串,也可以使用自定义日期格式字符串来满足特定的需求。在上面的示例中,如果输入的日期字符串符合指定的格式,那么 DateTime.TryParse() 方法会将解析结果存储在 date 变量中,并返回 true。在上面的示例中,我们使用 DateTime.Now 获取当前日期和时间,并将其转换为 “yyyy-MM-dd HH:mm:ss” 格式的字符串。

AhTf__

基于SSM的邮件管理系统

基于Java的SSM框架技术,实现用户写信、收信、已发送、用户中心等功能

「已注销」

ssm订餐管理系统

这是一个网上订餐系统,有前台和后台,里面还有数据库,可以直接运行。

qq_35426551

简单的停车管理系统(SSM)

简单的javamaven项目,使用SSM框架,内有数据库脚本,

lw_zj_ywn
评论 6
收藏 已收藏
分享
返回顶部
benjiamen
C# 111,129 成员 642,538 内容