使用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");
下边是核心的代码,问题肯定出在这里边: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");
解决方案 »
- 求助!!C#中如何将DataGridView中某一列中的值求和,将得到的和显示到一个TextBox中?
- 一个IC卡基础问题,帮忙解答一下,谢谢
- 导出excel问题
- 请问 C# winform 中的单选是怎样做出来的 类似于html中的select
- 请问这个结构的返回字节数为什么是16啊?
- form action之后做某事
- c#中的javascript重定向问题
- 请教一个c#中的treeview的问题
- 大家帮帮忙,小弟有问题要问?
- 给点提示--用C#写一个公路巡警的系统。软硬件如何联接
- 不懂就问吧..我得到了一个System.Reflection.Assembly assembly对象
- word里面的宋体五号字对应Font font = new Font("宋体",x);x是几磅啊?
{
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;
}
问题在这里,要跟控件关联
int hdc=GetDC(0);
这里边的getdc是api 函数,这个真邪门。
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();
}一个全屏截图代码,
测试完全可用。
上面的代码如果我放在web页面或windows服务
就会全黑。只有在win form里面才能正确截到。后来我在windows服务里面勾上“允许服务与桌面交互”
就不会出黑图的问题,
估计你也是这个问题。