none
How to save the picture on the picturebox and the control on the picturebox to a image file?

    Question

  • The picturebox has load a image(emf) ,and the image is stretched to the size of picturebox. Some controls(panel) are added to the picturebox.  
    Now I want to save them to a image file(emf).How to do this?
    That's my thought.

    private void exportEMF()
    {
    foreach (control c in picturebox.controls)
    {
         DrawImage???
    }
    
    }
    • Moved by CoolDadTxMVP Friday, November 06, 2009 2:35 PM WinForms related (From:.NET Base Class Library)
    Friday, November 06, 2009 2:34 AM

All replies

  • Hi,

    You can use the following code. It

            private void button1_Click(object sender, EventArgs e)
            {
                GetImageOfControl(pictureBox1).Save(@"C:\users\tamer\desktop\a.emf",ImageFormat.Emf);
            }
            public Image GetImageOfControl(Control c)
            {
                Rectangle rc = new Rectangle(this.PointToScreen(c.Location), c.Size);
                Image i = new Bitmap(rc.Width, rc.Height);
                Graphics g = Graphics.FromImage(i);
                g.CopyFromScreen(rc.Left, rc.Top, 0, 0, rc.Size);
                g.Dispose();
                return i;
            }
    Friday, November 06, 2009 3:34 AM
  • thank you.
    But there is a problem,the size of the picturebox is bigger than the size of the srceen,then I can't get the whole image.
    Friday, November 06, 2009 7:18 AM
  •   Bitmap bmp = new Bitmap(this.pictureBox1.image, this.pictureBox1.Size);
                foreach (Control c in this.pictureBox1.Controls)
                {
                    Rectangle rect = new Rectangle(c.Location, c.Size);
                    c.DrawToBitmap(bmp, rect);
                }
                bmp.Save(@"c:\a.bmp");

    It works.But the images of the controls in the picturebox are not very clear.Is there a better method?
    Friday, November 06, 2009 7:38 AM
  • you can use these two functions for that:

     

    private void handleFitToWindow(bool doFit)

    {

     

    /* Don't perform any operation if no image is loaded. */

     

    try

    {

     

    if (this.imgScan.Tag != null)

    {

     

    if (doFit) /* We're fitting it to the window, and centering it. */

    {

     

    /* Create a temporary Image.

    * Always work from the original image, stored in the Tag.

    */

     

    Image tempImage = (Image)this.imgScan.Tag;

     

    /* Calculate the dimensions necessary for an image to fit. */

     

    Size fitImageSize = this.getScaledImageDimensions(tempImage.Width, tempImage.Height, this.imgScan.Width, this.imgScan.Height);

     

    /* Create a new Bitmap from the original image with the new dimensions.

    * The constructor for the Bitmap object automatically scales as necessary.

    */

     

    Bitmap imgOutput = new Bitmap(tempImage, fitImageSize.Width, fitImageSize.Height);

     

    /* Clear any existing image in the PictureBox. */

     

    this.imgScan.Image = null;

     

    /* When fitting the image to the window, we want to keep it centered. */

     

    this.imgScan.SizeMode = PictureBoxSizeMode.CenterImage;

     

    /* Finally, set the Image property to point to the new, resized image. */

     

    this.imgScan.Image = imgOutput;

    }

     

    else /* Restore the image to its original size */

    {

     

    /* Clear any existing image int he PictureBox. */

     

    this.imgScan.Image = null;

     

    /* Set the resize more to Normal; this will place the image

    * in the upper left-hand corner, clipping the image as required.

    */

     

    this.imgScan.SizeMode = PictureBoxSizeMode.Normal;

     

    /* Finally, set the Image property to point to the original image. */

     

    this.imgScan.Image = (Image)this.imgScan.Tag;

    }

    }

    }

     

    catch (Exception ex)

    {

     

    MessageBox.Show(Convert.ToString(ex), "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    }

     

    private Size getScaledImageDimensions(int currentImageWidth,int currentImageHeight,int desiredImageWidth,int desiredImageHeight)

    {

     

    /* First, we must calculate a multiplier that will be used

    * to get the dimensions of the new, scaled image.

    */

     

    double scaleImageMultiplier = 0;

     

    /* This multiplier is defined as the ratio of the

    * Desired Dimension to the Current Dimension.

    * Specifically which dimension is used depends on the larger

    * dimension of the image, as this will be the constraining dimension

    * when we fit to the window.

    */

     

    /* Determine if Image is Portrait or Landscape. */

     

    if (currentImageHeight > currentImageWidth) /* Image is Portrait */

    {

     

    /* Calculate the multiplier based on the heights. */

     

    if (desiredImageHeight > desiredImageWidth)

    {

    scaleImageMultiplier = (

    double)desiredImageWidth / (double)currentImageWidth;

    }

     

    else

    {

    scaleImageMultiplier = (

    double)desiredImageHeight / (double)currentImageHeight;

    }

    }

     

    else /* Image is Landscape */

    {

     

    /* Calculate the multiplier based on the widths. */

     

    if (desiredImageHeight > desiredImageWidth)

    {

    scaleImageMultiplier = (

    double)desiredImageWidth / (double)currentImageWidth;

    }

     

    else

    {

    scaleImageMultiplier = (

    double)desiredImageHeight / (double)currentImageHeight;

    }

    }

     

    /* Generate and return the new scaled dimensions.

    * Essentially, we multiply each dimension of the original image

    * by the multiplier calculated above to yield the dimensions

    * of the scaled image. The scaled image can be larger or smaller

    * than the original.

    */

     

    return new Size((int)(currentImageWidth * scaleImageMultiplier),(int)(currentImageHeight * scaleImageMultiplier));

    }

    with regards


    Varun Kumar
    • Proposed as answer by varun007 Friday, November 06, 2009 10:13 AM
    • Unproposed as answer by ahking Monday, November 09, 2009 12:05 AM
    Friday, November 06, 2009 10:12 AM
  • How about the controls on the picturebox,the image should include the images of them.
    Monday, November 09, 2009 12:07 AM
  • Hi ahking,

    You can call the BitBlt API to capture a region of the screen and save to an image file. The thread below can give you some ideas, please pay attention to the method DrawToBitmap. You need to modify the code to adjust the capture size.
    http://social.msdn.microsoft.com/Forums/en/winforms/thread/5e964d29-85f8-4aa6-95f1-505f23bdbe32

    You can get more about BitBlt from:
    http://msdn.microsoft.com/en-us/library/dd183370(VS.85).aspx

    Regards,
    Aland Li


    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Tuesday, November 10, 2009 10:22 AM
  • The method of copyscreen is not  aplicable.
    the size of the picturebox is bigger than the size of the srceen,then I can't get the whole image on the picturebox.
    Wednesday, November 11, 2009 12:14 AM
  • Hi ahking,

    Do you mean only part of the PictureBox is shown on the screen?

    Regards,
    Aland Li
    Please mark the replies as answers if they help and unmark if they don't. This can be beneficial to other community members reading the thread.
    Wednesday, November 11, 2009 4:06 AM
  • Yes.
    Wednesday, November 11, 2009 5:34 AM