您的位置:威尼斯官方网站 > 威尼斯正规官网 > 这段时间在前后相继中做一动态截屏成效的小程

这段时间在前后相继中做一动态截屏成效的小程

发布时间:2020-01-04 13:44编辑:威尼斯正规官网浏览(100)

    这么些小说依然有部分得以借鉴的地点


    近日在程序中做一动态截屏成效的小程序;
    在成就经过中,遭逢了许多标题,
    现将里面最首要的进度记录如下:

    1,要贯彻动态截屏的准绳
    刚伊始并不知,以为能够赢得桌面包车型客车句柄直接调用左右鼠标点击拿到.
    哪有那么如想像中的捷报
    看了不菲外人的程序.,极度是QQ的动态截屏作用,
    她们在截屏的时候桌面都以静态的.
    本来那个时候的背景是一个最大化的FORM,把其标题栏,开关等都收回了.
    像做桌面敬爱程序这样的风格.
    在此个FORM中赢得它的三个坐标是相当的轻易的事;
    约等于说:先截全屏到叁个FORM中显示
    在这里个FORM中截你所想要的风度翩翩有的并在首先个FORM中显得所截部分;
    如此那般就涌出了要在四个里面传值;
    先前时代为了幸免传值(呵呵,此时不会传值State of Qatar,作者先把所截部分保存一张图纸,
    在首先个FORM中检验是娄底有图片存在,有则显得出来
    成效是贯彻了,但有了新主题材料?只好截壹遍!
    缘由是,第二要截屏则要删减保存的图片,为第二遍截屏筹划,但十三分彰显
    图形财富被调用,不能删除.也是干什么第三回截图不能够保留的原因.
    mypicturebox.Image=null;那样的操作也特别的.(有哪个人知道的什么样消释调用财富的告诉自身须臾间State of Qatar
    看来那几个笨办法不专业了.
    在互连网找到三种FORM间传值的方法.
    一]新建类过渡传值.
    二]传送第多个FORM的地址.

    2切实可行落到实处
    了解了落到实处原理,就轻易完结其功效了~*~
    本人先用第4个情势完毕!
    (1卡塔尔先在率先个FORM中用风流倜傥开关运营截屏程序
                this.Hide(卡塔尔;//隐蔽主对话框.
                Thread.Sleep(150卡塔尔(قطر‎;//结束一下
               
                AllBitmap = Getallscreen(卡塔尔国;//调用动态截屏
               
                CaptureScreen CaptureS = new CaptureScreen(AllBitmap,this卡塔尔国;// 传递全屏BITMAP和地址
                CaptureS.ShowDialog();
                this.Show();
    内部调用了截全屏的函数Getallscreen(卡塔尔(قطر‎
            private Bitmap Getallscreen()
            {
                //创立荧屏Graphics
                Graphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);
                //依据显示器尺寸组建位图
                Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, grpScreen);
                //构建位图相关Graphics
                Graphics grpBitmap = Graphics.FromImage(bitmap);
                //创设显示器上下文
                IntPtr hdcScreen = grpScreen.GetHdc();
                //建构位图上下文
                IntPtr hdcBitmap = grpBitmap.GetHdc();
                //将显示屏捕获保存在图位中
                BitBlt(hdcBitmap, 0, 0, bitmap.Width, bitmap.Height, hdcScreen, 0, 0, 0x00CC0020);
                //关闭位图句柄
                grpBitmap.ReleaseHdc(hdcBitmap);
                //关闭荧屏句柄
                grpScreen.ReleaseHdc(hdcScreen);
                //释放位图对像
                grpBitmap.Dispose();
                //释放显示屏对像
                grpScreen.Dispose();
                //再次回到捕获位图
                return bitmap;
            }
    在截全屏函数中用到了一个API函数.
    则开展如下操作
    using System.Runtime.InteropServices;

             [DllImportAttribute("gdi32.dll")]
             public 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 // 光栅的操作值
             );

    (2卡塔尔到了最关键的地点,要想把首个FORM中的数据传递过来就得在首先个FORM中的相应的显示管理程序
    本人用下列函数
            public void SetMyImage(Bitmap mybitmap)//注意是public类型
            {
                this.mypictureBox.Image = mybitmap;//显示
                myimage = mybitmap;                //用于重画
                PreBtn.Enabled = true;             //用于保存图片
            }
    先是个FORM中用到部分变量
            public Image myimage=null;
            public Bitmap AllBitmap;
    (3卡塔尔(قطر‎在首先个FORM中调用了第叁个FORM,当中结构函数字传送递了全屏的BITMAP,
    进而第一个FORM中增多三个结构函数:
            public CaptureScreen(Bitmap bit,Form parentForm)
            {
                InitializeComponent();
                this.allbitmap = bit;
                this._parentForm = parentForm;
            }
    传送了图片消息在FORM_LOAD中加载呈现
            private void CaptureScreen_Load(object sender, EventArgs e)
            {
                this.TopMost = true;//让其最前展现
                this.allpicture.Image = allbitmap;
          gg = this.allpicture.CreateGraphics(卡塔尔(قطر‎;//创造三个Graphics为后边创设提醒消息计划
            }
    (4卡塔尔要截部分显示屏,当然要鲜明一下范围.用左右鼠标键来明确.
    用到上边八个函数
            private void allpicture_MouseDown(object sender, MouseEventArgs e)
            {
                if (isdoubleclick == true)
                {
                    point1.X = e.X;
                    point1.Y = e.Y;
                    isdraw = true;
                    gg.DrawString("按Esc键退出截图程序程序", new Font("Tahoma", 13, FontStyle.Underline卡塔尔国,
                                      new SolidBrush(Color.Blue), e.X,e.Y);
                }
            }

            private void allpicture_MouseMove(object sender, MouseEventArgs e)
            {
                this.allpicture.Refresh();
                if (isdraw == true)
                {
                    int hh,ww;         //总结截图地方和开首点地方
                    Point startpoint=new Point(0,0);
                    if (e.X < point1.X && e.Y < point1.Y)
                    {
                        startpoint.X = e.X;
                        startpoint.Y = e.Y;
                        hh = (int)(point1.X - e.X);
                        ww = (int)(point1.Y - e.Y);
                    }
                    if (e.X > point1.X && e.Y < point1.Y)
                    {
                        startpoint.X = point1.X;
                        startpoint.Y = e.Y;
                        hh = (int)(e.X - point1.X);
                        ww = (int)(point1.Y - e.Y);
                    }
                    if (e.X < point1.X && e.Y > point1.Y)
                    {
                        startpoint.X = e.X;
                        startpoint.Y = point1.Y;
                        hh = (int)(point1.X - e.X);
                        ww = (int)(e.Y - point1.Y);
                    }
                    else
                    {
                        startpoint = point1;
                        hh = (int)(e.X - point1.X);
                        ww = (int)(e.Y - point1.Y);
                    }
                    gg.DrawRectangle(new Pen(Color.FromArgb(9, 247, 32)), startpoint.X,
                                                startpoint.Y, hh, ww卡塔尔(قطر‎;//截图范围含蓄表示图
                   
                    int hhh, www;//显示提示消息地点
                    if ((int)(e.X - point1.X) > 0)
                        hhh = (int)(e.X - point1.X);
                    else
                        hhh = (int)(point1.X - e.X);
                    if ((int)(e.Y - point1.Y) > 0)
                        www = (int)(e.Y - point1.Y);
                    else
                        www = (int)(point1.Y - e.Y);
                    string mystr = "截取范围:n    宽:" + hhh + "n    高:" + www;
                    gg.DrawString(mystr, new Font("Tahoma", 10, FontStyle.Underline),
                                         new SolidBrush(Color.Red), e.X,e.Y);//h1, w1);
                    gg.DrawString("按Esc键退出截图程序程序", new Font("Tahoma", 13, FontStyle.UnderlineState of Qatar,
                                                             new SolidBrush(Color.Blue), point1.X, point1.Y-22);         
                }
            }

            private void allpicture_MouseUp(object sender, MouseEventArgs e)
            {
                if (isdoubleclick == true)
                {
                    point2.X = e.X;
                    point2.Y = e.Y;
                    isdoubleclick = false;
                }
            }

    其次个FORM中用到的变量:
            private Point  point1 = new Point(卡塔尔;   //开端地方
            private Point  point2 = new Point(卡塔尔;   //最后地方
            private bool   isdraw = false;         //是还是不是伊始画直线
            private bool   isdoubleclick = true;    //是或不是足以双击
            public  Bitmap newbitmap;               //截的豆蔻梢头对图像
            private Bitmap allbitmap;              //全屏图像
            private Form   _parentForm = null;
            private Graphics gg;

            public static int h1;//展现提醒地方
            public static int w1;
    截屏用到的API函数:
            #region 导入函数
           
            [DllImport("gdi32.dll")]
            private static extern IntPtr CreateDC(string lpszDriver, string lpszDrivse, string lpszOutput, Int32 lpInitData);

            [DllImport("gdi32.dll")]
            private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

            [DllImport("gdi32.dll")]
            private static extern int GetDeviceCaps(IntPtr hdc, Int32 nIndex);

            [DllImport("gdi32.dll")]
            private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

            [DllImport("gdi32.dll")]
            private static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

            [DllImport("gdi32.dll")]
            private static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest,
                     int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, UInt32 dwRop);

            [DllImport("gdi32.dll")]
            private static extern int DeleteDC(IntPtr hdc);

            #endregion
    (5State of Qatar今后能够调用截取部分显示器的函数  
            private void allpicture_DoubleClick(object sender, EventArgs e)
            {
                this.allpicture.Refresh(State of Qatar;//先擦出提醒新闻,食物店提示音讯哪个地方都足以来得;
                newbitmap = GetPartScreen(point1, point2);
                ((MyTool)_parentForm卡塔尔(قطر‎.SetMyImage(newbitmap卡塔尔(قطر‎;//通过传地址调用第二个FORM的图片体现函数
                this.Close(卡塔尔(قطر‎;//该窗口关闭 // Application.Exit(卡塔尔;应用程序退出
            }
    上面是GetPartScreen函数的切切实实落实 private static Bitmap GetPartScreen(Point p, Point pp卡塔尔国
            {
                IntPtr hscrdc, hmemdc;
                IntPtr hbitmap, holdbitmap;
                int nx, ny, nxx, nyy;
                nx = nxx = ny = nyy = 0;
                int nwidth, nheight;
                int xscrn, yscrn;

                hscrdc = CreateDC("DISPLAY", null, null, 0);//(IntPtr)null);// 0);//创建DC句柄
                hmemdc = CreateCompatibleDC(hscrdc卡塔尔(قطر‎;//创制三个内部存储器DC
                xscrn = GetDeviceCaps(hscrdc, 8);//*HORZRES*/卡塔尔(قطر‎;//获取显示器宽度//wingdi.h
                yscrn = GetDeviceCaps(hscrdc, 10);//*VERTRES*/卡塔尔(قطر‎;//获取显示屏中度//wingdi.h

                nx = p.X;
                ny = p.Y;
                nxx = pp.X;
                nyy = pp.Y;    

                if (nx < 0State of Qatar      //检查数值合法性
                    nx = 0;
                if (ny < 0)
                    ny = 0;
                if (nxx > xscrn)
                    nxx = xscrn;
                if (nyy > yscrn)
                    nyy = yscrn;

                if (nxx - nx > 0卡塔尔(قطر‎//截取范围的肥瘦
                {
                    nwidth = nxx - nx;
                }
                else
                {
                    nwidth = nx - nxx;
                }
                if (nyy - ny > 0State of Qatar//截取范围的惊人
                {
                    nheight = nyy - ny;
                }
                else
                {
                    nheight = ny - nyy;
                }

                if (nxx < nx && nyy < ny)
                {
                    h1 = nx;
                    w1 = ny;

                    int k;
                    k = nxx;
                    nxx = nx;
                    nx = k;

                    k = nyy;
                    nyy = ny;
                    ny = k;
                }

                if (nxx < nx && nyy > ny)
                {
                    h1 = nx;
                    w1 = nyy;

                    nx = nxx;
                }
                if (nx < nxx && ny > nyy)
                {
                    h1=nx;
                    w1 = nyy;

                    ny = nyy;
                }

                hbitmap = CreateCompatibleBitmap(hscrdc, nwidth, nheight);//从内存DC复制到hbitmap句柄
                holdbitmap = SelectObject(hmemdc, hbitmap);
                BitBlt(hmemdc, 0, 0, nwidth, nheight, hscrdc, nx, ny, (UInt32)0xcc0020);
                hbitmap = SelectObject(hmemdc, holdbitmap);
                DeleteDC(hscrdc卡塔尔(قطر‎;//删除用过的靶子
                DeleteDC(hmemdcState of Qatar;//删除用过的对象
                return Bitmap.FromHbitmap(hbitmap);//用Bitmap.FromHbitmap从hbitmap返回Bitmap
            }
    (6)大家传递了地址.怎么贯彻的传地址呢.委托!(具国家体育运动委员会托知识请查教程委托部分!)
    在第叁个FORM中申请委托:
    public delegate void mydelegate(Bitmap mybitmap);//类外

    漫天效应完毕,可你会发觉传递的图纸并没有出示在首先个FORM中的图片控件中
    干什么呢,小编然而花了七四个小时才知,因为当时自个儿晕了
    原先大家在截图时隐藏了FORM1,又随着展现,再关闭了FORM2,使展现的图样并未更新,
    所以今后你驾驭该怎么办了吧.
    重载OnPaint函数:
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                if (myimage != null)
                    this.mypictureBox.Image = myimage;
                else
                {
                    this.mypictureBox.Image = null;
                    this.mypictureBox.Refresh();
                }
            }
    (7卡塔尔(قطر‎旗开马到,不知你是还是不是!
    (8卡塔尔国现在简短用过渡类完成一下传值:
    自己只交给差别等的风流倜傥对,有不精通的能够联系自身二头研商;
    第一个FORM截屏时
                PassBitmap newpassbitmap = new PassBitmap();//注意这.
                newpassbitmap.PassBitmapEvent += new SendBitmap(this.SetMyImage);
               
                this.Hide();
                Thread.Sleep(150);
               
                AllBitmap = Getallscreen(State of Qatar;//调用动态截屏
               
                CaptureScreen CaptureS = new CaptureScreen(AllBitmap, newpassbitmap);
                CaptureS.ShowDialog();
                this.Show();
    第三个FORM的布局函数
            public CaptureScreen(Bitmap bit,PassBitmap bitmapss)
            {
                InitializeComponent();
                this.allbitmap = bit;
                this.bitmapss=bitmapss;
            }
    双击截屏时:
                newbitmap = GetPartScreen(point1, point2);
                bitmapss.PassGetBitmap(newbitmap);

    中间过渡类:
    using System;
    using System.Drawing;

    namespace BoberTool
    {
        public delegate void SendBitmap(Bitmap mybitmap);

        public class PassBitmap
        {
            public Bitmap mybitmaps;
            public event SendBitmap PassBitmapEvent;

            public void PassGetBitmap(Bitmap bitmaps)
            {
                if (bitmaps != null)
                    PassBitmapEvent(bitmaps);
            }
        }
    }

    基本上完结了职能!
    由于自家是初读书人.在这之中分明存在好多小标题,接待我们和自个儿一起斟酌!
    QQ47145481
    E-MAIL:bob_er@sina.com

    2006.10.28

     

    <> <>

    本文由威尼斯官方网站发布于威尼斯正规官网,转载请注明出处:这段时间在前后相继中做一动态截屏成效的小程

    关键词: