Marc
Marc

Reputation: 6771

Draw manipulated graphic into another graphic

I want to draw a manipulated graphic into 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);

Output is the background img with the head img - but the head img is not rotated.

What am I missing?

Upvotes: 3

Views: 1174

Answers (2)

musefan
musefan

Reputation: 48415

The Transform property of a graphics object is exactly that, a property. It does not take any action but only tells the graphics object how it should draw images.

So what you want to do is set the Transform property on the graphics object that you are drawing onto - in this case it should be applied to your gBackground object, like so...

gBackground.Transform = matrix;

then when you come round to calling the DrawImage method on the gBackground object, it will take into account the Transform property that you have applied.

Keep in mind that this property change will persist through all subsequent DrawImage calls so you may need to reset it or change the value before doing any more drawing (if you even need to)


To be extra 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);

Upvotes: 3

tafa
tafa

Reputation: 7316

Applying a transformation to a Graphics object is only useful, if you are going to use that specific object. You are not doing anything with the variable gHead. Try applying that transformation to gBackground.

Upvotes: 1

Related Questions