// On new frame private void VideoNewFrame(object sender, NewFrameEventArgs e) { var tsBrush = new SolidBrush(iSpyServer.Default.TimestampColor); var f = new Font(FontFamily.GenericSansSerif, 9, FontStyle.Regular, GraphicsUnit.Pixel); var sbTs = new SolidBrush(Color.FromArgb(128, 0, 0, 0)); Bitmap bmOrig = null, bmp = null; Graphics g = null, gCam = null; try { if (e.Frame != null) { _width = e.Frame.Width; _height = e.Frame.Height; lock (this) { if (LastFrameUnmanaged != null) LastFrameUnmanaged.Dispose(); if (CW.Camobject.settings.resize && (CW.Camobject.settings.desktopresizewidth != e.Frame.Width || CW.Camobject.settings.desktopresizeheight != e.Frame.Height)) { var result = new Bitmap(CW.Camobject.settings.desktopresizewidth, CW.Camobject.settings.desktopresizeheight, PixelFormat.Format24bppRgb); using (Graphics g2 = Graphics.FromImage(result)) g2.DrawImage(e.Frame, 0, 0, result.Width, result.Height); e.Frame.Dispose(); bmOrig = result; } else { bmOrig = e.Frame; } if (CW.Camobject.flipx) { bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipX); } if (CW.Camobject.flipy) { bmOrig.RotateFlip(RotateFlipType.RotateNoneFlipY); } if (Mask != null) { g = Graphics.FromImage(bmOrig); g.DrawImage(Mask, 0, 0, _width, _height); } LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmOrig); if (CW.Camobject.settings.timestamplocation != 0 && CW.Camobject.settings.timestampformatter != "") { bmp = LastFrameUnmanaged.ToManagedImage(); gCam = Graphics.FromImage(bmp); string timestamp = String.Format( CW.Camobject.settings.timestampformatter.Replace("{FPS}", string.Format("{0:F2}", CW.Framerate)), DateTime.Now); Size rs = gCam.MeasureString(timestamp, f).ToSize(); var p = new Point(0, 0); switch (CW.Camobject.settings.timestamplocation) { case 2: p.X = _width/2 - (rs.Width/2); break; case 3: p.X = _width - rs.Width; break; case 4: p.Y = _height - rs.Height; break; case 5: p.Y = _height - rs.Height; p.X = _width/2 - (rs.Width/2); break; case 6: p.Y = _height - rs.Height; p.X = _width - rs.Width; break; } var rect = new Rectangle(p, rs); gCam.FillRectangle(sbTs, rect); gCam.DrawString(timestamp, f, tsBrush, p); LastFrameUnmanaged.Dispose(); LastFrameUnmanaged = UnmanagedImage.FromManagedImage(bmp); } } } } catch (UnsupportedImageFormatException ex) { CW.VideoSourceErrorState = true; CW.VideoSourceErrorMessage = ex.Message; if (LastFrameUnmanaged != null) { try { lock (this) { LastFrameUnmanaged.Dispose(); LastFrameUnmanaged = null; } } catch { } } } catch (Exception ex) { MainForm.LogExceptionToFile(ex); } if (gCam != null) gCam.Dispose(); if (bmp != null) bmp.Dispose(); if (g != null) g.Dispose(); if (bmOrig != null) bmOrig.Dispose(); tsBrush.Dispose(); f.Dispose(); sbTs.Dispose(); if (NewFrame != null && LastFrameUnmanaged != null) { LastFrameNull = false; NewFrame(this, new EventArgs()); } }
/// <summary> /// /// </summary> /// <param name="image"></param> /// <param name="start"></param> /// <param name="maxEyeRadius">Should be 2-3 percent of max(width/height)</param> /// <param name="maxPointSearchDistance">In source pixels, the max distance from 'start' from which to look for the starting point. Good default: roughly 24 display pixels.</param> public static void MarkEye(UnmanagedImage image, Point start, int maxEyeRadius, float maxPointSearchDistance = 0) { int maxRadius = maxEyeRadius * 2 + (int)Math.Ceiling(maxPointSearchDistance); //Find subset Rectangle subset = new Rectangle(start.X - maxRadius,start.Y - maxRadius,maxRadius * 2, maxRadius * 2); if (subset.X < 0) { subset.Width += subset.X; subset.X = 0; } if (subset.Y < 0) { subset.Height += subset.Y; subset.Y = 0; } if (subset.Right >= image.Width) subset.Width -= (subset.Right - image.Width + 1); if (subset.Bottom >= image.Height) subset.Height -= (subset.Bottom - image.Height + 1); start.X -= subset.X; start.Y -= subset.Y; //Skip processing if we're slightly out of bounds if (subset.X < 0 || subset.Y < 0 || subset.Width < 0 || subset.Height < 0 || subset.Right >= image.Width || subset.Bottom >= image.Height) return; UnmanagedImage red = null; try{ Point startAt = start; using (UnmanagedImage c = new Crop(subset).Apply(image)) { red = new RedEyeFilter(2).Apply(c); if (maxPointSearchDistance > 0) startAt = new ManualSearcher().FindMaxPixel(c,start,maxPointSearchDistance); } var fill = new AdaptiveCircleFill(red, startAt, start, maxEyeRadius * 2); fill.FirstPass(); fill.SecondPass(); fill.CorrectRedEye(image, subset.X, subset.Y); //fill.MarkFilledPixels(image, subset.X, subset.Y, new byte[] { 0, 255, 0, 0 }); //fill.SetPixel(image, startAt.X + subset.X, startAt.Y + subset.Y, new byte[] { 255, 255, 0, 0 }); }finally{ if (red != null) red.Dispose(); } }
public unsafe List <IntPoint> ProcessImage(UnmanagedImage image) { if (image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format24bppRgb && image.PixelFormat != PixelFormat.Format32bppRgb && image.PixelFormat != PixelFormat.Format32bppArgb) { throw new UnsupportedImageFormatException("Unsupported pixel format of the source image."); } int width = image.Width; int height = image.Height; UnmanagedImage unmanagedImage = null; unmanagedImage = ((image.PixelFormat != PixelFormat.Format8bppIndexed) ? Grayscale.CommonAlgorithms.BT709.Apply(image) : image); int[,] array = new int[height, width]; int stride = unmanagedImage.Stride; int num = stride - width; byte *ptr = (byte *)unmanagedImage.ImageData.ToPointer() + (long)stride * 3L + 3; int i = 3; for (int num2 = height - 3; i < num2; i++) { int num3 = 3; int num4 = width - 3; while (num3 < num4) { byte b = *ptr; int num5 = 0; int num6 = 0; int num7 = 0; for (int j = -3; j <= 3; j++) { int num8 = rowRadius[j + 3]; byte *ptr2 = ptr + (long)stride * (long)j; for (int k = -num8; k <= num8; k++) { if (System.Math.Abs(b - ptr2[k]) <= differenceThreshold) { num5++; num6 += num3 + k; num7 += i + j; } } } if (num5 < geometricalThreshold) { num6 /= num5; num7 /= num5; num5 = ((num3 != num6 || i != num7) ? (geometricalThreshold - num5) : 0); } else { num5 = 0; } array[i, num3] = num5; num3++; ptr++; } ptr += 6 + num; } if (image.PixelFormat != PixelFormat.Format8bppIndexed) { unmanagedImage.Dispose(); } List <IntPoint> list = new List <IntPoint>(); int l = 2; for (int num9 = height - 2; l < num9; l++) { int m = 2; for (int num10 = width - 2; m < num10; m++) { int num11 = array[l, m]; int num12 = -2; while (num11 != 0 && num12 <= 2) { for (int n = -2; n <= 2; n++) { if (array[l + num12, m + n] > num11) { num11 = 0; break; } } num12++; } if (num11 != 0) { list.Add(new IntPoint(m, l)); } } } return(list); }