使用c#建一个可以全屏截图的程序,可是问题很大,大虾们帮们看看问题在哪?

zzddl 2005-06-04 06:02:42
使用api函数来截图,但是保存的文件却是全黑的,这问题到底出在哪啊?
下边是核心的代码,问题肯定出在这里边:

int hdc=GetDC(0);
Graphics g=Graphics.FromHdc((IntPtr)hdc);
Bitmap bmp=new Bitmap(1024,768,g);
Graphics g2 = Graphics.FromImage(bmp);
IntPtr dc2 = g2.GetHdc();
BitBlt((IntPtr)dc2, 0, 0, 1024, 768, (IntPtr)hdc, 0, 0,13369376);
bmp.Save(@"c:\603.bmp");
...全文
229 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zzddl 2005-06-06
  • 打赏
  • 举报
回复
我这个也是win form程序,咱俩的程序好像就差在getdc和createdc上,而getdc我在其他语言中能够正常使用,为什么在c#就不行了?深层次原因是什么啊?
mmqingfeng 2005-06-05
  • 打赏
  • 举报
回复
全屏的hdc用这个试试

CreateDC("DISPLAY", null, null, IntPtr.Zero);
mba9001 2005-06-05
  • 打赏
  • 举报
回复
up
zzddl 2005-06-05
  • 打赏
  • 举报
回复
我希望截的是全屏的图啊,如果和控件关联了,全屏的图不就截不到了么? wangrhliuyh(TTL) 老兄的程序也是只能得到程序窗口里的图吧?
int hdc=GetDC(0);
这里边的getdc是api 函数,这个真邪门。
chenyuming2004 2005-06-05
  • 打赏
  • 举报
回复
会截出黑图我也遇到过,
上面的代码如果我放在web页面或windows服务
就会全黑。只有在win form里面才能正确截到。

后来我在windows服务里面勾上“允许服务与桌面交互”
就不会出黑图的问题,
估计你也是这个问题。
chenyuming2004 2005-06-05
  • 打赏
  • 举报
回复
[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern bool BitBlt (
IntPtr hdcDest , //目标设备的句柄
int nXDest , // 目标对象的左上角的X坐标
int nYDest , // 目标对象的左上角的X坐标
int nWidth , // 目标对象的矩形的宽度
int nHeight , // 目标对象的矩形的长度
IntPtr hdcSrc , // 源设备的句柄
int nXSrc , // 源对象的左上角的X坐标
int nYSrc , // 源对象的左上角的X坐标
System.Int32 dwRop // 光栅的操作值
) ;


[ System.Runtime.InteropServices.DllImportAttribute ( "gdi32.dll" ) ]
private static extern IntPtr CreateDC (
string lpszDriver , // 驱动名称
string lpszDevice , // 设备名称
string lpszOutput , // 无用,可以设定位"NULL"
IntPtr lpInitData // 任意的打印机数据
) ;

IntPtr dc1 = CreateDC ( "DISPLAY" , null , null , ( IntPtr ) null ) ;
Graphics g=Graphics.FromHdc(dc1);
Bitmap map=new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,g);
Graphics g1=Graphics.FromImage(map);
IntPtr dc3 = g.GetHdc() ;
IntPtr dc2 = g1.GetHdc() ;
BitBlt(dc2,0,0,System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,dc3,0,0,13369376);
g.ReleaseHdc(dc3);
g1.ReleaseHdc(dc2);

string dt=DateTime.Now.ToString();
g1.DrawString(DateTime.Now.ToString(),new Font("宋体",36),new SolidBrush(Color.Red),new Point(430,330));

try
{
map.Save(@"C:\Inetpub\wwwroot\dw\aaa.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch
{
}
finally
{
map.Dispose();
GC.Collect();
}

一个全屏截图代码,
测试完全可用。
Cry_Out 2005-06-04
  • 打赏
  • 举报
回复
mark
dreamsflying 2005-06-04
  • 打赏
  • 举报
回复
int hdc=GetDC(0);
问题在这里,要跟控件关联
wangrhliuyh 2005-06-04
  • 打赏
  • 举报
回复
我的函数是保存到一个byte[]中,再存入数据库,你去看看是否有帮助,我的是可以实现的

public static byte[] captureWinformImage(System.Windows.Forms.Control obj)
{
System.Drawing.Bitmap oB = new System.Drawing.Bitmap(obj.Width, obj.Height);
System.Drawing.Graphics gForm = obj.CreateGraphics();
IntPtr iHdcForm = gForm.GetHdc();
System.Drawing.Graphics gBitmap = System.Drawing.Graphics.FromImage(oB);
IntPtr iHdcBitmap = gBitmap.GetHdc();
int iResultBitBlt = BitBlt(iHdcBitmap,0,0,
obj.ClientRectangle.Width, obj.ClientRectangle.Height,
iHdcForm, 0,0,SRCCOPY);
gBitmap.ReleaseHdc(iHdcBitmap);
gForm.ReleaseHdc(iHdcForm);

gBitmap.Dispose();
gForm.Dispose();

System.IO.MemoryStream mStream = new System.IO.MemoryStream();
oB.Save(mStream, System.Drawing.Imaging.ImageFormat.Jpeg);
mStream.Flush();
mStream.Seek(0, System.IO.SeekOrigin.Begin);
byte[] bytes = new byte[(int)mStream.Length];
mStream.Read(bytes, 0, (int)mStream.Length);
return bytes;
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧