// 0 1 // 2 3 void DrawFrame(CamStream c, Graphics g) { int x = 0; int y = 0; if (c != null) { switch (c.Id) { case 0: x = 0; y = 0; break; case 1: x = 1; y = 0; break; case 2: x = 0; y = 1; break; case 3: x = 1; y = 1; break; } } var maximumWidth = boundsItem.Width; var maximumHeight = boundsItem.Height; var newImageWidth = maximumWidth; var newImageHeight = maximumHeight; var image = c.rgbBits; var ratioX = maximumWidth / (double)image.Width; var ratioY = maximumHeight / (double)image.Height; var ratio = ratioX < ratioY ? ratioX : ratioY; newImageHeight = (int)(image.Height * ratio); newImageWidth = (int)(image.Width * ratio); { int div = 1; Point pv = new Point( (int)((maximumWidth - (image.Width * ratio)) / div), (int)((maximumHeight - (image.Height * ratio)) / div)); //pv.Offset(x * maximumWidth, y * maximumHeight); g.DrawImage(image, pv.X, pv.Y, newImageWidth, newImageHeight); var rc = RectangleF.FromLTRB(0, 0, newImageWidth, newImageHeight); rc.Offset(pv); g.DrawString(c.Ip, f, Brushes.LimeGreen, rc, sfTopLeft); //if (c.Id == 1) { lock(this) { g.DrawString($"{DateTime.Now.ToLongTimeString()}", f, Brushes.LimeGreen, rc, sfTopRight); } } } }
private void button1_Click(object sender, EventArgs e) { Queue<string> camStack = new Queue<string>(textBoxCams.Lines.Where(p => p.Contains(':'))); if (cams.Count == 0) { timerUpdate = new System.Windows.Forms.Timer(); timerUpdate.Interval = 1000; timerUpdate.Tick += new System.EventHandler(this.timerUpdate_Tick); timerUpdate.Start(); } Stack<CamStream> camTmp = new Stack<CamStream>(cams); int i = 0; while(camStack.Count > 0) { var cIp = camStack.Dequeue(); CamStream c; if (camTmp.Count > 0) { c = camTmp.Pop(); } else { c = new CamStream() { Ip = cIp, Worker = new BackgroundWorker(), Id = i++, SetEncode = true, SetView = true, encoder = TurboJpegEncoder.CreateEncoder() }; c.Worker.WorkerSupportsCancellation = true; c.Worker.DoWork += GetCameraFrames_DoWork; cams.Add(c); } c.Worker.RunWorkerAsync(c); //break; } button1.Enabled = false; buttonStop.Enabled = true; }
int ProcessJpegFrame(byte[] buff, int size, CamStream c) { { if (buff[0] != 0xFF || buff[1] != 0xD8 || buff[size - 2] != 0xFF || buff[size - 1] != 0xD9) { Debug.WriteLine("Bad jpeg..."); return -1; } c.encoder.EncodeJpegToRGB24(buff, size, ref c.rgb, ref fwidth, ref fheight); if (c.rgbBits == null) { var p = Marshal.UnsafeAddrOfPinnedArrayElement(c.rgb, 0); int bitsPerPixel = ((int)PixelFormat.Format24bppRgb & 0xff00) >> 8; int bytesPerPixel = (bitsPerPixel + 7) / 8; int stride = 4 * ((fwidth * bytesPerPixel + 3) / 4); c.rgbBits = new Bitmap(fwidth, fheight, stride, PixelFormat.Format24bppRgb, p); //t.Save("test2.bmp"); //var data = c.rgbBits.LockBits(boundsItem, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb); //c.rgbBits.UnlockBits(data); } if (SetEncode) { lock (img) { using (var g = Graphics.FromImage(img)) { DrawFrame(c, g); } } } if (SetView && c.rgbBits != null) { lock (imgView) { using (var gw = Graphics.FromImage(imgView)) { DrawFrame(c, gw); } } } } return 0; }