Estoy dibujando un montón de clips ( trocitos de un bitmap) utilizando DrawImage. Es bastante lento, de hecho veo como se van 'pintando ' los clips:
foreach (Clip clip in clipGroup.Lista)
{
clip.Draw(g, x, y);
Rectangle rect = new Rectangle(x, y, clip.Rectangulo.Width, clip.Rectangulo.Height);
g.DrawRectangle(Pens.Red, rect);
}
¿Es esto normal? ¿tan lento es DrawImage?
No te puedo ayudar mucho porue nunca le he pillado el truco a pintar en forms,
pero quizas tendrias que usar una especie de doble buffer.
Googleando:
1.
//bitmap donde pintar
Bitmap BackBuffer = new Bitmap(this.ClientSize.Width,this.ClientSize.Height);
Graphics DrawingArea = Graphics.FromImage(BackBuffer);
//pintas lo que quieras
//hacemos un present
Graphics Viewable = Me.CreateGraphics();
Viewable.DrawImageUnscaled(BackBuffer, 0, 0);
2.
I have just noticed there's a Control.SetStyle method that automates double buffering in many situations
where you are drawing to a control - place this in the forms initialisation:
this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
this.UpdateStyles();
Gracias, ya utilizo el double buffering, pero en otro control. No es un tema de flickering, es un problema de velocidad al pintar.
No puedes poner más código? Con el trozo que has puesto es complicado ver donde está el problema :S
Ahí está todo el código. Me da la sensación de que lo que ocurre es que al utilizar DrawImage con un Source Rectangle y un DestRenctangle y aunque tengan el mismo Size, ya no utiliza BitBlt internamente y por eso se vuelve muy muy lento.
Comprobado:
Image image=(Image)bitmap.Bitmap;
/*
if (imagen == null)
{
Rectangle rect = new Rectangle(x, y, rectangulo.Width, rectangulo.Height);
Bitmap cropped = bitmap.Bitmap.Clone(rect, bitmap.Bitmap.PixelFormat);
imagen=(Image)cropped;
}
g.DrawImage(imagen, x, y);
*/
g.DrawImage(image, new Rectangle(x, y, rectangulo.Width, rectangulo.Height), rectangulo, GraphicsUnit.Pixel);
Si utilizo DrawIamge sin especificar el origen de la imagen, va rápido.
Porque no usas DrawImageUnscaled? O necesitas redimensionar la imagen?
Yo hice un proyecto hace tiempo que pintaba un tileset tirando de algo así, le echaré un vistazo a ver. Recuerdo que usaba matrices para posicionar cada tile en lugar de pasarle un Rect, pero no dispongo del código en este momento.
No obstante, el editor de tiles que estamos usando ahora usa lo mismo que tienes ahora.
Genial, porque es lento de.... >:(
Porque no pones el método enteritooo para que le podamos dar un vistazo? :p