我正在使用Aforge的videoSourcePlayer。我有可以放大和缩小的代码,但是现在我想在缩放后进行平移。因此,我想发生的是,放大后,您可以单击一个位置,然后平移将您单击的位置放在屏幕中央。 这是我的代码:
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 );
// 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;
if (Panflag == true && zoomFactor >1)
{
width = (int)(this.Width* this.zoomFactor);
height = (int)(this.Height * this.zoomFactor);
x = (rect.Width < width) ? this.PanPosition.X * (-1) * (this.PanFactor - 1) : (rect.Width - width) / 2;
y = (rect.Height < height) ? this.PanPosition.Y * (-1) * (this.PanFactor - 1) : (rect.Height - height) / 2;
}
else
{
this.PanPosition.X =(int) x;
this.PanPosition.Y =(int) y;
this.PanFactor = this.zoomFactor;
}
Bitmap frame = ( convertedFrame != null ) ? convertedFrame : currentFrame;
if ( keepRatio )
{
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, x, y, width, height);
// g.DrawImage( frame, newRect.X + 1, newRect.Y + 1, newRect.Width - 2, newRect.Height - 2);
}
else
{
// draw current frame
g.DrawImage(currentFrame, x, y, width, height);
// draw current frame
// g.DrawImage( frame, 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( );
}
}
public void Pan(Point PanPoint)
{
this.PanPosition = PanPoint;
this.PanFactor = 1;
}
我遇到的问题是放大并尝试平移后,它没有将我单击的位置放在videoSourcePlayer的中央。