B
Btb4198
Guest
I am using Aforge VideoSourcePlayer control. I added in a zoom and a pan and both are working fine. now I need to take a copy current Frame that haves the modification from the zoom and the pan and save it to a bitmap. The problem I am having it, when I use currentPic = new Bitmap(imgWidth, imgHeight, g); my image is always blank. How can I get my same my modified frame to currentPic?
I have tried doing currentPic = new Bitmap(imgWidth, imgHeight, g); but the image is blank. it should not be blank.
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 imgWidth = (int)(this.Width * zoomFactor);
int imgHeight = (int)(this.Height * zoomFactor);
if (RecalcImage && !Panflag)
{
imagex = (rect.Width < imgWidth) ? (zoomPosition.X * (-1) * (zoomFactor - 1)) : (rect.Width - imgWidth) / 2;
imagey = (rect.Height < imgHeight) ? (zoomPosition.Y * (-1) * (zoomFactor - 1)) : (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;
}
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, imagex, imagey, imgWidth, imgHeight);
// g.DrawImage( frame, newRect.X + 1, newRect.Y + 1, newRect.Width - 2, newRect.Height - 2);
}
else
{
// draw current frame
g.DrawImage(currentFrame, imagex, imagey, imgWidth, imgHeight);
// draw current frame
// g.DrawImage( frame, rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);
}
firstFrameNotProcessed = false;
currentPic = new Bitmap(imgWidth, imgHeight, g);
}
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( );
}
Continue reading...
I have tried doing currentPic = new Bitmap(imgWidth, imgHeight, g); but the image is blank. it should not be blank.
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 imgWidth = (int)(this.Width * zoomFactor);
int imgHeight = (int)(this.Height * zoomFactor);
if (RecalcImage && !Panflag)
{
imagex = (rect.Width < imgWidth) ? (zoomPosition.X * (-1) * (zoomFactor - 1)) : (rect.Width - imgWidth) / 2;
imagey = (rect.Height < imgHeight) ? (zoomPosition.Y * (-1) * (zoomFactor - 1)) : (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;
}
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, imagex, imagey, imgWidth, imgHeight);
// g.DrawImage( frame, newRect.X + 1, newRect.Y + 1, newRect.Width - 2, newRect.Height - 2);
}
else
{
// draw current frame
g.DrawImage(currentFrame, imagex, imagey, imgWidth, imgHeight);
// draw current frame
// g.DrawImage( frame, rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);
}
firstFrameNotProcessed = false;
currentPic = new Bitmap(imgWidth, imgHeight, g);
}
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( );
}
Continue reading...