Draw graphics to other graphics

I want to draw graphics to another:

// I have two graphics:
var gHead = Graphics.FromImage(h);
var gBackground = Graphics.FromImage(b);

// Transform the first one
var matrix = new Matrix();
matrix.Rotate(30);
gHead.Transform = matrix;

// Write the first in the second
gBackground.DrawImage(h, 200, 0, 170, 170);

      

The output is an img background with an img head - but the img head is not rotating.

What am I missing?

+3


source to share


2 answers


This property is a Transform

property of the graphics object. It takes no action, but only tells the graphic object how it should draw images.

So you want to set a property Transform

on the graphics object you are drawing on - in which case it should be applied to your object gBackground

, for example ...

gBackground.Transform = matrix;

      

then when you come to call a method of an DrawImage

object gBackground

it will take into account the property Transform

that you applied.



Remember that this property change will persist through all subsequent calls DrawImage

, so you may need to reset or change the value before doing another drawing (if you need to)


To be more clear, your final code should look like this:

// Just need one graphics
var gBackground = Graphics.FromImage(b);

// Apply transform to object to draw on
var matrix = new Matrix();
matrix.Rotate(30);
gBackground.Transform = matrix;

// Write the first in the second
gBackground.DrawImage(h, 200, 0, 170, 170);

      

+3


source


Applying a transform to an object Graphics

is useful if you intend to use that particular object. You are not doing anything with the variable gHead

. Try applying this transformation to gBackground

.



+1


source







All Articles