Exemple #1
0
        /// <summary>
        /// This is the event handler for the Paint Event, try to draw as little as possible!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void SurfacePaint(object sender, PaintEventArgs e)
        {
            Graphics  targetGraphics = e.Graphics;
            Rectangle clipRectangle  = e.ClipRectangle;

            if (Rectangle.Empty.Equals(clipRectangle))
            {
                LOG.Debug("Empty cliprectangle??");
                return;
            }

            if (elements.hasIntersectingFilters(clipRectangle))
            {
                if (buffer != null)
                {
                    if (buffer.Width != Image.Width || buffer.Height != Image.Height || buffer.PixelFormat != Image.PixelFormat)
                    {
                        buffer.Dispose();
                        buffer = null;
                    }
                }
                if (buffer == null)
                {
                    buffer = new Bitmap(Image.Width, Image.Height, Image.PixelFormat);
                }
                // Elements might need the bitmap, so we copy the part we need
                using (Graphics graphics = Graphics.FromImage(buffer)) {
                    graphics.DrawImage(Image, clipRectangle, clipRectangle, GraphicsUnit.Pixel);
                    graphics.SetClip(targetGraphics);
                    elements.Draw(graphics, buffer, RenderMode.EDIT, clipRectangle);
                }
                targetGraphics.DrawImage(buffer, clipRectangle, clipRectangle, GraphicsUnit.Pixel);
            }
            else
            {
                // Only "simple" elements need to be redrawn, as the image is already drawn before getting the event we don't need the next line:
                //targetGraphics.DrawImage(Image, clipRectangle, clipRectangle, GraphicsUnit.Pixel);
                elements.Draw(targetGraphics, null, RenderMode.EDIT, clipRectangle);
            }
        }