BufferedGraphics object will AutoScroll position
-
Hi guys, I've been working on a custom component which acts as a 'view' control, such that it manually paints cells for child items. The component can contain hundreds of child items, which means that I've had to utilise the
BufferedGraphics
object for maintaining the graphics, and reset it only when the component is resized or when the child items are changed. Each child items also owns it's ownBufferedGraphics
object to maintain the rendering of each item (since these items can contain anImage
in the graphics, it speeds up the painting considerably.) A problem surfaces when I start to take into account scrolling through the use ofAutoScroll
andAutoScrollPosition
. I've spent a good while trying to search for an answer to applying the scroll position offset to aBufferedGraphics
and I've yet yielded unsuccessful results. Several problems are standing in the way of the scrolling functionality: 1. Graphics.TranslateTransform() or Graphics.Transform = new Matrix() does not translate a BufferedGraphics.Render() operation. I believe this is namely because the Render() function just paints directly onto theGraphics
handle for the region it was allocated by. For example, the below does not work:override void OnPaint(PaintEventArgs e)
{
// code here which deals with allocating the buffer if it needs creating// translate by the auto scroll position e.Graphics.TranslateTransform(-AutoScrollPosition.X, -AutoScrollPosition.Y); // render the buffered graphics onto the graphics handle for the paint event this.bufferedGraphics.Render(e.Graphics);
}
2. Following the code in this example [^] results in many invalidations, which causes the graphics to lag to the point that it's infeasible for the component. My question is, is there a reliable method of being able to translate the buffered graphics of a
BufferedGraphics
object when painting onto aGraphics
handle? For instance, could I allocate myBufferedGraphics
as I do in point #1, and intrinsically invalidate the component upon scrolling and simply offset the buffered graphics depending on the position the user has scrolled to? If not, can anyone recommend a suitable repla -
Hi guys, I've been working on a custom component which acts as a 'view' control, such that it manually paints cells for child items. The component can contain hundreds of child items, which means that I've had to utilise the
BufferedGraphics
object for maintaining the graphics, and reset it only when the component is resized or when the child items are changed. Each child items also owns it's ownBufferedGraphics
object to maintain the rendering of each item (since these items can contain anImage
in the graphics, it speeds up the painting considerably.) A problem surfaces when I start to take into account scrolling through the use ofAutoScroll
andAutoScrollPosition
. I've spent a good while trying to search for an answer to applying the scroll position offset to aBufferedGraphics
and I've yet yielded unsuccessful results. Several problems are standing in the way of the scrolling functionality: 1. Graphics.TranslateTransform() or Graphics.Transform = new Matrix() does not translate a BufferedGraphics.Render() operation. I believe this is namely because the Render() function just paints directly onto theGraphics
handle for the region it was allocated by. For example, the below does not work:override void OnPaint(PaintEventArgs e)
{
// code here which deals with allocating the buffer if it needs creating// translate by the auto scroll position e.Graphics.TranslateTransform(-AutoScrollPosition.X, -AutoScrollPosition.Y); // render the buffered graphics onto the graphics handle for the paint event this.bufferedGraphics.Render(e.Graphics);
}
2. Following the code in this example [^] results in many invalidations, which causes the graphics to lag to the point that it's infeasible for the component. My question is, is there a reliable method of being able to translate the buffered graphics of a
BufferedGraphics
object when painting onto aGraphics
handle? For instance, could I allocate myBufferedGraphics
as I do in point #1, and intrinsically invalidate the component upon scrolling and simply offset the buffered graphics depending on the position the user has scrolled to? If not, can anyone recommend a suitable replaJust for anyone with a similar question in the future, I managed to resolve this quite simply. Upon the OnScroll event method, simply invalidate the
BufferedGraphics
object by disposing it, and reset it back to null. Invalidate the whole component, and when constructing the newBufferedGraphics
object, set thetargetRectangle
parameter'sX
andY
values to the respectiveAutoScrollPosition
values. Why this isn't mentioned elsewhere, I don't know.