Exemplo n.º 1
0
        /// <summary>
        /// 利用 GetScreenData 截图,并体现在 pictureBox 上
        /// </summary>
        /// <param name="dm">大漠对象</param>
        /// <param name="x1">截图左上角X</param>
        /// <param name="y1">截图左上角Y</param>
        /// <param name="x2">截图右下角X</param>
        /// <param name="y2">截图右下角Y</param>
        /// <returns></returns>
        public static Bitmap GetBitMap(this DmPlugin dm, int x1, int y1, int x2, int y2)
        {
            int bmpdata = dm.GetScreenData(x1, y1, x2, y2);

            Logger.Info(bmpdata);
            int    width = x2 - x1;
            int    size  = (x2 - x1) * (y2 - y1);
            Bitmap bmp   = new Bitmap(x2 - x1, y2 - y1);
            Color  c;

            for (int i = 0; i < size; i++)
            {
                int bb = System.Runtime.InteropServices.Marshal.ReadByte((IntPtr)(bmpdata + i * 4));
                int gg = System.Runtime.InteropServices.Marshal.ReadByte((IntPtr)(bmpdata + i * 4 + 1));
                int rr = System.Runtime.InteropServices.Marshal.ReadByte((IntPtr)(bmpdata + i * 4 + 2));
                c = Color.FromArgb(rr, gg, bb);
                //备用方案 上述方案生成不成功选用下面的
                //int cc = System.Runtime.InteropServices.Marshal.ReadInt32((IntPtr)(bmpdata + i * 4));
                //c = Color.FromArgb(cc);
                bmp.SetPixel(i % width, i / width, c);
            }

            // System.Runtime.InteropServices.Marshal.FreeHGlobal((IntPtr)bmpdata);
            return(bmp);
        }