gdi+ question

Z

ZQ

Hi everyone!

I have a question about GDI+.

float deltaY = 0;

// the onPaint handler of my control
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TranslateTransform(0, deltaY);
GenerateGraphics(e.Graphics);
}

In an OnClick event, I change the value of deltaY, and invalidate the
control.
Everything works fine - the graphics is then moved upwards or downwards -
depending
what the deltaY is set to, but it's very slow.

So I figured I should use BufferedGraphics and do something like this:

BufferedGraphics buff;
float deltaY = 0;

....somewhere in the code...
InitializeBufferedGraphics(buff);

....elsewhere in the code...
GenerateGraphics(buff.Graphics);
// this draws strings and rectangles and stuff on the buffered graphics
// this happens only when graphics needs to be changed, and not in the
OnPaint handler like it used
// to be earlier

// the onPaint handler of my control
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TranslateTransform(0, deltaY);
buff.Render(e.Graphics);
}

However, the TranslateTransform has absolutely no effect on buff.Render. -
the graphics rendered
from bufferedGraphics is not moved anywhere.

Is there a way to render the BufferedGraphics object over the OnPaint's
Graphics object, but translated?
 
J

Jianwei Sun

see inline.
Hi everyone!

I have a question about GDI+.

float deltaY = 0;

// the onPaint handler of my control
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TranslateTransform(0, deltaY);
GenerateGraphics(e.Graphics);
}

In an OnClick event, I change the value of deltaY, and invalidate the
control.
Everything works fine - the graphics is then moved upwards or downwards -
depending
what the deltaY is set to, but it's very slow.

So I figured I should use BufferedGraphics and do something like this:

BufferedGraphics buff;
float deltaY = 0;

...somewhere in the code...
InitializeBufferedGraphics(buff);

...elsewhere in the code...
GenerateGraphics(buff.Graphics);
// this draws strings and rectangles and stuff on the buffered graphics
// this happens only when graphics needs to be changed, and not in the
OnPaint handler like it used
// to be earlier

// the onPaint handler of my control
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TranslateTransform(0, deltaY);
[ you probably need to the transfer on the BufferedGraphics
buff.Graphics.TranslateTransform(0, deltaY);
 
Z

ZQ

Thanks for your response!

I considered that, and buff.Graphics.TranslateTransform(0, deltaY) has no
effect whatsoever.
TranslateTransform does it's transformations only on things that are drawn
AFTER it is called.

In this case:
1. a lot of things are drawn to buff.Graphics at time X
2. inside OnPaint(), buff.Graphics is rendered to the e.Graphics (everything
shows up on screen ok)
3. when user clicks a button only deltaY changes, nothing new is drawn on
buff.Graphics -- therefore, if I call the
buff.Graphics.TranslateTransform(0, deltaY) now, it won't apply to anything
because nothing is drawn after that
4. i call this.Invlidate() to refresh the control and display the translated
drawings
5. inside OnPaint(), buff.Graphics is rendered to the e.Graphics (it is
supposed to be on a new location, but it seems impossible)

I expected it to be normal that e.Graphics.translateTransform (...) would
affect everything that is rendered to e.Graphics from bufferedGraphics, but
that doesn't seem to be right.

It seems to me all I can do is optimize the GenerateGraphics method.


Jianwei Sun said:
see inline.
Hi everyone!

I have a question about GDI+.

float deltaY = 0;

// the onPaint handler of my control
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TranslateTransform(0, deltaY);
GenerateGraphics(e.Graphics);
}

In an OnClick event, I change the value of deltaY, and invalidate the
control.
Everything works fine - the graphics is then moved upwards or downwards -
depending
what the deltaY is set to, but it's very slow.

So I figured I should use BufferedGraphics and do something like this:

BufferedGraphics buff;
float deltaY = 0;

...somewhere in the code...
InitializeBufferedGraphics(buff);

...elsewhere in the code...
GenerateGraphics(buff.Graphics);
// this draws strings and rectangles and stuff on the buffered graphics
// this happens only when graphics needs to be changed, and not in the
OnPaint handler like it used
// to be earlier

// the onPaint handler of my control
protected override void OnPaint(PaintEventArgs e)
{
e.Graphics.TranslateTransform(0, deltaY);
[ you probably need to the transfer on the BufferedGraphics
buff.Graphics.TranslateTransform(0, deltaY);
buff.Render(e.Graphics);
}

However, the TranslateTransform has absolutely no effect on
buff.Render. - the graphics rendered
from bufferedGraphics is not moved anywhere.

Is there a way to render the BufferedGraphics object over the OnPaint's
Graphics object, but translated?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top