locked
How set the image to PictureBox? RRS feed

  • Question

  • Hi,

    I am working with c# windows application.In my form i have one PictureBox and One button.If i click on the button i am giving the image to pictureBox and i am giving the anchore = Bottom, Right and SizeMode=AutoSize.My problem is if i selected the small image the the image is going to up that means it is not align to Bottom, Right .

    If i selected any image that must be Bottom, Right (i.e the picture is align to 2 pix form right and 2 pix from buttom)

    My button click code is

    private void button1_Click(object sender, EventArgs e)
    {
    OpenFileDialog dlg = new OpenFileDialog();
    dlg.AddExtension = true;
    dlg.CheckFileExists = true;
    dlg.CheckPathExists = true;
    dlg.Filter ="Image Files"+ " (*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp)|*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp";
    dlg.ShowDialog();
    if (dlg.FileName != "")
    {
    Image img = null;
    Image thumbnailImage = null;

    img = Image.FromFile(dlg.FileName, true);
    pictureBox1.Image = img;
    }
    }

    Any one give me the Answer plz...It is very urgent to me

    Regards,
    Venakt.


    venakt
    Wednesday, January 7, 2009 4:14 AM

Answers

  • No, this is not a bug. It is my failure to read carefully.
     
    See the Remarks section of the Control.Anchor documentation. When the form is resized the PictureBox will retain its initial position relative to the bottom and right edges of the form.

    Bear in mind that the Image within the PictureBox will still be aligned to at top left of the PictureBox. If the size of the Image varies with respect to the size of the PictureBox, it will appear as if the picture has moved. So when a new picture is selected, you would need to resize the PictureBox to fit the picture.

    Also, shrinking or increasing the size of the PictureBox will not affect the PictureBox's Location property. So after resizing the PictureBox, you would then need to move it to a new calculated Location.

    Once you have resized and moved the PictureBox, the Anchor property will keep it in the correct relative position as the form is resized.
    • Proposed as answer by mexil Thursday, January 8, 2009 1:32 AM
    • Marked as answer by Zhi-Xin Ye Monday, January 12, 2009 11:51 AM
    Thursday, January 8, 2009 1:32 AM
  • OK, here's some code which would allow you to use the Image and Anchor properties:

            private void button1_Click(object sender, EventArgs e)  
            {  
                OpenFileDialog dlg = new OpenFileDialog();  
                dlg.AddExtension = true;  
                dlg.CheckFileExists = true;  
                dlg.CheckPathExists = true;  
                dlg.Filter = "Image Files" + " (*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp)|*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp";  
                dlg.ShowDialog();  
                if (dlg.FileName != "")  
                {  
                    Image img = null;  
                    Image thumbnailImage = null;  
                    img = Image.FromFile(dlg.FileName, true);  
     
                    pictureBox1.Image = img;  
     
                    // Store the PictureBox's relative position before resizing it.  
                    int rightMargin = pictureBox1.Parent.ClientSize.Width -  
                        (pictureBox1.Left + pictureBox1.Width);  
                    int bottomMargin = pictureBox1.Parent.ClientSize.Height -  
                        (pictureBox1.Top + pictureBox1.Height);  
     
                    // You could resize the PictureBox to fit the Image like this.  
                    // It depends on what you want.  
     
                    if (img.Width > pictureBox1.Parent.ClientSize.Width)  
                    {  
                        pictureBox1.Width = pictureBox1.Parent.ClientSize.Width;  
                    }  
                    else 
                    {  
                        pictureBox1.Width = img.Width + pictureBox1.Width - pictureBox1.ClientSize.Width;  
                    }  
     
                    if (img.Height > pictureBox1.Parent.ClientSize.Height)  
                    {  
                        pictureBox1.Height = pictureBox1.Parent.ClientSize.Height;  
                    }  
                    else 
                    {  
                        pictureBox1.Height = img.Height + pictureBox1.Height - pictureBox1.ClientSize.Height;  
                    }  
     
                    // Now move the PictureBox to its new Location  
                    pictureBox1.Left = pictureBox1.Parent.ClientSize.Width  
                        - pictureBox1.Width - rightMargin;  
                    pictureBox1.Top = pictureBox1.Parent.ClientSize.Height  
                        - pictureBox1.Height - bottomMargin;  
                }  
            } 


    When the form is resized, the Anchor property should keep it in the correct relative position.

    If your requirements include allowing the PictureBox to grow when more space becomes available on the form, it would be better not to use the Anchor property at all. Just resize the PictureBox when the picture changes, and also in the Form's OnResize event handler.

    What a mess, eh?

    • Edited by mexil Thursday, January 8, 2009 2:07 AM Corrected code comments
    • Marked as answer by Zhi-Xin Ye Monday, January 12, 2009 11:51 AM
    Thursday, January 8, 2009 2:04 AM

All replies

  • Anchor is not the correct property. It has to do with the relationship between the PictureBox and its parent (which is probably your form). Actually, I don't see any property which allows you to directly set the alignment of PictureBox.Image.

    However, if you want to center an image in a PictureBox, you can do it by drawing the image yourself with a Graphics object.

        private void button1_Click(object sender, EventArgs e) 
        { 
            OpenFileDialog dlg = new OpenFileDialog(); 
            dlg.AddExtension = true
            dlg.CheckFileExists = true
            dlg.CheckPathExists = true
            dlg.Filter ="Image Files"" (*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp)|*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp"
            dlg.ShowDialog(); 
            if (dlg.FileName != ""
            { 
                Image img = null
                Image thumbnailImage = null
                img = Image.FromFile(dlg.FileName, true); 
                 
                int x = (pictureBox1.ClientSize.Width - img.Width) / 2; 
                int y = (pictureBox1.ClientSize.Height - img.Height) / 2; 
                         
                Graphics g = pictureBox1.CreateGraphics(); 
                g.DrawImageUnscaled(img, x, y); 
            } 
        } 

    • Edited by mexil Wednesday, January 7, 2009 6:21 AM Code formatting
    Wednesday, January 7, 2009 6:20 AM
  • Correction -- to draw the image at bottom right (as you need) calculate the offset as follows:

                // Calculate the offsets to align at bottom right 
                int x = pictureBox1.ClientSize.Width - img.Width; 
                int y = pictureBox1.ClientSize.Height - img.Height; 



    Wednesday, January 7, 2009 6:27 AM
  • For the baove code where i can set the image to pictureBox

    Regards,
    venkat.

    venakt
    Wednesday, January 7, 2009 8:38 AM
  • You don't. If you set the PictureBox.Image property, I don't think you can control its location within the PictureBox.

    That's why I suggested using a Graphics object.
    Wednesday, January 7, 2009 10:16 PM
  • Maybe I misunderstood the intended use of a PictureBox. Perhaps the answer is to resize the PictureBox to fit either the image, or the parent control's ClientSize, whichever is smaller. You could then use the Anchor property to place the correctly sized PictureBox at bottom right.

    I will try this and let you know.
    Thursday, January 8, 2009 12:25 AM
  • No, that doesn't work -- and neither does MSDN's Control.Anchor example. This could be a bug.
    Thursday, January 8, 2009 12:42 AM
  • No, this is not a bug. It is my failure to read carefully.
     
    See the Remarks section of the Control.Anchor documentation. When the form is resized the PictureBox will retain its initial position relative to the bottom and right edges of the form.

    Bear in mind that the Image within the PictureBox will still be aligned to at top left of the PictureBox. If the size of the Image varies with respect to the size of the PictureBox, it will appear as if the picture has moved. So when a new picture is selected, you would need to resize the PictureBox to fit the picture.

    Also, shrinking or increasing the size of the PictureBox will not affect the PictureBox's Location property. So after resizing the PictureBox, you would then need to move it to a new calculated Location.

    Once you have resized and moved the PictureBox, the Anchor property will keep it in the correct relative position as the form is resized.
    • Proposed as answer by mexil Thursday, January 8, 2009 1:32 AM
    • Marked as answer by Zhi-Xin Ye Monday, January 12, 2009 11:51 AM
    Thursday, January 8, 2009 1:32 AM
  • OK, here's some code which would allow you to use the Image and Anchor properties:

            private void button1_Click(object sender, EventArgs e)  
            {  
                OpenFileDialog dlg = new OpenFileDialog();  
                dlg.AddExtension = true;  
                dlg.CheckFileExists = true;  
                dlg.CheckPathExists = true;  
                dlg.Filter = "Image Files" + " (*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp)|*.gif;*.jpg;*.jpeg;*.tif;*.png;*.bmp";  
                dlg.ShowDialog();  
                if (dlg.FileName != "")  
                {  
                    Image img = null;  
                    Image thumbnailImage = null;  
                    img = Image.FromFile(dlg.FileName, true);  
     
                    pictureBox1.Image = img;  
     
                    // Store the PictureBox's relative position before resizing it.  
                    int rightMargin = pictureBox1.Parent.ClientSize.Width -  
                        (pictureBox1.Left + pictureBox1.Width);  
                    int bottomMargin = pictureBox1.Parent.ClientSize.Height -  
                        (pictureBox1.Top + pictureBox1.Height);  
     
                    // You could resize the PictureBox to fit the Image like this.  
                    // It depends on what you want.  
     
                    if (img.Width > pictureBox1.Parent.ClientSize.Width)  
                    {  
                        pictureBox1.Width = pictureBox1.Parent.ClientSize.Width;  
                    }  
                    else 
                    {  
                        pictureBox1.Width = img.Width + pictureBox1.Width - pictureBox1.ClientSize.Width;  
                    }  
     
                    if (img.Height > pictureBox1.Parent.ClientSize.Height)  
                    {  
                        pictureBox1.Height = pictureBox1.Parent.ClientSize.Height;  
                    }  
                    else 
                    {  
                        pictureBox1.Height = img.Height + pictureBox1.Height - pictureBox1.ClientSize.Height;  
                    }  
     
                    // Now move the PictureBox to its new Location  
                    pictureBox1.Left = pictureBox1.Parent.ClientSize.Width  
                        - pictureBox1.Width - rightMargin;  
                    pictureBox1.Top = pictureBox1.Parent.ClientSize.Height  
                        - pictureBox1.Height - bottomMargin;  
                }  
            } 


    When the form is resized, the Anchor property should keep it in the correct relative position.

    If your requirements include allowing the PictureBox to grow when more space becomes available on the form, it would be better not to use the Anchor property at all. Just resize the PictureBox when the picture changes, and also in the Form's OnResize event handler.

    What a mess, eh?

    • Edited by mexil Thursday, January 8, 2009 2:07 AM Corrected code comments
    • Marked as answer by Zhi-Xin Ye Monday, January 12, 2009 11:51 AM
    Thursday, January 8, 2009 2:04 AM