private Bitmap GetScreenShot(int width, int height) { var cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle); System.Drawing.Point cPoint = System.Windows.Forms.Cursor.Position; System.Drawing.Point hotSpot = cursor.HotSpot; System.Drawing.Point position = new System.Drawing.Point((cPoint.X - hotSpot.X), (cPoint.Y - hotSpot.Y)); var resizedBmp = new Bitmap(width, height); using (var bmp = new Bitmap((int)SystemParameters.PrimaryScreenWidth, (int)SystemParameters.PrimaryScreenHeight)) using (Graphics g = Graphics.FromImage(bmp)) using (Graphics resizedG = Graphics.FromImage(resizedBmp)) { // スクリーンショットを撮る g.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), bmp.Size); cursor.Draw(g, new Rectangle(position, cursor.Size)); // 動画サイズを減らすためリサイズする resizedG.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear; resizedG.DrawImage(bmp, 0, 0, width, height); } return(resizedBmp); }
private Bitmap CaptureScreen() { var cursor = new System.Windows.Forms.Cursor(System.Windows.Forms.Cursor.Current.Handle); cursor = System.Windows.Forms.Cursors.Arrow; System.Drawing.Point cPoint = System.Windows.Forms.Cursor.Position; System.Drawing.Point hotSpot = cursor.HotSpot; System.Drawing.Point position = new System.Drawing.Point((cPoint.X - hotSpot.X), (cPoint.Y - hotSpot.Y)); //プライマリモニタのデバイスコンテキストを取得 IntPtr disDC = GetDC(IntPtr.Zero); //Bitmapの作成 Bitmap bmp = new Bitmap( System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height); //Graphicsの作成 Graphics g = Graphics.FromImage(bmp); //Graphicsのデバイスコンテキストを取得 IntPtr hDC = g.GetHdc(); //Bitmapに画像をコピーする BitBlt(hDC, 0, 0, bmp.Width, bmp.Height, disDC, 0, 0, SRCCOPY); //解放 g.ReleaseHdc(hDC); //カーソルを追加 cursor.Draw(g, new Rectangle(position, cursor.Size)); g.Dispose(); ReleaseDC(IntPtr.Zero, disDC); return(bmp); }
/// <summary> /// 桌面输出回调处理 /// </summary> private void VideoNewFrameHandle(object sender, NewFrameEventArgs e) { if (IsRecording && !IsParsing) { TimeSpan time = DateTime.Now - beginTime - parseSpan; if (SettingHelp.Settings.捕获鼠标) { CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); try { System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor);//在每一帧上手动绘制鼠标 cur.Draw(Graphics.FromImage(e.Frame), new Rectangle(System.Windows.Forms.Cursor.Position.X - 10 - CurrentScreen.Bounds.X, System.Windows.Forms.Cursor.Position.Y - 10 - CurrentScreen.Bounds.Y, cur.Size.Width, cur.Size.Height)); } catch { }//打开任务管理器时会导致异常 } VideoWriter.WriteVideoFrame(e.Frame, time);//处理视频时长和实际物理时长不符,用帧间隔时长的办法指定每一帧的间隔 FrameCount += 1; if (FrameCount == SettingHelp.Settings.视频帧率) { FrameCount = 0; Dispatcher.Invoke(new Action(() => { lbTime.Content = time.ToString("hh\\:mm\\:ss"); })); } } }
public static Rectangle Bounds(this System.Windows.Forms.Cursor cursor) { using (var bmp = new Bitmap(cursor.Size.Width, cursor.Size.Height)) { using (var g = Graphics.FromImage(bmp)) { g.Clear(Color.Transparent); cursor.Draw(g, new Rectangle(Point.Empty, bmp.Size)); var xMin = bmp.Width; var xMax = -1; var yMin = bmp.Height; var yMax = -1; for (var x = 0; x < bmp.Width; x++) { for (var y = 0; y < bmp.Height; y++) { if (bmp.GetPixel(x, y).A > 0) { xMin = Math.Min(xMin, x); xMax = Math.Max(xMax, x); yMin = Math.Min(yMin, y); yMax = Math.Max(yMax, y); } } } return(new Rectangle(new Point(xMin, yMin), new Size((xMax - xMin) + 1, (yMax - yMin) + 1))); } } }
/// <summary> /// 桌面输出回调 /// </summary> private void VideoNewFrame(object sender, NewFrameEventArgs e) { if (isRecording && !isParsing) { if (SettingHelp.Settings.捕获鼠标) { var g = System.Drawing.Graphics.FromImage(e.Frame); CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); try { System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor); cur.Draw(g, new System.Drawing.Rectangle(System.Windows.Forms.Cursor.Position.X - 10, System.Windows.Forms.Cursor.Position.Y - 10, cur.Size.Width, cur.Size.Height)); } catch { }//打开任务管理器时会导致异常 } videoWriter.WriteVideoFrame(e.Frame); System.Threading.Tasks.Task.Factory.StartNew(() => { FrameCount += 1; if (FrameCount == SettingHelp.Settings.视频帧率)//凑够一个帧组加1s1,一切努力都是为了使实际视频时长和显示的时长高度匹配 { FrameCount = 0; videoSpan = videoSpan.Add(new TimeSpan(0, 0, 0, 1)); Dispatcher.Invoke(new Action(() => { lbTime.Content = videoSpan.ToString("hh\\:mm\\:ss"); })); } }); } }
public Image GetFullScreenImage(VideoParameters v) { try { System.Drawing.Image MyImage = new Bitmap((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight); CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); Graphics g = Graphics.FromImage(MyImage); g.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), new System.Drawing.Size((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight)); System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor); cur.Draw(g, new System.Drawing.Rectangle(pci.ptScreenPos.x - 10, pci.ptScreenPos.y - 10, cur.Size.Width, cur.Size.Height)); Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); MyImage.RotateFlip(RotateFlipType.Rotate90FlipNone); Image img = MyImage.GetThumbnailImage(v.Height, v.Width, myCallback, IntPtr.Zero); return(img); } catch { throw; } }
public byte[] GetDesktopBitmapBytes() { Size DesktopBitmapSize = GetDesktopBitmapSize(); Graphics Graphic = Graphics.FromHwnd(GetDesktopWindow()); Bitmap MemImage = new Bitmap(DesktopBitmapSize.Width, DesktopBitmapSize.Height, Graphic); Graphics MemGraphic = Graphics.FromImage(MemImage); IntPtr dc1 = Graphic.GetHdc(); IntPtr dc2 = MemGraphic.GetHdc(); BitBlt(dc2, 0, 0, DesktopBitmapSize.Width, DesktopBitmapSize.Height, dc1, 0, 0, SRCCOPY); Graphic.ReleaseHdc(dc1); MemGraphic.ReleaseHdc(dc2); Graphic.Dispose(); MemGraphic.Dispose(); Graphics g = System.Drawing.Graphics.FromImage(MemImage); System.Windows.Forms.Cursor cur = System.Windows.Forms.Cursors.Arrow; //cur.Draw(g, new Rectangle(System.Windows.Forms.Cursor.Position.X - 10, System.Windows.Forms.Cursor.Position.Y - 10, cur.Size.Width, cur.Size.Height)); cur.Draw(g, new Rectangle(System.Windows.Forms.Cursor.Position.X, System.Windows.Forms.Cursor.Position.Y, cur.Size.Width, cur.Size.Height)); MemoryStream ms = new MemoryStream(); MemImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); return(ms.GetBuffer()); }
public static ImageSource ToImageSource(this System.Windows.Forms.Cursor cursor) { int width = cursor.Size.Width; int height = cursor.Size.Height; using (System.Drawing.Bitmap b = new System.Drawing.Bitmap(width, height)) { using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(b)) { cursor.Draw(g, new System.Drawing.Rectangle(0, 0, width, height)); return(b.ToBitmapSource()); } } }
public static Bitmap GetSystemCursorBitmap(System.Windows.Forms.Cursor cursor) { Bitmap bitmap = new Bitmap( cursor.Size.Width, cursor.Size.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(bitmap); cursor.Draw(graphics, new Rectangle(new Point(0, 0), cursor.Size)); bitmap = Crop(bitmap); return(bitmap); }
public Bitmap Decoder(List <Stream> images, List <Rectangle> bounds, Rectangle screen, Point cursor) { if (Old == null) { Old = new Bitmap(screen.Width, screen.Height); Graphics g = Graphics.FromImage(Old); g.DrawImage((Bitmap)Bitmap.FromStream(images[0]), screen); g.Flush(); g.Dispose(); imageWithCursor = new Bitmap(Old); Graphics g2 = Graphics.FromImage(imageWithCursor); cursorIco.Draw(g2, new Rectangle(cursor.X, cursor.Y, 10, 10)); g2.Flush(); g2.Dispose(); return(imageWithCursor); } else { for (int i = 0; i < bounds.Count; i++) { Bitmap rec = (Bitmap)Bitmap.FromStream(images[i]); Graphics g = Graphics.FromImage(Old); g.DrawImage(rec, bounds[i]); g.Flush(); g.Dispose(); rec.Dispose(); } imageWithCursor = new Bitmap(Old); Graphics g2 = Graphics.FromImage(imageWithCursor); cursorIco.Draw(g2, new Rectangle(cursor.X, cursor.Y, 10, 10)); g2.Flush(); g2.Dispose(); return(imageWithCursor); } }
public byte[] GetDesktopBitmapBytes() { Bitmap DrawImage = new Bitmap(320, 240); Size DesktopBitmapSize = GetDesktopBitmapSize(); Graphics Graphic = Graphics.FromHwnd(GetDesktopWindow()); Bitmap MemImage = new Bitmap(DesktopBitmapSize.Width, DesktopBitmapSize.Height, Graphic); Graphics MemGraphic = Graphics.FromImage(MemImage); IntPtr dc1 = Graphic.GetHdc(); IntPtr dc2 = MemGraphic.GetHdc(); BitBlt(dc2, 0, 0, DesktopBitmapSize.Width, DesktopBitmapSize.Height, dc1, 0, 0, SRCCOPY); Graphic.ReleaseHdc(dc1); MemGraphic.ReleaseHdc(dc2); Graphic.Dispose(); MemGraphic.Dispose(); Graphics g = System.Drawing.Graphics.FromImage(MemImage); System.Windows.Forms.Cursor cur = System.Windows.Forms.Cursors.Arrow; cur.Draw(g, new Rectangle(System.Windows.Forms.Cursor.Position.X - 10, System.Windows.Forms.Cursor.Position.Y - 10, cur.Size.Width, cur.Size.Height)); using (Graphics gp = Graphics.FromImage(DrawImage)) { gp.DrawImage(MemImage, 0, 0, 320, 240); } int offset = 623; EncoderParameter epQuality = new EncoderParameter(Encoder.Quality, 74L); // Store the quality parameter in the list of encoder parameters EncoderParameters epParameters = new EncoderParameters(1); epParameters.Param[0] = epQuality; MemoryStream ms = new MemoryStream(); DrawImage.Save(ms, GetImageCodecInfo(ImageFormat.Jpeg), epParameters); byte[] data = new byte[(int)ms.Length - offset]; Array.Copy(ms.GetBuffer(), offset, data, 0, (int)ms.Length - offset); return(data); }
private void InitializeMouse() { defaultImageUsed = true; MemoryStream stream = new MemoryStream(); WFCursor cursor = WFCursors.Default; wfBitmap = new WFBitmap(WFCursors.Default.Size.Width, cursor.Size.Height); Graphics g = Graphics.FromImage(wfBitmap); cursor.Draw(g, new WFRectangle(0, 0, wfBitmap.Width, wfBitmap.Height)); WFBitmap bitmap = wfBitmap; bitmap.MakeTransparent(WFColor.Magenta); bitmap.Save(stream, WFImageFormat.Png); stream.Position = 0L; defaultImage = new BitmapImage(); defaultImage.BeginInit(); defaultImage.StreamSource = stream; defaultImage.EndInit(); image.Source = defaultImage; }
public Image GetFullScreenImage(VideoParameters v) { try { System.Drawing.Image MyImage = new Bitmap((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight); CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); Graphics g = Graphics.FromImage(MyImage); g.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), new System.Drawing.Size((int)SystemParameters.VirtualScreenWidth, (int)SystemParameters.VirtualScreenHeight)); System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor); cur.Draw(g, new System.Drawing.Rectangle(pci.ptScreenPos.x - 10, pci.ptScreenPos.y - 10, cur.Size.Width, cur.Size.Height)); Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); MyImage.RotateFlip(RotateFlipType.Rotate90FlipNone); Image img = MyImage.GetThumbnailImage(v.Height, v.Width, myCallback, IntPtr.Zero); return img; } catch { throw; } }
private GLCursor(System.Windows.Forms.Cursor cursor, bool centeredHotSpot = true) { if (!usingSdl2) { Handle = cursor.Handle; Cursor = cursor; return; } // now it's getting ugly... // this is converting our shiny cursors to the minimalistic cursor support of sdl2. // since there seems to be NO F*CKING WAY to get the cursor hot spot from X11, // we just center it for every cursor except the default pointer and the hand. int w = Math.Max(cursor.Size.Width, 32); // also, no size... int h = Math.Max(cursor.Size.Height, 32); // (╯°□°)╯︵ ┻━┻ var rect = new Rectangle(0, 0, w, h); var bmp = new Bitmap(w, h); bmp.MakeTransparent(); using (var g = Graphics.FromImage(bmp)) { cursor.Draw(g, rect); } var data = bmp.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); var color = new byte[data.Height * data.Width / 8]; var visibility = new byte[data.Height * data.Width / 8]; unsafe { fixed(byte *c = color, v = visibility) { Point min = new Point(int.MaxValue, int.MaxValue), max = new Point(); byte *d = (byte *)data.Scan0; for (int y = 0; y < data.Height; y++) { for (int x = 0; x < data.Width; x++) { int a = d[y * data.Stride + x * 4 + 3]; int l = d[y * data.Stride + x * 4 + 0] + d[y * data.Stride + x * 4 + 1] + d[y * data.Stride + x * 4 + 2]; if (a > 100) { min.X = Math.Min(min.X, x); min.Y = Math.Min(min.Y, y); max.X = Math.Max(max.X, x); max.Y = Math.Max(max.Y, y); v[(y * data.Width + x) / 8] |= (byte)(1 << (7 - (x & 7))); if (l < 300) { c[(y * data.Width + x) / 8] |= (byte)(1 << (7 - (x & 7))); } } } } Point hotspot = centeredHotSpot ? new Point((min.X + max.X) / 2, (min.Y + max.Y) / 2) : min; Handle = CreateCursor((IntPtr)c, (IntPtr)v, data.Width, data.Height, hotspot.X, hotspot.Y); } } bmp.UnlockBits(data); bmp.Dispose(); }
public Image GetRectangleScreenshot(VideoParameters v) { try { int videoWidth = (int)SystemParameters.VirtualScreenWidth; int videoHeight = (int)SystemParameters.VirtualScreenHeight; System.Drawing.Image MyImage = new Bitmap(v.Width, v.Height); CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); Graphics g = Graphics.FromImage(MyImage); int px = 0; int py = 0; int wx = videoWidth - pci.ptScreenPos.x; int wy = videoHeight - pci.ptScreenPos.y; int mouseX = 0; int mouseY = 0; if (pci.ptScreenPos.x > v.Width && wx < v.Width) { px = videoWidth - v.Width; mouseX = pci.ptScreenPos.x - (videoWidth - v.Width); } else if (pci.ptScreenPos.x < v.Width && wx > v.Width) { px = 0; mouseX = pci.ptScreenPos.x; } else if (pci.ptScreenPos.x - (v.Width / 2) > 0 && pci.ptScreenPos.x + (v.Width / 2) < videoWidth) { px = pci.ptScreenPos.x - (v.Width / 2); mouseX = v.Width / 2; } if (pci.ptScreenPos.y > v.Height && wy < v.Height) { py = videoHeight - v.Height; mouseY = pci.ptScreenPos.y - (videoHeight - v.Height); } else if (pci.ptScreenPos.y < v.Height && wy > v.Height) { py = 0; mouseY = pci.ptScreenPos.y; } else if (pci.ptScreenPos.y - (v.Height / 2) > 0 && pci.ptScreenPos.y + (v.Height / 2) < videoHeight) { py = pci.ptScreenPos.y - (v.Height / 2); mouseY = v.Height / 2; } g.CopyFromScreen(new System.Drawing.Point(px, py), new System.Drawing.Point(0, 0), new System.Drawing.Size(v.Width, v.Height)); System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor); cur.Draw(g, new System.Drawing.Rectangle(mouseX, mouseY, cur.Size.Width, cur.Size.Height)); Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); MyImage.RotateFlip(RotateFlipType.Rotate90FlipNone); Image img = MyImage.GetThumbnailImage(v.Height, v.Width, myCallback, IntPtr.Zero); //img.Save(@"D:\1.jpg"); return(img); } catch { throw; } }
public Image GetRectangleScreenshot(VideoParameters v) { try { int videoWidth = (int)SystemParameters.VirtualScreenWidth; int videoHeight = (int)SystemParameters.VirtualScreenHeight; System.Drawing.Image MyImage = new Bitmap(v.Width,v.Height ); CURSORINFO pci; pci.cbSize = Marshal.SizeOf(typeof(CURSORINFO)); GetCursorInfo(out pci); Graphics g = Graphics.FromImage(MyImage); int px = 0; int py = 0; int wx= videoWidth - pci.ptScreenPos.x; int wy = videoHeight - pci.ptScreenPos.y; int mouseX=0; int mouseY=0; if(pci.ptScreenPos.x>v.Width && wx<v.Width) { px = videoWidth - v.Width; mouseX=pci.ptScreenPos.x-(videoWidth - v.Width); } else if(pci.ptScreenPos.x<v.Width && wx>v.Width) { px = 0; mouseX=pci.ptScreenPos.x; } else if(pci.ptScreenPos.x-(v.Width/2)>0 && pci.ptScreenPos.x+(v.Width/2)<videoWidth) { px = pci.ptScreenPos.x - (v.Width / 2); mouseX=v.Width/2; } if(pci.ptScreenPos.y>v.Height && wy<v.Height) { py = videoHeight - v.Height; mouseY=pci.ptScreenPos.y-(videoHeight-v.Height); } else if (pci.ptScreenPos.y < v.Height && wy > v.Height) { py = 0; mouseY=pci.ptScreenPos.y; } else if(pci.ptScreenPos.y-(v.Height/2)>0 && pci.ptScreenPos.y+(v.Height/2)<videoHeight) { py = pci.ptScreenPos.y - (v.Height / 2); mouseY=v.Height/2; } g.CopyFromScreen(new System.Drawing.Point(px, py), new System.Drawing.Point(0, 0), new System.Drawing.Size(v.Width, v.Height)); System.Windows.Forms.Cursor cur = new System.Windows.Forms.Cursor(pci.hCursor); cur.Draw(g, new System.Drawing.Rectangle(mouseX,mouseY, cur.Size.Width, cur.Size.Height)); Image.GetThumbnailImageAbort myCallback = new Image.GetThumbnailImageAbort(ThumbnailCallback); MyImage.RotateFlip(RotateFlipType.Rotate90FlipNone); Image img = MyImage.GetThumbnailImage(v.Height, v.Width, myCallback, IntPtr.Zero); //img.Save(@"D:\1.jpg"); return img; } catch { throw; } }