当前位置: 首页 > wzjs >正文

公司做网站需要注意什么事情网站设计与网站建设书店

公司做网站需要注意什么事情,网站设计与网站建设书店,湖南人事考试网,斐讯k2做网站1. 核心功能与技术栈 该截图功能类 ScreenShotClass 基于 Win32 API 实现了两种截图方式: CopyFromScreen 方法:利用 Graphics.CopyFromScreen 直接截取屏幕区域。BitBlt 方法:通过 GDI 的位图块传输(BitBlt)实现窗口…
1. 核心功能与技术栈

该截图功能类 ScreenShotClass 基于 Win32 API 实现了两种截图方式:

  • CopyFromScreen 方法:利用 Graphics.CopyFromScreen 直接截取屏幕区域。
  • BitBlt 方法:通过 GDI+ 的位图块传输(BitBlt)实现窗口截图。

核心依赖的 Win32 API 包括:

  • user32.dll:获取窗口句柄、窗口矩形区域。
  • dwmapi.dll:获取窗口扩展边界(适用于现代 Windows 窗口阴影等效果)。
  • gdi32.dll:执行位图复制操作(BitBlt)。

DwmGetWindowAttribute与 GetWindowRect区别
GetWindowRect 返回窗口边框矩形(不含阴影等视觉扩展),而 DwmGetWindowAttribute 能获取实际显示区域,确保截图完整。 

2. 两种截图方式对比
方法实现原理
CopyFromScreen使用 Graphics.CopyFromScreen 直接从屏幕坐标复制像素到目标位图。
BitBlt通过 GetWindowDC 获取窗口 DC,再用 BitBlt 复制像素到目标 DC(位图)。

适用场景与限制

  • 适用场景
    • 截取当前活动窗口或指定窗口内容。
    • 需要包含窗口边框、阴影等视觉元素的精确截图。
  • 限制
    • 性能影响:频繁调用 BitBlt 可能影响 UI 线程,建议异步执行。
    • 兼容性:仅适用于 Windows 系统,依赖 Win32 API。
3. 使用示例 
// 截取前台窗口(使用 CopyFromScreen)
try {Image screenshot = ScreenShotClass.Screenshot_CopyFromScreen();screenshot.Save("screenshot.png", System.Drawing.Imaging.ImageFormat.Png);
} catch (Exception ex) {Console.WriteLine($"截图失败:{ex.Message}");
}// 截取指定窗口(使用 BitBlt)
IntPtr targetHandle = ...; // 获取目标窗口句柄(如通过 FindWindow)
Image screenshot = ScreenShotClass.Screenshot_CopyFromDC(targetHandle);
完整代码:
using System;
using System.Drawing;
using System.Runtime.InteropServices;namespace CSharp学习之截图功能
{public static class Win32Api{// 获取前台窗口句柄[DllImport("user32.dll")]public static extern IntPtr GetForegroundWindow();// 获取窗口属性(这里用于获取扩展框架边界)[DllImport("dwmapi.dll")]public static extern int DwmGetWindowAttribute(IntPtr hwnd, int dwAttribute, out RECT pvAttribute, int cbAttribute);// 获取窗口的矩形区域(包括边框、标题栏等)[DllImport("user32.dll")]public static extern bool GetWindowRect(IntPtr hWnd, out RECT lpRect);// 获取指定窗口的设备上下文(DC)[DllImport("user32.dll")]public static extern IntPtr GetWindowDC(IntPtr hWnd);// 释放设备上下文(DC)[DllImport("user32.dll")]public static extern bool ReleaseDC(IntPtr hWnd, IntPtr hDC);// 执行位块传输(BitBlt)操作,用于复制图像[DllImport("gdi32.dll")]public static extern bool BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);// 定义RECT结构体,用于表示矩形区域[StructLayout(LayoutKind.Sequential)]public struct RECT{public int Left;public int Top;public int Right;public int Bottom;// 计算矩形的宽度public int Width => Right - Left;// 计算矩形的高度public int Height => Bottom - Top;}// DWMWA_EXTENDED_FRAME_BOUNDS常量public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;// SRCCOPY表示源图像直接复制到目标设备上下文public const int SRCCOPY = 0x00CC0020;}public static class ScreenShotClass{public static Image Screenshot_CopyFromScreen(){IntPtr handle = Win32Api.GetForegroundWindow();Win32Api.RECT rect;int result = Win32Api.DwmGetWindowAttribute(handle, Win32Api.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, Marshal.SizeOf(typeof(Win32Api.RECT)));if (result != 0){throw new InvalidOperationException("无法获取窗口边界");}Bitmap bmp = new Bitmap(rect.Width, rect.Height);try{using (Graphics g = Graphics.FromImage(bmp)){g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size);}return bmp;}catch(Exception ex) {// 如果发生异常,确保释放Bitmapbmp?.Dispose();throw new InvalidOperationException($"截图失败: {ex.Message}");}}public static Image Screenshot_CopyFromScreen(IntPtr handle){Win32Api.RECT rect;int result = Win32Api.DwmGetWindowAttribute(handle, Win32Api.DWMWA_EXTENDED_FRAME_BOUNDS, out rect, Marshal.SizeOf(typeof(Win32Api.RECT)));if (result != 0){throw new InvalidOperationException("无法获取窗口边界");}Bitmap bmp = new Bitmap(rect.Width, rect.Height);try{using (Graphics g = Graphics.FromImage(bmp)){g.CopyFromScreen(rect.Left, rect.Top, 0, 0, bmp.Size);}return bmp;}catch (Exception ex){// 如果发生异常,确保释放Bitmapbmp?.Dispose();throw new InvalidOperationException($"截图失败: {ex.Message}");}}public static Image Screenshot_CopyFromDC(){IntPtr handle = Win32Api.GetForegroundWindow();Win32Api.RECT rect;Win32Api.GetWindowRect(handle, out rect);Bitmap bmp = new Bitmap(rect.Width, rect.Height);Graphics g = null;IntPtr hdcSrc = IntPtr.Zero;IntPtr hdcDest = IntPtr.Zero;try{g = Graphics.FromImage(bmp);hdcDest = g.GetHdc();hdcSrc = Win32Api.GetWindowDC(handle);if (hdcSrc == IntPtr.Zero){throw new InvalidOperationException("无法获取窗口设备上下文");}bool success = Win32Api.BitBlt(hdcDest, 0, 0, rect.Width, rect.Height, hdcSrc, 0, 0, Win32Api.SRCCOPY);if (!success){throw new InvalidOperationException("BitBlt 调用失败");}return bmp;}catch (Exception ex){// 释放资源bmp?.Dispose();throw new InvalidOperationException($"截图失败: {ex.Message}");}finally{// 确保释放资源if (hdcDest != IntPtr.Zero && g != null){g.ReleaseHdc(hdcDest);}if (hdcSrc != IntPtr.Zero){Win32Api.ReleaseDC(handle, hdcSrc);}g?.Dispose();}}public static Image Screenshot_CopyFromDC(IntPtr handle){Win32Api.RECT rect;Win32Api.GetWindowRect(handle, out rect);Bitmap bmp = new Bitmap(rect.Width, rect.Height);Graphics g = null;IntPtr hdcSrc = IntPtr.Zero;IntPtr hdcDest = IntPtr.Zero;try{g = Graphics.FromImage(bmp);hdcDest = g.GetHdc();hdcSrc = Win32Api.GetWindowDC(handle);if (hdcSrc == IntPtr.Zero){throw new InvalidOperationException("无法获取窗口设备上下文");}bool success = Win32Api.BitBlt(hdcDest, 0, 0, rect.Width, rect.Height, hdcSrc, 0, 0, Win32Api.SRCCOPY);if (!success){throw new InvalidOperationException("BitBlt 调用失败");}return bmp;}catch (Exception ex){// 释放资源bmp?.Dispose();throw new InvalidOperationException($"截图失败: {ex.Message}");}finally{// 确保释放资源if (hdcDest != IntPtr.Zero && g != null){g.ReleaseHdc(hdcDest);}if (hdcSrc != IntPtr.Zero){Win32Api.ReleaseDC(handle, hdcSrc);}g?.Dispose();}}}
}


文章转载自:

http://UuoqfxaU.mnyzz.cn
http://01Pl97j7.mnyzz.cn
http://X87MvrPQ.mnyzz.cn
http://xz0oYfIX.mnyzz.cn
http://tVUfRoeJ.mnyzz.cn
http://tnpNErT2.mnyzz.cn
http://zWfjrKXJ.mnyzz.cn
http://o6W0SDoC.mnyzz.cn
http://MKDezcoi.mnyzz.cn
http://ELZcx5aw.mnyzz.cn
http://5jMaMBAM.mnyzz.cn
http://KEna9SpR.mnyzz.cn
http://96bFr1YC.mnyzz.cn
http://FYFaBF6n.mnyzz.cn
http://uAwOzRaT.mnyzz.cn
http://pVRgSmi3.mnyzz.cn
http://6ROU0Wlx.mnyzz.cn
http://8jP3VukI.mnyzz.cn
http://wsyzalPT.mnyzz.cn
http://S7xmO4LM.mnyzz.cn
http://ohoGVqxW.mnyzz.cn
http://yZtJ0f81.mnyzz.cn
http://xO1Tc7dl.mnyzz.cn
http://yiIs1hcJ.mnyzz.cn
http://uAD7N711.mnyzz.cn
http://legJIdSP.mnyzz.cn
http://xQhtQDa0.mnyzz.cn
http://pVHE5e2R.mnyzz.cn
http://FSm5IX7h.mnyzz.cn
http://v1ABm8vS.mnyzz.cn
http://www.dtcms.com/wzjs/764547.html

相关文章:

  • 义乌网站建设zisou8php做的网站打包成exe
  • 门户网站优化怎么做系统开发是什么意思
  • 网站建设图片手机中国建设银行官网站下载中心
  • 动态和静态网站的区别怎样在文章后做网站链接
  • 网站开发去哪里找昆明网站运营
  • html学校网站模板进入公众号核酸检测
  • 电动车网站模板个人网站的版权怎么写
  • 如何开发wap网站公司网站申请
  • 众筹那些网站可以做合同管理软件
  • 安徽省新天源建设公司网站南京网站建设小程序开发 雷仁网络
  • 个人做旅游网站怎样冷门且好听的公司名字
  • 想建个网站网页定制哪家不错
  • 湖北网站建设搭建企业所得税5%的标准
  • 做淘推广的网站做外贸如何建网站
  • 用局域网建设网站企业网站推广哪个公司好
  • 网站开发的工作职责复刻手表网站
  • 上海网站设计公司企业网站seo优帮云
  • dede网站地图模板上海专业制作网站
  • 怎么做一网站首页wordpress 全文检索
  • 网站开发 q3687474网店卖什么最赚钱
  • 建设网站公司哪里好提供网站建设报价
  • 小说网站开发多少钱今天深圳新增确诊最新消息
  • 谷歌网站推广方案可以看辽宁经济频道的app
  • 判断网站开发语言广东建筑信息平台
  • 山东做网站公司阿里云 备案 网站服务内容
  • 微信网站开发公司电话微信如何绑定网站
  • app应用网站单页模板下载视觉比较好看的网站
  • 好看的手机网站模板免费下载呼伦贝尔网站建设公司
  • 访问不了网站目录中的网页做网站的网站源码
  • 哪家公司做移动网站增城企业网站建设