none
Current video frame with Aforge RRS feed

  • Question

  • I am using Aforge. I added a zoom and pan function that seems to be working, but I want to save the frame with the zoom and pan added to it but I can't seem too:

    g.DrawImage(currentFrame, imagex, imagey, imgWidth, imgHeight);

    That line has the correct information for zoom and pan and it works. The problem is I need to save it as a bitmap and using it later, but nothing I tried works,

    I tried this:

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

    But the image was not correct. I know because I displace currentPic in a picture back.

    Can anyone help me?

    • Moved by CoolDadTx Monday, January 7, 2019 4:38 PM Winforms related
    • Moved by Stanly Fan Tuesday, January 8, 2019 7:06 AM 3rd Resources
    Sunday, January 6, 2019 11:44 PM

All replies

  • Andrew 

    I want to copy 

    g.DrawImage(currentFrame, imagex, imagey, imgWidth, imgHeight);

    this line displays the right fame to my video VideoSourcePlayer.

    I just do not know how to copy is as a bitmap.

    currentFrame is a bitmap.

    but I do not know how to save it when the modification from the DrawImage...

    Monday, January 7, 2019 12:34 AM
  • here:

     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;
    
                    // 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);
                                     newWide = (int)(imgWidth - imagex);
                                     newHeight = (int) (imgHeight - imagey);
    
                                // 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);
                                newWide = (int)(imgWidth - imagex);
                                newHeight = (int)(imgHeight - imagey);
                                // draw current frame
                                // g.DrawImage( frame, rect.X + 1, rect.Y + 1, rect.Width - 2, rect.Height - 2);
                            }
    
                            firstFrameNotProcessed = false;
                            currentPic = new Bitmap(currentFrame, newWide, newHeight);//   imgWidth, imgHeight);
                            Console.WriteLine("Outside the Else");
                        }
                        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( );
                 
                }
    
    
            }

    Monday, January 7, 2019 12:45 AM
  • ok  CopyFromScreen  copy the whole screen right ?

    I just need to copy what is in my videoSourcePlayer .

    how would I do that ?

    Monday, January 7, 2019 12:49 AM
  • I just tried this:

     // You need to start with the Bitmap first
                // The width,height should correspond to the width and height of the image you want to copy.
                Bitmap b = new Bitmap(videoSourcePlayer.Width, videoSourcePlayer.Height);
                // Create a System.Drawing.Graphics object from that
                Graphics bg = Graphics.FromImage(b);
                // Then this should be your ticket.  The first new Point() 
                // should correspond to the upper left corner of your video screen
                // The second new Point() should stay 0,0 (upper left corner of output bitmap)
                // The new Size(width,height) should correspond to the width and height of the image you want to copy.
                bg.CopyFromScreen(new System.Drawing.Point(0, 0), new System.Drawing.Point(0, 0), b.Size);
                // filename is where to save the bitmap to
                pictureBox1.Image = new Bitmap(videoSourcePlayer.Width, videoSourcePlayer.Height, bg);

    but my pictureBox1 was blank 

    Monday, January 7, 2019 12:54 AM
  • hi I tried the code above :

      void test_Screen()
            {
                // The width,height should correspond to the width and height of the image you want to copy.
                Bitmap b = new Bitmap(videoSourcePlayer.Width, videoSourcePlayer.Height);
                // Create a System.Drawing.Graphics object from that
                Graphics bg = Graphics.FromImage(b);
    
                bg.CopyFromScreen(new System.Drawing.Point(videoSourcePlayer.Location.X, videoSourcePlayer.Location.Y), new System.Drawing.Point(0, 0), b.Size);
                // This could also be written as:
                // bg.CopyFromScreen(videoSourcePlayer.Location, new System.Drawing.Point(0, 0), b.Size);
                // If you're setting the Image property of a PictureBox, you use the Bitmap instance, not the Graphics Instance tied to that Bitmap.
                // Bitmap being derivative of System.Drawing.Image
                pictureBox1.Image = b;
            }

    and it is not getting my whole picture.

    it get part of my video and part of other stuff.

    I have also tried this :

       private void CaptureScreen()
            {
                Graphics myGraphics = this.CreateGraphics();
                Size s = videoSourcePlayer.Size;
                memoryImage = new Bitmap(videoSourcePlayer.Width, videoSourcePlayer.Height, myGraphics);
                Graphics memoryGraphics = Graphics.FromImage(memoryImage);
                int x = videoSourcePlayer.Location.X + videoSourcePlayer.Width;
                int y = videoSourcePlayer.Location.Y + videoSourcePlayer.Height; 
                memoryGraphics.CopyFromScreen(videoSourcePlayer.Location.X, videoSourcePlayer.Location.Y,  x, y, s);
                pictureBox1.Image = memoryImage;
            }

    and it is not working too.

    Monday, January 7, 2019 3:34 AM
  • 
    Monday, January 7, 2019 4:28 AM
  • it gets the top of my forum  and only part of the video 

    Monday, January 7, 2019 4:29 AM
  • what is :

    vspScreenLoc.X, vspScreenLoc.Y,

    where do I get the values from ?

    Monday, January 7, 2019 4:55 AM
  • could you post the whole function ?

    Monday, January 7, 2019 4:55 AM
  • ok this is the code I just tried:

      void test_Screen()
            {
                // The width,height should correspond to the width and height of the image you want to copy.
                Bitmap b = new Bitmap(videoSourcePlayer.Width, videoSourcePlayer.Height);
                // Create a System.Drawing.Graphics object from that
                Graphics bg = Graphics.FromImage(b);
    
                System.Drawing.Point vspScreenLoc = videoSourcePlayer.PointToScreen(videoSourcePlayer.Location);
    
                bg.CopyFromScreen(new System.Drawing.Point(vspScreenLoc.X, vspScreenLoc.Y), new System.Drawing.Point(0, 0), b.Size);
    
               // bg.CopyFromScreen(new System.Drawing.Point(videoSourcePlayer.Location.X, videoSourcePlayer.Location.Y), new System.Drawing.Point(0, 0), b.Size);
                // This could also be written as:
                // bg.CopyFromScreen(videoSourcePlayer.Location, new System.Drawing.Point(0, 0), b.Size);
                // If you're setting the Image property of a PictureBox, you use the Bitmap instance, not the Graphics Instance tied to that Bitmap.
                // Bitmap being derivative of System.Drawing.Image
                pictureBox1.Image = b;
            }

    and this is how the picture looked :

    Monday, January 7, 2019 5:05 AM
  • I just tried it and it is not working..

    Monday, January 7, 2019 3:19 PM