AForge.NET

  :: AForge.NET Framework :: Articles :: Forums ::

Aforge.Controls.Videosourceplayer Zoom in and out

Forum to discuss AForge.NET Framework, its features, API, how-tos, etc.

Aforge.Controls.Videosourceplayer Zoom in and out

Postby wrojas » Thu Jul 08, 2010 4:41 pm

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
wrojas
 
Posts: 3
Joined: Wed Feb 17, 2010 7:03 pm

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby fox86 » Sun Jul 25, 2010 1:02 am

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?
fox86
 
Posts: 4
Joined: Sat Jul 24, 2010 11:59 pm

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby wrojas » Mon Aug 02, 2010 8:32 pm

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
wrojas
 
Posts: 3
Joined: Wed Feb 17, 2010 7:03 pm

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby rakeshry » Fri Dec 21, 2012 2:32 pm

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.
rakeshry
 
Posts: 1
Joined: Fri Dec 21, 2012 2:23 pm

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby Val » Fri Jan 25, 2013 12:22 pm

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..
Val
 
Posts: 3
Joined: Fri Jan 11, 2013 4:24 pm

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby btb4198 » Thu Jul 26, 2018 1:36 pm

how did you get access to the Aforge.Conntrols.VideoSourcePlayer class to change it ?
btb4198
 
Posts: 34
Joined: Fri May 18, 2018 2:31 am

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby andrew.kirillov » Thu Jul 26, 2018 9:47 pm

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 ?
With best regards,
Andrew


Interested in supporting AForge.NET Framework?
User avatar
andrew.kirillov
Site Admin, AForge.NET Developer
 
Posts: 3453
Joined: Fri Jan 23, 2009 9:12 am
Location: UK

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby btb4198 » Fri Jul 27, 2018 1:43 pm

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?
btb4198
 
Posts: 34
Joined: Fri May 18, 2018 2:31 am

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby btb4198 » Fri Jul 27, 2018 2:44 pm

ok I downloaded the code and I made the changes but when I tried to Build it,
I got 7 errors.
btb4198
 
Posts: 34
Joined: Fri May 18, 2018 2:31 am

Re: Aforge.Controls.Videosourceplayer Zoom in and out

Postby btb4198 » Fri Jul 27, 2018 3:04 pm

ok I think I got it to work
Last edited by btb4198 on Fri Jul 27, 2018 4:36 pm, edited 2 times in total.
btb4198
 
Posts: 34
Joined: Fri May 18, 2018 2:31 am



Next

Return to AForge.NET Framework