In my code, let's say I have the PaintObject(Graphics g)
. In some other function, I want to call the PaintObject
function to draw something at an offset, instead of drawing it at (0,0).
I know that in Java, I could use the Graphics.create(x, y, width, height)
function to create a copy of my graphics object which I could use, which would draw within those bounds of the original graphics. Is there a way to do something similarly in C#?
Just to give you an example of what my code could look like:
class MyClass : UserControl {
void PaintObject(Graphics g) {
// Example: draw 10x10 rectangle
g.DrawRectangle(new Pen(Color.Black), 0, 0, 10, 10);
}
protected override void OnPaint(PaintEventArgs e) {
Graphics g = e.Graphics;
// TODO: Paint object from PaintObject() at offset (50, 50)
}
}
Set a transformation on the Graphics
object:
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
Matrix transformation = new Matrix();
transformation.Translate(50, 50);
g.Transform = transformation;
}
or
protected override void OnPaint(PaintEventArgs e)
{
Graphics g = e.Graphics;
g.TranslateTransform(50, 50);
}
Use the Graphics
method
public void TranslateTransform(float dx, float dy)
g.TranslateTransform(dx, dy);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With