Pregunta

Tengo una aplicación simple que muestra imágenes arrastradas. Me gustaría que la aplicación cambie su tamaño según la imagen que muestra. El siguiente código hace exactamente eso:

// Load the picture
Bitmap picture = new Bitmap(s);

// Calculate the size of the main form
this.Size = new Size(picture.Width + 
                       (this.pictureBox.Padding.All + 
                        this.pictureBox.Margin.All + 
                        this.tableLayoutPanel.Padding.All + 
                        this.tableLayoutPanel.Margin.All) * 2, 
                     picture.Height + 
                       (int)this.tableLayoutPanel.RowStyles[1].Height + 
                       (this.pictureBox.Padding.All + 
                        this.pictureBox.Margin.All + 
                        this.tableLayoutPanel.Padding.All + 
                        this.tableLayoutPanel.Margin.All) * 2);
 // Display the file.
 this.pictureBox.Image = picture;

Creo que es bastante obvio donde me gustaría un poco de ayuda para mejorar esto. A medida que los formularios se vuelven más complicados, también lo haría el cálculo del tamaño apropiado. ¿Sugerencias?

¿Fue útil?

Solución

Podría calcular la diferencia de tamaño entre la imagen anterior y la nueva imagen, y luego simplemente ajustar el tamaño del formulario en esa cantidad ... siempre que todas las demás cosas en el formulario permanezcan del mismo tamaño.

Otros consejos

Puede echar un vistazo a las propiedades de Formularios:

  • Form.AutoSize
  • Form.AutoSizeMode

Esos, junto con la configuración de AutoSizeMode de PictureBox deberían darle el efecto que está buscando (sin tener que escribir ningún código).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top