Exemple #1
0
        private void UpdatePreview()
        {
            //Paper bitmap initializing
            int width;
            int height;

            width  = 0;
            height = 0;
            CalculatePaperImageDimensions(ref width, ref height);
            System.Drawing.Size fittedSize = FitDimensions(width, height, BitmapViewer1.Width - 20, BitmapViewer1.Height - 20);

            if (BitmapViewer1.Bitmap != null)
            {
                BitmapViewer1.Bitmap.Dispose();
            }

            BitmapViewer1.Bitmap = new Aurigma.GraphicsMill.Bitmap(fittedSize.Width, fittedSize.Height, PixelFormat.Format24bppRgb, RgbColor.White);

            //Scale coefficients
            _previewScaleX = Convert.ToSingle(width) / Convert.ToSingle(fittedSize.Width);
            _previewScaleY = Convert.ToSingle(height) / Convert.ToSingle(fittedSize.Height);

            //Image rubberband initializing
            Aurigma.GraphicsMill.Bitmap previewImage = new Aurigma.GraphicsMill.Bitmap();
            Resize resizeTransform = new Resize();

            resizeTransform.Width      = fittedSize.Width;
            resizeTransform.Height     = fittedSize.Height;
            resizeTransform.ResizeMode = Aurigma.GraphicsMill.Transforms.ResizeMode.Fit;
            previewImage = resizeTransform.Apply(_originalImage);
            ImageRubberband rubberband = new ImageRubberband(previewImage);

            BitmapViewer1.Rubberband     = rubberband;
            rubberband.RectangleChanged += new Aurigma.GraphicsMill.WinControls.RectangleEventHandler(this.RubberbandRectangleChanged);

            //Setting image size
            int imageActualWidth;
            int imageActualHeight;

            imageActualWidth  = Convert.ToInt32(Convert.ToSingle(_originalImage.Width) / Convert.ToSingle(_originalImage.DpiX) * Convert.ToSingle(_printerResolutionX));
            imageActualHeight = Convert.ToInt32(Convert.ToSingle(_originalImage.Height) / Convert.ToSingle(_originalImage.DpiY) * Convert.ToSingle(_printerResolutionY));

            int previewImageWidth;
            int previewImageHeight;

            previewImageWidth  = Convert.ToInt32(Convert.ToSingle(imageActualWidth) / Convert.ToSingle(_previewScaleX));
            previewImageHeight = Convert.ToInt32(Convert.ToSingle(imageActualHeight) / Convert.ToSingle(_previewScaleY));

            System.Drawing.Size tmp = FitDimensions(previewImageWidth, previewImageHeight, fittedSize.Width, fittedSize.Height);
            previewImageWidth    = tmp.Width;
            previewImageHeight   = tmp.Height;
            rubberband.Rectangle = new System.Drawing.Rectangle((fittedSize.Width - previewImageWidth) / 2, (fittedSize.Height - previewImageHeight) / 2, previewImageWidth, previewImageHeight);

            //Initializing coordinates & size editboxes
            UpdateSizeAndPositionFromPreview();
        }
Exemple #2
0
        public static ProcessResultArray <Clip> ProcessCommand(Command command, Clip[] incomingClips, ClipMetaData targetMetadata)
        {
            var clips = new Clip[incomingClips.Length];

            for (var i = 0; i < incomingClips.Length; i++)
            {
                clips[i] = new Clip(incomingClips[i]);
            }

            return(command.Id switch
            {
                TokenType.Arpeggiate => Arpeggiate.Apply(command, clips),
                TokenType.Concat => Concat.Apply(clips),
                TokenType.Crop => Crop.Apply(command, clips),
                TokenType.Filter => Filter.Apply(command, clips),
                TokenType.Interleave => Interleave.Apply(command, targetMetadata, clips),
                TokenType.Legato => Legato.Apply(clips),
                TokenType.Mask => Mask.Apply(command, clips),
                TokenType.Monophonize => Monophonize.Apply(clips),
                TokenType.Padding => Padding.Apply(command, clips),
                TokenType.Quantize => Quantize.Apply(command, clips),
                TokenType.Ratchet => Ratchet.Apply(command, clips),
                TokenType.Relength => Relength.Apply(command, clips),
                TokenType.Remap => Remap.Apply(command, clips),
                TokenType.Resize => Resize.Apply(command, clips),
                TokenType.Scale => Scale.Apply(command, clips),
                TokenType.Scan => Scan.Apply(command, clips),
                TokenType.SetLength => SetLength.Apply(command, clips),
                TokenType.SetPitch => SetPitch.Apply(command, clips),
                TokenType.SetRhythm => SetRhythm.Apply(command, clips),
                TokenType.Shuffle => Shuffle.Apply(command, clips),
                TokenType.Skip => Skip.Apply(command, clips),
                TokenType.Slice => Slice.Apply(command, clips),
                TokenType.Take => Take.Apply(command, clips),
                TokenType.Transpose => Transpose.Apply(command, clips),
                TokenType.VelocityScale => VelocityScale.Apply(command, clips),
                TokenType.InterleaveEvent => ((Func <ProcessResultArray <Clip> >)(() =>
                {
                    var(success, msg) = OptionParser.TryParseOptions(command, out InterleaveOptions options);
                    if (!success)
                    {
                        return new ProcessResultArray <Clip>(msg);
                    }

                    options.Mode = InterleaveMode.Event;
                    return Interleave.Apply(options, targetMetadata, clips);
                }))(),
                _ => new ProcessResultArray <Clip>($"Unsupported command {command.Id}")
            });
        public static void Main(string[] args)
        {
            FileStream fs = new FileStream("d:\\DSC_6013.jpg", FileMode.Open, FileAccess.Read);
            Image img = new Image(fs);

            var resizer = new Resize(new BicubicResampler());

            var newImage = new Image();
            resizer.Apply(newImage, img, 100, 100);

            var wfs = new FileStream("d:\\DSC_6013_resized.jpg", FileMode.OpenOrCreate, FileAccess.Write);
            newImage.Save(wfs);
            wfs.Flush();

            string[] filenames;

            Parallel.ForEach<string>(filenames, ShrinkFile());
        }
Exemple #4
0
    public static ProcessResult <Clip[]> ProcessCommand(Command command, Clip[] incomingClips, ClipMetaData targetMetadata)
    {
        var clips = new Clip[incomingClips.Length];

        for (var i = 0; i < incomingClips.Length; i++)
        {
            clips[i] = new Clip(incomingClips[i]);
        }

        return(command.Id switch
        {
            TokenType.Arpeggiate => Arpeggiate.Apply(command, clips),
            TokenType.Concat => Concat.Apply(clips),
            TokenType.Crop => Crop.Apply(command, clips),
            TokenType.Echo => Echo.Apply(command, clips),
            TokenType.Extract => Take.Apply(command, clips, true),
            TokenType.Filter => Filter.Apply(command, clips),
            TokenType.Invert => Invert.Apply(command, clips),
            TokenType.Interleave => Interleave.Apply(command, targetMetadata, clips, InterleaveMode.NotSpecified),
            TokenType.InterleaveEvent => Interleave.Apply(command, targetMetadata, clips, InterleaveMode.Event),
            TokenType.Legato => Legato.Apply(clips),
            TokenType.Loop => Loop.Apply(command, clips),
            TokenType.Mask => Mask.Apply(command, clips),
            TokenType.Monophonize => Monophonize.Apply(clips),
            TokenType.Padding => Padding.Apply(command, clips),
            TokenType.Quantize => Quantize.Apply(command, clips),
            TokenType.Ratchet => Ratchet.Apply(command, clips),
            TokenType.Relength => Relength.Apply(command, clips),
            TokenType.Remap => Remap.Apply(command, clips),
            TokenType.Resize => Resize.Apply(command, clips),
            TokenType.Scale => Scale.Apply(command, clips),
            TokenType.Scan => Scan.Apply(command, clips),
            TokenType.SetLength => SetLength.Apply(command, clips),
            TokenType.SetPitch => SetPitch.Apply(command, clips),
            TokenType.SetRhythm => SetRhythm.Apply(command, clips),
            TokenType.Shuffle => Shuffle.Apply(command, clips),
            TokenType.Skip => Skip.Apply(command, clips),
            TokenType.Slice => Slice.Apply(command, clips),
            TokenType.Take => Take.Apply(command, clips),
            TokenType.Transpose => Transpose.Apply(command, clips),
            TokenType.VelocityScale => VelocityScale.Apply(command, clips),
            _ => new ProcessResult <Clip[]>($"Unsupported command {command.Id}")
        });
        // Process new frame
        public void ProcessFrame(ref Bitmap image)
        {
            if (_mForm.leftMouseButtonDown == true)
            {
                //Code to draw a rubberband selection rectangle

                //Compute width and height of rectangle
                int w = Math.Abs((int)_mForm.x2 - (int)_mForm.x1);
                int h = Math.Abs((int)_mForm.y2 - (int)_mForm.y1);

                //Decide x and y of rectangle
                int x = 0, y = 0;

                if (_mForm.x1 < _mForm.x2)
                {
                    x = (int)_mForm.x1;
                }
                else if ((int)_mForm.x1 > _mForm.x2)
                {
                    x = (int)_mForm.x2;
                }

                if (_mForm.y1 < _mForm.y2)
                {
                    y = (int)_mForm.y1;
                }
                else if (_mForm.y1 > _mForm.y2)
                {
                    y = (int)_mForm.y2;
                }

                //Draw the rectangle
                Rectangle rect = new Rectangle(x, y, w, h);

                Graphics g = Graphics.FromImage((System.Drawing.Image)image);

                g.FillRectangle(new SolidBrush(Color.FromArgb
                                                   (60, 184, 184, 0)), rect); //Draws a semi-transparent rectangle using alpha blending

                g.Dispose();
                return;
            }

            if (_mForm.videoCropHeight == -1)
            {
                //Set or reset video cropping so that nothing gets trimmed out
                _mForm.videoCropHeight = image.Height;
                _mForm.videoCropWidth  = image.Width;
            }

            //Crop the bitmap
            Bitmap tmpImage0 = image.Clone(new Rectangle(_mForm.videoCropX,
                                                         _mForm.videoCropY, _mForm.videoCropWidth, _mForm.videoCropHeight),
                                           System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            //Resize the bitmap
            AForge.Imaging.Filters.Resize resize = new Resize(image.Width / _mForm.resizeFactor,
                                                              image.Height / _mForm.resizeFactor, InterpolationMethod.NearestNeighbor);

            Bitmap tmpImage1 = resize.Apply(tmpImage0);

            //Get width and height of Bitmap
            imageWidth  = tmpImage1.Width;
            imageHeight = tmpImage1.Height;

            tmpImage0.Dispose();

            Utility.UnsafeBitmap uBitmap = new Utility.UnsafeBitmap(tmpImage1);

            //Locate the brightest pixel in the bitmap

            bool brightnessFound = false;

            uBitmap.LockBitmap();

            float brightest = 0;
            int   xPos = 0, yPos = 0, xprev = 0, yprev = 0, currx = 0, curry = 0, dispx = 0, dispy = 0;



            for (int y = 0; y < imageHeight; y += 1)
            {
                for (int x = 0; x < imageWidth; x += 1)
                {
                    byte red, green, blue;
                    red   = uBitmap.GetPixel(x, y).red;
                    green = uBitmap.GetPixel(x, y).green;
                    blue  = uBitmap.GetPixel(x, y).blue;



                    if (red > 150 && green < 100 && blue < 100)
                    {
                        xPos            = x;
                        yPos            = y;
                        currx           = x;
                        curry           = y;
                        brightnessFound = true;
                        s_red           = red; s_green = green; s_blue = blue;
                    }

                    if (green > 170 && red < 150 && blue < 190)
                    {
                        xPos            = x;
                        yPos            = y;
                        currx           = x;
                        curry           = y;
                        brightnessFound = true;

                        s_red = red; s_green = green; s_blue = blue;
                    }

                    if (blue > 150 && green < 100 && red < 100)
                    {
                        xPos            = x;
                        yPos            = y;
                        currx           = x;
                        curry           = y;
                        brightnessFound = true;
                        s_red           = red; s_green = green; s_blue = blue;
                    }

                    /* if (red > 220 && green > 220 && blue > 220)
                     * {
                     *
                     *   xPos = x;
                     *   yPos = y;
                     *   currx = x;
                     *   curry = y;
                     *   brightnessFound = true;
                     *   s_red = red; s_green = green; s_blue = blue;
                     * } */
                    if (red > 150 && green > 150 && blue < 100)
                    {
                        xPos            = x;
                        yPos            = y;
                        currx           = x;
                        curry           = y;
                        brightnessFound = true;
                        s_red           = red; s_green = green; s_blue = blue;
                    }

                    // (brightest > _mForm.threshold)
                } // x loop
            }     // y loop

            string path = "ByteValues.txt";

            // This text is added only once to the file.
            if (File.Exists(path))
            {
                File.Delete(path);
            }

            if (!File.Exists(path))
            {
                // Create a file to write to.
                using (StreamWriter sw = new StreamWriter(path, true))
                {
                    sw.WriteLine(s_red.ToString() + " " + s_green.ToString() + " " + s_blue.ToString());
                    sw.Close();
                }
            }
            if (brightnessFound == true)
            {
                int colour = 0;

                if (((s_red > s_blue) && (s_red > s_green) && (Math.Abs(s_red - s_green) > 65) && ((Math.Abs(s_red - s_blue) > 65))))
                {
                    colour = 1;
                }
                else if ((((s_green > s_red) && (s_green > s_blue) && Math.Abs(s_green - s_red) > 65) && ((Math.Abs(s_green - s_blue) > 65))))
                {
                    colour = 2;
                }
                else if (((s_blue > s_red) && (s_blue > s_green) && (Math.Abs(s_blue - s_red) > 65) && ((Math.Abs(s_blue - s_green) > 65))))
                {
                    colour = 3;
                }
                // else if (s_red>220  &&  s_green>220 && s_blue>220 )  colour = 4;
                else if (s_red > 150 && s_green > 150 && s_blue < 100)
                {
                    colour = 5;
                }

                string path1 = "Colour Detected.txt";



                switch (colour)
                {
                case 1:

                    if (!File.Exists(path1))
                    {
                        // Create a file to write to.
                        using (StreamWriter sw = new StreamWriter(path1, true))
                        {
                            sw.Write("Red Detected  ");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(path1))
                        {
                            sw.Write("Red Detected  ");
                            sw.Close();
                        }
                    }

                    if ((xprev - currx != 0) || (yprev - curry != 0))
                    {
                        dispx = xprev - currx;
                        dispy = yprev - curry;

                        ControlCursor(xPos, yPos, _mForm.enableClick);                  //Set cursor position
                    }
                    xprev = currx;
                    yprev = curry;

                    break;

                case 2:


                    if (!File.Exists(path1))
                    {
                        // Create a file to write to.
                        using (StreamWriter sw = new StreamWriter(path1, true))
                        {
                            sw.Write("Green Detected  ");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(path1))
                        {
                            sw.Write("Green Detected  ");
                            sw.Close();
                        }
                    }

                    //Generate a left mouse button click
                    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

                    break;

                case 3:

                    if (!File.Exists(path1))
                    {
                        // Create a file to write to.
                        using (StreamWriter sw = new StreamWriter(path1, true))
                        {
                            sw.Write("Blue Detected  ");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(path1))
                        {
                            sw.Write("Blue Detected  ");
                            sw.Close();
                        }
                    }
                    //Generate a left mouse button click
                    mouse_event(MOUSEEVENTF_RIGHTDOWN | MOUSEEVENTF_RIGHTUP, 0, 0, 0, 0);
                    break;

                /*  case 4:
                 *        if (!File.Exists(path1))
                 *             {
                 *                 // Create a file to write to.
                 *                 using (StreamWriter sw = new StreamWriter(path1, true))
                 *                 {
                 *                     sw.Write("White Detected ");
                 *                     sw.Close();
                 *                 }
                 *             }
                 *             else
                 *             {
                 *                 using (StreamWriter sw = File.AppendText(path1))
                 *                 {
                 *                     sw.Write("White Detected ");
                 *                     sw.Close();
                 *                 }
                 *             }
                 *                break;*/
                case 5:


                    if (!File.Exists(path1))
                    {
                        // Create a file to write to.
                        using (StreamWriter sw = new StreamWriter(path1, true))
                        {
                            sw.Write("Yellow Detected  ");
                            sw.Close();
                        }
                    }
                    else
                    {
                        using (StreamWriter sw = File.AppendText(path1))
                        {
                            sw.Write("Yellow Detected  ");
                            sw.Close();
                        }
                    }
                    break;
                }



                uBitmap.UnlockBitmap();

                //Encircle the brightest pixel
                Graphics dc = Graphics.FromImage(tmpImage1);
                Pen      p  = new Pen(Color.LimeGreen, 1);

                dc.DrawEllipse(p, xPos - 5, yPos - 5, 10, 10);

                //Show the x and y coordinates of the brightest pixel
                dc.DrawString("X: " + xPos.ToString() + ", Y: " +
                              yPos.ToString(), new Font("Verdana", 7, FontStyle.Regular),
                              Brushes.Yellow, new Point(xPos, yPos + 8));

                if (_mForm.controlMouse == true)
                {
                    ControlCursor(xPos, yPos, _mForm.enableClick); //Set cursor position
                }
                dc.Dispose();

                uBitmap.LockBitmap();
            }
            else
            {
                if (_mForm.enableClick == true && leftMouseButtonDown == true)
                {
                    //Generate a left mouse button click
                    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    leftMouseButtonDown = false;
                }
            }

            uBitmap.UnlockBitmap();
            uBitmap.Bitmap.Dispose();

            image.Dispose();
            image = tmpImage1;
        }
Exemple #6
0
        public static ProcessResultArray <Clip> ProcessCommand(Command command, Clip[] incomingClips, ClipMetaData targetMetadata)
        {
            var clips = new Clip[incomingClips.Length];

            for (var i = 0; i < incomingClips.Length; i++)
            {
                clips[i] = new Clip(incomingClips[i]);
            }

            ProcessResultArray <Clip> resultContainer;

            switch (command.Id)
            {
            case TokenType.Arpeggiate:
                resultContainer = Arpeggiate.Apply(command, clips);
                break;

            case TokenType.Concat:
                resultContainer = Concat.Apply(clips);
                break;

            case TokenType.Crop:
                resultContainer = Crop.Apply(command, clips);
                break;

            case TokenType.Filter:
                resultContainer = Filter.Apply(command, clips);
                break;

            case TokenType.Interleave:
                resultContainer = Interleave.Apply(command, targetMetadata, clips);
                break;

            case TokenType.InterleaveEvent:
                var(success, msg) = OptionParser.TryParseOptions(command, out InterleaveOptions options);
                if (!success)
                {
                    return(new ProcessResultArray <Clip>(msg));
                }
                options.Mode    = InterleaveMode.Event;
                resultContainer = Interleave.Apply(options, targetMetadata, clips);
                break;

            case TokenType.Legato:
                resultContainer = Legato.Apply(clips);
                break;

            case TokenType.Mask:
                resultContainer = Mask.Apply(command, clips);
                break;

            case TokenType.Monophonize:
                resultContainer = Monophonize.Apply(clips);
                break;

            case TokenType.Quantize:
                resultContainer = Quantize.Apply(command, clips);
                break;

            case TokenType.Ratchet:
                resultContainer = Ratchet.Apply(command, clips);
                break;

            case TokenType.Relength:
                resultContainer = Relength.Apply(command, clips);
                break;

            case TokenType.Resize:
                resultContainer = Resize.Apply(command, clips);
                break;

            case TokenType.Scale:
                resultContainer = Scale.Apply(command, clips);
                break;

            case TokenType.Scan:
                resultContainer = Scan.Apply(command, clips);
                break;

            case TokenType.SetLength:
                resultContainer = SetLength.Apply(command, clips);
                break;

            case TokenType.SetRhythm:
                resultContainer = SetRhythm.Apply(command, clips);
                break;

            case TokenType.Shuffle:
                resultContainer = Shuffle.Apply(command, clips);
                break;

            case TokenType.Skip:
                resultContainer = Skip.Apply(command, clips);
                break;

            case TokenType.Slice:
                resultContainer = Slice.Apply(command, clips);
                break;

            case TokenType.Take:
                resultContainer = Take.Apply(command, clips);
                break;

            case TokenType.Transpose:
                resultContainer = Transpose.Apply(command, clips);
                break;

            default:
                return(new ProcessResultArray <Clip>($"Unsupported command {command.Id}"));
            }
            return(resultContainer);
        }
Exemple #7
0
 private void ShrinkWorkingImage(string path)
 {
     using (var bitmap = new Aurigma.GraphicsMill.Bitmap(path))
     using (var resize = new Resize(display.Width, display.Height, ResizeInterpolationMode.High, ResizeMode.Fit))
     using (var result = resize.Apply(bitmap))
     {
         result.Save(resizedImage);
     }
 }
Exemple #8
0
        /// <summary>
        /// Se realiza:
        /// <para>1. Se posiciona la marca de agua de acuerdo a los <see cref="TissotIndicatrix"/> (Top, Bottom y del 1 - 8)</para>
        /// <para>2. Se realiza la deformación <see cref="Fisheye.Convert(FisheyeBitmap)"/></para>
        /// <para>3. Se realiza la deformación <see cref="Fisheye.ToLandscape(FisheyeBitmap)"/></para>
        /// <para>4. Se posiciona la marca de agua de acuerdo a los <see cref="TissotIndicatrix"/> de la posición del centro (9 - 12)</para>
        /// </summary>
        public void Prepare()
        {
            int            size = 0;
            AbstractBitmap img  = null;

            foreach (var indicatrix in _indicatrixes.Where(i => i.Position != Position.Center))
            {
                // Se conserva la relacion aspecto de acuerdo al ancho de la imagen

                if (_imageWatermark.Width > _imageWatermark.Height)
                {
                    size = (indicatrix.MaxWidth * _imageWatermark.Height) / _imageWatermark.Width;

                    img = Resize.Apply(_imageWatermark, indicatrix.MaxWidth, size);
                }
                else
                {
                    size = (indicatrix.MaxHeight * _imageWatermark.Width) / _imageWatermark.Height;

                    img = Resize.Apply(_imageWatermark, size, indicatrix.MaxHeight);
                }

                img = Rotate.Apply(img, indicatrix.Angle);

                var posX = indicatrix.X - (img.Width / 2);
                var posY = indicatrix.Y - (img.Height / 2);

                if (indicatrix.Position == Position.Top)
                {
                    this._topFisheye.InsertImageUnmanaged(img, posX, posY);
                }
                else
                {
                    img._image.RotateFlip(RotateFlipType.RotateNoneFlipXY);

                    this._bottomFisheye.InsertImageUnmanaged(img, posX, posY);
                }
            }

            // Se realiza la deformación fisheye

            var feTop    = Fisheye.Convert(this._topFisheye);
            var feBottom = Fisheye.Convert(this._bottomFisheye);

            // Se deforman los fisheye a landscape

            feBottom = Fisheye.ToLandscape(feBottom);

            feTop = Fisheye.ToLandscape(feTop);
            feTop._image.RotateFlip(RotateFlipType.RotateNoneFlipXY);

            // Se insertan en el bitmap "watermak"

            _waterMark.InsertImageUnmanaged(feTop, 0, 0);
            _waterMark.InsertImageUnmanaged(feBottom, 0, feBottom.Height);

            // Se insertan aquellos indicatrix que pertenezcan al centro de la imagen

            foreach (var indicatrix in _indicatrixes.Where(i => i.Position == Position.Center))
            {
                if (_imageWatermark.Width > _imageWatermark.Height)
                {
                    size = (indicatrix.MaxWidth * _imageWatermark.Height) / _imageWatermark.Width;

                    img = Resize.Apply(_imageWatermark, indicatrix.MaxWidth, size) as WatermarkBitmap;
                }
                else
                {
                    size = (indicatrix.MaxHeight * _imageWatermark.Width) / _imageWatermark.Height;

                    img = Resize.Apply(_imageWatermark, size, indicatrix.MaxHeight) as WatermarkBitmap;
                }

                _waterMark.InsertImageUnmanaged(img, indicatrix.X - img.Width / 2, indicatrix.Y - img.Height / 2);
            }

            //_waterMark.Save(@"C:\Users\Euler\Pictures\WMpreared2.png", ImageFormat.Png);
        }
Exemple #9
0
        // Process new frame
        public void ProcessFrame(ref Bitmap image)
        {
            if (_mForm.leftMouseButtonDown == true)
            {
                //Code to draw a rubberband selection rectangle

                //Compute width and height of rectangle
                int w = Math.Abs((int)_mForm.x2 - (int)_mForm.x1);
                int h = Math.Abs((int)_mForm.y2 - (int)_mForm.y1);

                //Decide x and y of rectangle
                int x = 0, y = 0;

                if (_mForm.x1 < _mForm.x2)
                {
                    x = (int)_mForm.x1;
                }
                else if ((int)_mForm.x1 > _mForm.x2)
                {
                    x = (int)_mForm.x2;
                }

                if (_mForm.y1 < _mForm.y2)
                {
                    y = (int)_mForm.y1;
                }
                else if (_mForm.y1 > _mForm.y2)
                {
                    y = (int)_mForm.y2;
                }

                //Draw the rectangle
                Rectangle rect = new Rectangle(x, y, w, h);

                Graphics g = Graphics.FromImage((System.Drawing.Image)image);

                g.FillRectangle(new SolidBrush(Color.FromArgb
                                                   (60, 184, 184, 0)), rect); //Draws a semi-transparent rectangle using alpha blending

                g.Dispose();
                return;
            }

            if (_mForm.videoCropHeight == -1)
            {
                //Set or reset video cropping so that nothing gets trimmed out
                _mForm.videoCropHeight = image.Height;
                _mForm.videoCropWidth  = image.Width;
            }

            //Crop the bitmap
            Bitmap tmpImage0 = image.Clone(new Rectangle(_mForm.videoCropX,
                                                         _mForm.videoCropY, _mForm.videoCropWidth, _mForm.videoCropHeight),
                                           System.Drawing.Imaging.PixelFormat.Format24bppRgb);

            //Resize the bitmap
            AForge.Imaging.Filters.Resize resize = new Resize(image.Width / _mForm.resizeFactor,
                                                              image.Height / _mForm.resizeFactor, InterpolationMethod.NearestNeighbor);

            Bitmap tmpImage1 = resize.Apply(tmpImage0);

            //Get width and height of Bitmap
            imageWidth  = tmpImage1.Width;
            imageHeight = tmpImage1.Height;

            tmpImage0.Dispose();

            Utility.UnsafeBitmap uBitmap = new Utility.UnsafeBitmap(tmpImage1);

            //Locate the brightest pixel in the bitmap

            bool brightnessFound = false;

            uBitmap.LockBitmap();

            float brightest = 0;
            int   xPos = 0, yPos = 0;

            for (int y = 0; y < imageHeight; y += 1)
            {
                for (int x = 0; x < imageWidth; x += 1)
                {
                    byte red, green, blue;
                    red   = uBitmap.GetPixel(x, y).red;
                    green = uBitmap.GetPixel(x, y).green;
                    blue  = uBitmap.GetPixel(x, y).blue;

                    float brightness = (299 * red + 587 * green + 114 * blue) / 1000;

                    if (brightness > _mForm.threshold)
                    {
                        brightest = brightness;
                        xPos      = x;
                        yPos      = y;

                        brightnessFound = true;
                    } // (brightest > _mForm.threshold)
                }     // x loop
            }         // y loop

            if (brightnessFound == true)
            {
                uBitmap.UnlockBitmap();

                //Encircle the brightest pixel
                Graphics dc = Graphics.FromImage(tmpImage1);
                Pen      p  = new Pen(Color.LimeGreen, 1);

                dc.DrawEllipse(p, xPos - 5, yPos - 5, 10, 10);

                //Show the x and y coordinates of the brightest pixel
                dc.DrawString("X: " + xPos.ToString() + ", Y: " +
                              yPos.ToString(), new Font("Verdana", 7, FontStyle.Regular),
                              Brushes.Yellow, new Point(xPos, yPos + 8));

                if (_mForm.controlMouse == true)
                {
                    ControlCursor(xPos, yPos, _mForm.enableClick); //Set cursor position
                }
                dc.Dispose();

                uBitmap.LockBitmap();
            }
            else
            {
                if (_mForm.enableClick == true && leftMouseButtonDown == true)
                {
                    //Generate a left mouse button click
                    mouse_event(MOUSEEVENTF_LEFTDOWN | MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
                    leftMouseButtonDown = false;
                }
            }

            uBitmap.UnlockBitmap();
            uBitmap.Bitmap.Dispose();

            image.Dispose();
            image = tmpImage1;
        }