public void SelectBitmap(Bitmap bitmap)
        {
            if (bitmap.PixelFormat != PixelFormat.Format32bppArgb)
            {
                throw new ApplicationException("The bitmap must be 32bpp with alpha-channel.");
            }

            IntPtr screenDc   = APIHelp.GetDC(IntPtr.Zero);
            IntPtr memDc      = APIHelp.CreateCompatibleDC(screenDc);
            IntPtr hBitmap    = IntPtr.Zero;
            IntPtr hOldBitmap = IntPtr.Zero;

            try {
                hBitmap    = bitmap.GetHbitmap(Color.FromArgb(0));
                hOldBitmap = APIHelp.SelectObject(memDc, hBitmap);

                APIHelp.Size  newSize        = new APIHelp.Size(bitmap.Width, bitmap.Height);
                APIHelp.Point sourceLocation = new APIHelp.Point(0, 0);
                APIHelp.Point newLocation    = new APIHelp.Point(this.Left, this.Top);

                APIHelp.BLENDFUNCTION blend = new APIHelp.BLENDFUNCTION {
                    BlendOp             = APIHelp.AC_SRC_OVER,
                    BlendFlags          = 0,
                    SourceConstantAlpha = 255,
                    AlphaFormat         = APIHelp.AC_SRC_ALPHA
                };

                APIHelp.UpdateLayeredWindow(Handle, screenDc, ref newLocation, ref newSize, memDc, ref sourceLocation, 0, ref blend, APIHelp.ULW_ALPHA);
            } finally {
                APIHelp.ReleaseDC(IntPtr.Zero, screenDc);
                if (hBitmap != IntPtr.Zero)
                {
                    APIHelp.SelectObject(memDc, hOldBitmap);
                    APIHelp.DeleteObject(hBitmap);
                }
                APIHelp.DeleteDC(memDc);
            }
        }
Example #2
0
        private void timer1_Tick(object sender, EventArgs e)
        {
            var c = Cursor.Position;

            //Invalidate();


            APIHelp.BLENDFUNCTION blend;

            //Only works with a 32bpp bitmap
            blend.BlendOp = APIHelp.AC_SRC_OVER;
            //Always 0
            blend.BlendFlags = 0;
            //Set to 255 for per-pixel alpha values
            blend.SourceConstantAlpha = 255;
            //Only works when the bitmap contains an alpha channel
            blend.AlphaFormat = APIHelp.AC_SRC_ALPHA;

            IntPtr screenDc;

            screenDc = APIHelp.GetDC(IntPtr.Zero);
            shiftX   = Magnifier.lastRect.left;
            shiftY   = Magnifier.lastRect.top;

            using (Bitmap bmp = new Bitmap(this.Width, this.Height))
            {
                using (var gr = Graphics.FromImage(bmp))
                {
                    gr.Clear(Color.Transparent);
                    DoubleBuffered = true;
                    gr.FillRectangle(new SolidBrush(Color.FromArgb(128, Color.Red)), xx, 100, 200, 300);
                    gr.DrawRectangle(new Pen(Color.Yellow, 3), new Rectangle(shiftX, shiftY, Magnifier.lastRect.right - Magnifier.lastRect.left, Magnifier.lastRect.bottom - Magnifier.lastRect.top));


                    lock (Magnifier.lock1)
                    {
                        //gr.DrawImage(Magnifier.LastBmp, 0, 0);
                        //gr.DrawImage(grayer, 0, 0);
                    }
                    Rect[]  detections = new Rect[0];
                    float[] oscores    = new float[0];
                    int[]   classes    = new int[0];
                    lock (lock1)
                    {
                        if (last != null)
                        {
                            /*using (var bmp1 = new Bitmap(1920, 1080))
                             * {
                             *  bmp1.MakeTransparent();
                             *  using (var ggr = Graphics.FromImage(bmp1))
                             *  {
                             *      ggr.Clear(Color.FromArgb(64, Color.Red));
                             *      e.Graphics.DrawImage(bmp1, 0, 0);
                             *  }
                             * }*/

                            detections = last.Item1;
                            oscores    = last.Item2;
                            classes    = last.Item3;
                        }
                    }
                    int   hi    = -1;
                    float hconf = 0;
                    for (int i = 0; i < detections.Length; i++)
                    {
                        if (oscores[i] < visTresh)
                        {
                            continue;
                        }
                        int cls = 0;
                        if (classes != null)
                        {
                            cls = classes[i];
                        }
                        if (cls == 0)
                        {
                            if (oscores[i] > hconf)
                            {
                                hconf = oscores[i];
                                hi    = i;
                            }
                        }
                    }

                    for (int i = 0; i < detections.Length; i++)
                    {
                        if (oscores[i] < visTresh)
                        {
                            continue;
                        }
                        Rects.Add(new RectLog()
                        {
                            Rect = detections[i], Shift = new OpenCvSharp.Point(shiftX, shiftY)
                        });

                        var text = Math.Round(oscores[i], 4).ToString();
                        int cls  = -1;
                        if (classes != null)
                        {
                            cls   = classes[i];
                            text += $"(cls: {cls})";
                        }
                        var cx = detections[i].X;
                        var cy = detections[i].Y + 12;

                        /*   if (cls == 0 && hi == i)
                         * {
                         *     gr.DrawRectangle(new Pen(Color.FromArgb(0, 255, 0), 3), detections[i].Left + shiftX, detections[i].Top + shiftY, detections[i].Width, detections[i].Height);
                         *     if (FillRects)
                         *         gr.FillRectangle(new SolidBrush(Color.FromArgb(0, 255, 0)), detections[i].Left + shiftX, detections[i].Top + shiftY, detections[i].Width, detections[i].Height);
                         * }
                         * else if (cls >= 2)
                         * {
                         *     gr.DrawRectangle(new Pen(Color.FromArgb(255, 0, 255), 2), detections[i].Left + shiftX, detections[i].Top + shiftY, detections[i].Width, detections[i].Height);
                         *     if (FillRects)
                         *         gr.FillRectangle(new SolidBrush(Color.FromArgb(0, 255, 0)), detections[i].Left + shiftX, detections[i].Top + shiftY, detections[i].Width, detections[i].Height);
                         * }
                         * else
                         * {
                         *     gr.DrawRectangle(new Pen(Color.FromArgb(255, 0, 0), 2), detections[i].Left + shiftX, detections[i].Top + shiftY, detections[i].Width, detections[i].Height);
                         *     if (FillRects)
                         *         gr.FillRectangle(new SolidBrush(Color.FromArgb(0, 255, 0)), detections[i].Left + shiftX, detections[i].Top + shiftY, detections[i].Width, detections[i].Height);
                         * }*/
                        //mat.Rectangle(new OpenCvSharp.Point(cx, cy + 5), new OpenCvSharp.Point(cx + 120, cy - 15), new Scalar(0, 0, 0), -1);
                        //mat.PutText(text, new OpenCvSharp.Point(cx, cy),
                        //          HersheyFonts.HersheyDuplex, 0.5, new Scalar(255, 255, 255));
                    }

                    foreach (var item in Rects)
                    {
                        var shift = item.Shift;
                        item.Timestamp--;
                        gr.DrawRectangle(new Pen(Color.FromArgb(255, 0, 255), 2), item.Rect.Left + shift.X, item.Rect.Top + shift.Y, item.Rect.Width, item.Rect.Height);
                        if (FillRects)
                        {
                            gr.FillRectangle(new SolidBrush(Color.FromArgb(0, 255, 0)), item.Rect.Left + shift.X, item.Rect.Top + shift.Y, item.Rect.Width, item.Rect.Height);
                        }
                    }
                    if (UseHistory)
                    {
                        Rects.RemoveAll(z => z.Timestamp == 0);
                    }
                    else
                    {
                        Rects.Clear();
                    }



                    //using (bmp = (Bitmap)Bitmap.FromFile(@"C:\.......png")) //the image must be the same size as your form
                    {
                        memDc   = APIHelp.CreateCompatibleDC(screenDc);
                        hBmp    = bmp.GetHbitmap(Color.FromArgb(0));
                        hOldBmp = APIHelp.SelectObject(memDc, hBmp); //memDc is a device context that contains our image
                    }
                }
            }

            APIHelp.DeleteDC(screenDc);


            APIHelp.Size  newSize;
            APIHelp.Point newLocation;
            APIHelp.Point sourceLocation;

            newLocation.x = this.Location.X;
            newLocation.y = this.Location.Y;

            sourceLocation.x = 0;
            sourceLocation.y = 0;

            newSize.cx = this.Width;
            newSize.cy = this.Height;

            APIHelp.UpdateLayeredWindow(Handle, IntPtr.Zero, ref newLocation, ref newSize, memDc, ref sourceLocation,
                                        0, ref blend, APIHelp.ULW_ALPHA);
            APIHelp.DeleteDC(memDc);
            APIHelp.DeleteObject(hBmp);
            APIHelp.DeleteObject(hOldBmp);
        }