protected override void WndProc(ref Message m) { if (m.Msg == DX_DISPLAYREADY) { WindowsPixels wp = new WindowsPixels(); Marshal.PtrToStructure(m.WParam, wp); byte[] pixs = new byte[wp.width * wp.height * 3]; Marshal.Copy(wp.pixels, pixs, 0, wp.width * wp.height * 3); this.DrawPicture(wp, ref pixs, true); } base.WndProc(ref m); }
protected void DrawPicture(WindowsPixels wp, ref byte[] pixs, bool force) { //if (storedBmap != null && (force || storedBmap != ip || // pictureBox.Width != wp.width || pictureBox.Height != wp.height)) if ((force || pictureBox.Width != wp.width || pictureBox.Height != wp.height)) { //storedBmap = wp.pixels; this.ClientSize = new Size(wp.width, wp.height); //pictureBox.Width = wp.width; //pictureBox.Height = wp.height; Bitmap bmp; // bitmap is upside down--can it be fixed easily? bmp = new Bitmap(wp.width, wp.height, PixelFormat.Format24bppRgb); int i; BitmapData data = bmp.LockBits(new Rectangle(0, 0, bmp.Width, bmp.Height), ImageLockMode.WriteOnly, bmp.PixelFormat); if (data.Stride == wp.width * 3) { Marshal.Copy(pixs, 0, data.Scan0, wp.width * wp.height * 3); } else { for (i = 0; i < bmp.Height; i++) { IntPtr p = new IntPtr(data.Scan0.ToInt32() + data.Stride * i); Marshal.Copy(pixs, i * bmp.Width * 3, p, bmp.Width * 3); } } bmp.UnlockBits(data); //img = new Bitmap(pictureBox.Width, pictureBox.Height, // pictureBox.Width * 3, System.Drawing.Imaging.PixelFormat.Format24bppRgb, ip); bmp.RotateFlip(RotateFlipType.Rotate180FlipX); this.pictureBox.Image = bmp; pictureBox.Update(); Show(); BringToFront(); } }