Principales respuestas
C#, dibujo en windows forms se borra al minimizar pantalla

Pregunta
-
Buenos dias!
Espero me puedan ayudar, tengo el siguiente codigo para dibujar una funcion al hacer click a un boton, el problema es que me bota error cuando minimizo la ventana. El error es:
An unhandled exception of type 'System.ArgumentException' occurred in System.Drawing.dll
y me indica la siguiente linea como la culpable:
buff = new Bitmap(pole.Width, pole.Height);
Cual es el error? y como lo puedo corregir? gracias de antemano!
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace graficasfunciones { public partial class Grafico : Form { Random r = new Random(); Bitmap buff = null; int pio = 0; public Grafico() { InitializeComponent(); DoubleBuffered = true; } void draw() { if (buff == null) buff = new Bitmap(pole.Width, pole.Height); Graphics g = Graphics.FromImage(buff); Pen p = new Pen(Color.Red); for (double x = 0; x < 100; x = x + 0.01) { double y = Math.Sin(x); int xx = (int)(x * 50); int yy = 150 + (int)(y * 100); g.DrawEllipse(p, xx, yy, 1, 1); } } void redraw() { if (buff == null) draw(); Graphics g = Graphics.FromHwnd(pole.Handle); g.DrawImageUnscaled(buff, 0, 0); } private void clickboton(object sender, EventArgs e) { draw(); redraw(); pio = 1; } private void pole_Paint(object sender, PaintEventArgs e) { if (pio == 1) { redraw(); } } private void pole_Resize(object sender, EventArgs e) { if (pio == 1) { buff = null; draw(); redraw(); } } } }
Respuestas
-
¿Qué es "pole"? Presumo que debe ser el objeto sobre el que estás dibujando. Probablemente al minimizar la ventana sus propiedades Width y Height valgan cero, y por eso da un error el new Bitmap(0,0). Dentro de pole_Resize, utiliza la propiedad WindowState del formulario para ver si está minimizado, y en ese caso no intentes llamar a draw() y redraw() (al fin y al cabo, si está minimizada la pantalla no se puede ver el dibujo).
- Marcado como respuesta yadernik jueves, 13 de febrero de 2014 14:09
Todas las respuestas
-
¿Qué es "pole"? Presumo que debe ser el objeto sobre el que estás dibujando. Probablemente al minimizar la ventana sus propiedades Width y Height valgan cero, y por eso da un error el new Bitmap(0,0). Dentro de pole_Resize, utiliza la propiedad WindowState del formulario para ver si está minimizado, y en ese caso no intentes llamar a draw() y redraw() (al fin y al cabo, si está minimizada la pantalla no se puede ver el dibujo).
- Marcado como respuesta yadernik jueves, 13 de febrero de 2014 14:09
-