Page 1 of 2

Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Thu Jul 08, 2010 4:41 pm
by wrojas
Hi,

I just wanted to share this piece of code with everybody.

I had the need to zoom in and out of the video player, but the class does not support this feature yet, and did not actually find anything in the forums, so i decided to implement it myself. If there is a better piece of code out there, please share it with all of us.

I am working with the version 2.1.0 of the framework.

To do this, I had to modify the Aforge.Conntrols.VideoSourcePlayer class as follows:

Firts, add two variables to store the zoom and zoom location
Code: Select all
public float zoomFactor = 1;
private Point zoomPosition = new Point(0, 0);


Then modify the callback for the OnPaint control --> VideoSourcePlayer_Paint
Code: Select all
  // Paint control
        private void VideoSourcePlayer_Paint( object sender, PaintEventArgs e )
        {
            // is it required to update control's size/position
            if ( ( needSizeUpdate ) || ( firstFrameNotProcessed ) )
            {
                UpdatePosition( );
                needSizeUpdate = false;
            }

            lock ( this )
            {
                Graphics  g = e.Graphics;
                Rectangle rect = this.ClientRectangle;
                Pen       borderPen = new Pen( borderColor, 1 );

                // draw rectangle
                g.DrawRectangle( borderPen, rect.X, rect.Y, rect.Width - 1, rect.Height - 1 );

                if ( videoSource != null )
                {
                    if ( ( currentFrame != null ) && ( lastMessage == null ) )
                    {
                        int width = (int)(this.Width * this.zoomFactor);
                        int height = (int)(this.Height * this.zoomFactor);
                        float x = (rect.Width < width) ? (this.zoomPosition.X * (-1) * (this.zoomFactor - 1)) : (rect.Width - width) / 2;
                        float y = (rect.Height < height) ? (this.zoomPosition.Y * (-1) * (this.zoomFactor - 1)): (rect.Height - height) / 2;

                        // draw current frame
                        g.DrawImage(currentFrame, x, y, width, height);

                        //Original line
                        //g.DrawImage(currentFrame, rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2 );
                       
                        firstFrameNotProcessed = false;
                    }
                    else
                    {
                        // create font and brush
                        SolidBrush drawBrush = new SolidBrush( this.ForeColor );

                        g.DrawString( ( lastMessage == null ) ? "Connecting ..." : lastMessage,
                            this.Font, drawBrush, new PointF( 5, 5 ) );

                        drawBrush.Dispose( );
                    }
                }

                borderPen.Dispose( );
            }
        }


And add a function that actually sets the two variables:
Code: Select all
     public void Zoom(float zoomFactor, Point zoomPoint)
        {
            this.zoomFactor = zoomFactor;
            this.zoomPosition = zoomPoint;
        }


There are also magnification limits, but i handle that on the form that contains the control itself.

Now, to zoom into the frame, just call the function from the form that contains the control as follows:
Note that i capture the mouse position with the MouseEventArgs
Code: Select all
private void videoSourcePlayer1_Click(object sender, EventArgs e)
{
   
            MouseEventArgs arg = (MouseEventArgs)e;
            if (this.btZoomIn.Checked == true)
            {
                float zoom = this.videoSourcePlayer1.zoomFactor * 2;
                if (zoom > 10) zoom = 10F;     //maximum value is 10
                //minimum value is 0.2
                this.videoSourcePlayer1.Zoom(zoom, arg.Location);
            }
            else if (this.btZoomOut.Checked == true)
            {
                float zoom = this.videoSourcePlayer1.zoomFactor / 2;
                if (zoom < 0.2) zoom = 0.2F;     //minimum value is 0.2
                this.videoSourcePlayer1.Zoom(zoom, arg.Location);
            }
}


Note that in my code, I also have 2 buttons, one to zoom in and another one to zoom out and they get checked when clicked (only one checked at the same time).
When the mouse enters and leaves the video source control the mouse pointer gets sets with a zoom_in/ zoom_out pic and defualt pointer accordingly, which is gives the customer some kind of feed back on what he is trying to do.

Code: Select all
   private void videoSourcePlayer1_MouseEnter(object sender, EventArgs e)
        {
           
            if (this.btZoomIn.Checked == true)
            {
                IntPtr ptr = Resources.zoom_in.GetHicon();
                Cursor c = new Cursor(ptr);
                this.Cursor = c;
            }
            else if (this.btZoomOut.Checked == true)
            {
                IntPtr ptr = zoom_out.GetHicon();
                Cursor c = new Cursor(ptr);
                this.Cursor = c;
            }

        }

private void videoSourcePlayer1_MouseLeave(object sender, EventArgs e)
        {
            this.Cursor = Cursors.Default;
        }



I will try to implement the pan function of it as well, i will post it when ready.

Hope it helps

Regards

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Sun Jul 25, 2010 1:02 am
by fox86
nice, i followed this and it worked great! did you ever develop this? on the camera Im using the video will zoom to its fullest in 3 large jumps, is it possible to make thisless, so that I can increment from lowest to highest zoom in maybe 8 clicks instead of 3?

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Mon Aug 02, 2010 8:32 pm
by wrojas
Hi fox86,

Glad I could help,

yes, i just today developed the pan function (finally i had time); i will test it today and probably post it tomorrow.
Regarding your question, all you need to do is modify the factor multiplier, and i guess since you you want to go from 3 jumps to 8jumps then do it as a factor of 3/8, something like this:

Code: Select all

private void videoSourcePlayer1_Click(object sender, EventArgs e)
{
   
            MouseEventArgs arg = (MouseEventArgs)e;
            if (this.btZoomIn.Checked == true)
            {
                //float zoom = this.videoSourcePlayer1.zoomFactor * 2;  <-- CHANGE THIS LINE TO SET YOUR DESIRED FACTOR
                float zoom = this.videoSourcePlayer1.zoomFactor * 3/8;  <-- THIS WOULD BE THE NEW VALUE
                if (zoom > 10) zoom = 10F;     //maximum value is 10
                //minimum value is 0.2
                this.videoSourcePlayer1.Zoom(zoom, arg.Location);
...
...
...


Regards

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Fri Dec 21, 2012 2:32 pm
by rakeshry
Hi wrojas,

Thanks for your post on Zoom-In / Zoom-out functionality. It seems that you have implemented the pan functionality also. Is it possible for you to share the code for pan functionality. If yes, please do so. Thanks.

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Fri Jan 25, 2013 12:22 pm
by Val
Hi all,

I add the zoom code and it works perfectly, just a very small contribution..

Since I am using a very slow processor, initially the program generated the exception "object in use elsewhere"..
To solve the problem I add also the original instrucion "lock ( sync )" to the videosourceplayer control..
The result is a double lock as follow

private void VideoSourcePlayer_Paint(object sender, PaintEventArgs e)
{
//..code ...
lock (sync)
lock (this)
{
//.. code..
}

Hope this can be helpful..

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Thu Jul 26, 2018 1:36 pm
by btb4198
how did you get access to the Aforge.Conntrols.VideoSourcePlayer class to change it ?

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Thu Jul 26, 2018 9:47 pm
by andrew.kirillov
btb4198 wrote:how did you get access to the Aforge.Conntrols.VideoSourcePlayer class to change it ?


It is one of those silly questions, I believe ... The AForge.NET Framework is open source, is not it ?

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Fri Jul 27, 2018 1:43 pm
by btb4198
seeing how I am new to open source code, I do not think it is a silly question.
anyhow, so I download the class, editor and added to my project. do I then have to remove the using AForge.Video and using AForge.Video.DirectShow
will it still work with out them?

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Fri Jul 27, 2018 2:44 pm
by btb4198
ok I downloaded the code and I made the changes but when I tried to Build it,
I got 7 errors.

Re: Aforge.Controls.Videosourceplayer Zoom in and out

PostPosted: Fri Jul 27, 2018 3:04 pm
by btb4198
ok I think I got it to work