当前位置:编程学习 > C#/ASP.NET >>

求助ArgumentException参数错误

我的源代码是
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Example024
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern bool BitBlt(
            IntPtr hdcDest,             //目标设备的句柄
            int nxDest,                 //目标对象的左上角的X坐标
            int nyDest,                 //目标对象的左上角的Y左边
            int nwidth,                 //目标对象的矩形宽度
            int nHeight,                //目标对象的矩形长度
            IntPtr hdcSrc,              //源设备的句柄
            int nXSrc,                  //源对象的左上角X坐标
            int nYSrc,                  //源对象的左上角Y坐标
            System.Int32 dwRop          //光栅的操作值
            );
        [System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        private static extern IntPtr CreateDC(
            string lpszDriver,          //驱动名称
            string lpszDevice,          //设备名称
            string lpszOutput,          //无用,可以设定为null
            IntPtr lpIntData            //任意的打印机数据
            );

        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            System.Threading.Thread.Sleep(1000);
            IntPtr dc1=CreateDC("DISPLAY",null,null,(IntPtr)null);
            System.Drawing.Graphics g1 = System.Drawing.Graphics.FromHdc(dc1);
            Bitmap MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
            Graphics g2 = Graphics.FromImage(MyImage);
            IntPtr dc2 = g2.GetHdc();
            BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 0xCC0020);
            g1.ReleaseHdc( dc1 );
            g2.ReleaseHdc( dc2 );
            MyImage.Save("d:\\C#\\hong.jpg",ImageFormat.Jpeg);
            MessageBox.Show("已经把当前屏幕保存到d:\\hong.jpg文件中!");
            this.Show();
        }
    }
}

运行后老是提示g1.ReleaseHdc( dc1 )有ArgumentException参数错误 --------------------编程问答-------------------- d1不能用这样释放,需要声明api,用ReleaseDC
例如

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, //目标设备的句柄
int nxDest, //目标对象的左上角的X坐标
int nyDest, //目标对象的左上角的Y左边
int nwidth, //目标对象的矩形宽度
int nHeight, //目标对象的矩形长度
IntPtr hdcSrc, //源设备的句柄
int nXSrc, //源对象的左上角X坐标
int nYSrc, //源对象的左上角Y坐标
System.Int32 dwRop //光栅的操作值
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern IntPtr CreateDC(
string lpszDriver, //驱动名称
string lpszDevice, //设备名称
string lpszOutput, //无用,可以设定为null
IntPtr lpIntData //任意的打印机数据
);
[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern int ReleaseDC(
    IntPtr hwnd,
    IntPtr hdc
);
private void button5_Click(object sender, EventArgs e)
{
    this.Hide();
    System.Threading.Thread.Sleep(1000);
    IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
    System.Drawing.Graphics g1 = System.Drawing.Graphics.FromHdc(dc1);
    Bitmap MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
    Graphics g2 = Graphics.FromImage(MyImage);
    IntPtr dc2 = g2.GetHdc();
    BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, dc1, 0, 0, 0xCC0020);
    ReleaseDC(IntPtr.Zero, dc1);
    g2.ReleaseHdc(dc2);
    MyImage.Save("d:\\C#\\hong.jpg", ImageFormat.Jpeg);
    MessageBox.Show("已经把当前屏幕保存到d:\\hong.jpg文件中!");
    this.Show();

}



关键的就是:
声明

[DllImport("user32.dll", EntryPoint = "ReleaseDC")]
public static extern int ReleaseDC(
    IntPtr hwnd,
    IntPtr hdc
);

替换:
g1.ReleaseHdc( dc1 );
换为:
ReleaseDC(IntPtr.Zero, dc1); --------------------编程问答-------------------- 参数异常
dc1是否为null
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,