C# digital zoom

  • Thread starter Thread starter btb900
  • Start date Start date
B

btb900

Guest
So I coded a digital zoom, and the way it works right now is, if you click on an area in my video player, is will zoom in on that area where you click at, but if you are ready zoom in and you click on another area, it will zoom in but it will move the area you click at a little be away from the mouse. then if you click on that area again, it will zoom in but it will not move away from the mouse.

So anytime I zoom in on a new area the 1st time it work fine, but if I want to zoom in to a new area it does but it move that new area away from the mouse a little and I do not know why.

here is my code :

private void videoSourcePlayer_Click(object sender, EventArgs e)
{

MouseEventArgs arg = (MouseEventArgs)e;
float tempPointX;
float tempPointY;

tempPointX = ((arg.Location.X - videoSourcePlayer.imagex) / videoSourcePlayer.zoomFactor);
tempPointY = ((arg.Location.Y - videoSourcePlayer.imagey) / videoSourcePlayer.zoomFactor);

if (HandCheckBox.Checked)
{
return;

}
if (this.btZoomIn.Checked == true)
{
this.videoSourcePlayer.Panflag = false;
float zoom = this.videoSourcePlayer.zoomFactor * 1.5F;
if (zoom > 15)
{
zoom = 15;
return;

}//maximum value is 10
//minimum value is 0.2


this.videoSourcePlayer.Zoom(zoom, tempPointX, tempPointY);

}
else if (this.btZoomOut.Checked == true)
{
this.videoSourcePlayer.Panflag = false;
float zoom = this.videoSourcePlayer.zoomFactor / 1.5F;

this.videoSourcePlayer.Zoom(zoom, tempPointX, tempPointY);
}



if (btPan.Checked == true)
{
this.videoSourcePlayer.Pan(arg.Location);
this.videoSourcePlayer.Panflag = true;
}
videoSourcePlayer.RecalcImage = true;

}



public void Zoom(float zoomFactor, float zoomPointX, float zoomPointY)
{
this.zoomFactor = zoomFactor;
//zoomPosition = zoomPoint;
RecalcImage = true;
this.zoomPointX = zoomPointX;
this.zoomPointY = zoomPointY;
}




private void VideoSourcePlayer_Paint( object sender, PaintEventArgs e )
{
if ( !Visible )
{
return;
}
// is it required to update control's size/position
if ( ( needSizeUpdate ) || ( firstFrameNotProcessed ) )
{
UpdatePosition( );
needSizeUpdate = false;
}

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



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

if ( videoSource != null )
{
if ( ( currentFrame != null ) && ( lastMessage == null ) )
{
imgWidth = (int)(this.Width * zoomFactor);
imgHeight = (int)(this.Height * zoomFactor);
if (RecalcImage && !Panflag)
{
if (rect.Width < imgWidth)
imagex = (zoomPointX * (-1) * (zoomFactor - 1));
else
imagex = (rect.Width - imgWidth) / 2;

if (rect.Height < imgHeight)
imagey = (zoomPointY * (-1) * (zoomFactor - 1)) ;
else
imagey = (rect.Height - imgHeight) / 2;

RecalcImage = false;
}
if (RecalcImage && Panflag == true && zoomFactor >1)
{
// x, y are positions of the upper left of the image
// PanPosition is the position of the cursor within the ClientRectangle
// need to find PanPosition relative to the image and move that point to the center of the ClientRectangle


imagex -= (PanPosition.X - rect.Width / 2) ;
imagey -= (PanPosition.Y - rect.Height / 2) ;
if (imagex > 0)
imagex = 0;
imagey = (imagey > 0) ? 0 : imagey;

if (imagex + imgWidth < rect.Width)
imagex = rect.Width - imgWidth;

if (imagey + imgHeight < rect.Height)
imagey = rect.Height - imgHeight;


RecalcImage = false;
}



//keepRatio = true;
if ( keepRatio )
{

Bitmap frame = ( convertedFrame != null ) ? convertedFrame : currentFrame;
double ratio = (double) frame.Width / frame.Height;
Rectangle newRect = rect;

if ( rect.Width < rect.Height * ratio )
{
newRect.Height = (int) ( rect.Width / ratio );
}
else
{
newRect.Width = (int) ( rect.Height * ratio );
}

newRect.X = ( rect.Width - newRect.Width ) / 2;
newRect.Y = ( rect.Height - newRect.Height ) / 2;



g.DrawImage(currentFrame, imagex, imagey, imgWidth, imgHeight);
newWide = (int)(imgWidth - imagex);
newHeight = (int) (imgHeight - imagey);


}
else
{

// draw current frame

try
{
g.DrawImage(currentFrame, imagex, imagey, imgWidth, imgHeight);
newWide = (int)(imgWidth - imagex);
newHeight = (int)(imgHeight - imagey);

}
catch (Exception err)
{

}



}

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 ) );
currentPic = currentFrame;//new Bitmap(currentFrame, this.Width, this.Height);
Console.WriteLine("in side the Else");
drawBrush.Dispose( );
}

}

borderPen.Dispose( );

}


}


Again, it moved away from the mouse every time i pick a new location to zoom in... but not the very 1st times I zoom in. and if I keep clicking on the new area it will zoom in but not move. it is almost like it takes the new area and tries to move it to the center of the screen. but it does not do that for very 1st area you click on. so I really do not think it. and I cannot figure out why it's doing that.

can anyone help me ?


Continue reading...
 
Back
Top