2012-02-21 38 views
7

Estoy diseñando un visor de imágenes simple con la capacidad de hacer un procesamiento de imagen básico. Por el momento tengo el problema de mantener el PictureBox centrado dentro de un TabPage todo el tiempo, así como mantener el ancho y el tamaño del cuadro de imagen igual que la imagen que muestra. Hasta ahora no he tenido éxito.Mantener un PictureBox centrado dentro de un contenedor

Tengo el siguiente código que llamo en el constructor de formulario para colocarlo en el centro. funciona la primera vez para centrar el cuadro de imagen:

private void SetPictureBoxOriginalSizeAndLocation(bool makeImageNull = false, DockStyle dockStyle = DockStyle.None) 
{ 
    if (makeImageNull) picBoxView.Image = null; 
    picBoxView.Dock = dockStyle; 

    var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width/2)/2); 
    var yPoint = tabImageView.Location.Y; 

    var width = tabImageView.Width/2; 
    var height = (tabImageView.Height/2) - toolStripImageView.Height; 

    if (picBoxView.Image == null) return; 

    //Resize image according to width 
    picBoxView.Image = ImageMethods.ResizeImage(picBoxView.Image.Tag.ToString(), width, height, false); 

    picBoxView.Location = new Point(xPoint, yPoint); 
    picBoxView.Width = width; 
    picBoxView.Height = height; 
} 

Pero no cambia el tamaño del cuadro de imagen a su imagen (se puede ver la parte en negro que está de nuevo color para el control PictureBox):

IT is ok the first time

el problema está empeorando en cuanto me cambio el tamaño de la forma, la posición de cuadro de imagen se va a arriba:

Form resized

Llamo también al código anterior en el evento de cambio de tamaño del formulario, no tengo idea de por qué funciona cuando se inicia la aplicación. Sería bueno que alguien me diga de qué propiedades debería ocuparme para obtener una imagen bien centrada que siempre es tan grande como su imagen.

+0

Intenta configurar Dock para completar y establecer la imagen como imagen de fondo. BackgroundImageLayout se puede establecer en Center. Y BackGroundColor to Transparent –

+0

¿El tamaño de 'picturebox.Image' es igual al tamaño de 'picturebox'? porque necesito trabajar en los píxeles de la imagen, no sé si el cuadro de imagen es más grande que la imagen, la imagen en la que estoy trabajando será el mismo cuadro de imagen ?! suena estúpido, lo sé! –

+0

, pero creo que tendré problemas si quiero acercar la imagen. –

Respuesta

14

Es bastante fácil si usted acaba de establecer el estilo Anchor incomparables:

picBoxView = new PictureBox(); 
picBoxView.SizeMode = PictureBoxSizeMode.AutoSize; 
picBoxView.Anchor = AnchorStyles.None; 
tabImageView.Controls.Add(picBoxView); 
CenterPictureBox(picBoxView, myImage); 

A continuación, sólo centrar la PictureBox inicialmente cada vez que cambie la imagen de la PictureBox:

private void CenterPictureBox(PictureBox picBox, Bitmap picImage) { 
    picBox.Image = picImage; 
    picBox.Location = new Point((picBox.Parent.ClientSize.Width/2) - (picImage.Width/2), 
           (picBox.Parent.ClientSize.Height/2) - (picImage.Height/2)); 
    picBox.Refresh(); 
} 

Tener Anchor = None centavo er el control PictureBox siempre que el contenedor primario se cambie de tamaño porque "no está" anclado a las ubicaciones izquierda y superior predeterminadas.

1

Givien un Form con TabControl, que tiene Dock conjunto de Fill, a continuación, mantendrá su PictureBox en el centro. También establece PictureBox tamaño de Bitmap tamaño:

public partial class Form1 : Form 
    { 
     Bitmap b = new Bitmap(320, 200); 
     public Form1() 
     { 
      InitializeComponent(); 
      CenterTheBox(); 
     } 

     private void Form1_Resize(object sender, EventArgs e) 
     { 
      CenterTheBox(); 
     } 

     void CenterTheBox() 
     { 
      pictureBox1.Size = b.Size; 
      var left = (tabPage1.ClientRectangle.Width - pictureBox1.ClientRectangle.Width)/2; 
      var top = (tabPage1.ClientRectangle.Height - pictureBox1.ClientRectangle.Height)/2; 
      pictureBox1.Location = new Point(tabPage1.ClientRectangle.Location.X + left, tabPage1.ClientRectangle.Location.Y + top); 

     } 
    } 
1

Creo que su problema se encuentra aquí

var xPoint = tabImageView.Location.X + ((splitContainer.Panel2.Width/2)/2); 
var yPoint = tabImageView.Location.Y; 

var width = tabImageView.Width/2; 
var height = (tabImageView.Height/2) - toolStripImageView.Height; 

ypoint se establece alwways a tabImageView Y, aunque incluya que se debe establecer en

tabImageView.Location.Y + (tabImageView.Size.Height - picBoxView.Size.Height)/2 

debería ser casi lo mismo con xPoint

tabImageView.Location.X + (tabImageView.Size.Width - picBoxView.Size.Width)/2 

y

width = picBoxView.Image.Width; 
height = picBoxView.Image.Height; 
Cuestiones relacionadas