3

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?

Marc
  • 6,749
  • 9
  • 47
  • 78
  • What happens if you apply the transform to the gBackground object instead? `gBackground.Transform = matrix;` – musefan Mar 14 '12 at 08:54

2 Answers2

3

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);
musefan
  • 47,875
  • 21
  • 135
  • 185
  • But I don't want to rotate the background - I want to rotate the head? – Marc Mar 14 '12 at 09:03
  • @Marc: It doesn't rotate the background. It tells the graphics object that you are using for drawing that any subsequent images you draw will have a rotation applied. Try it and it should work. [See here for more information](http://www.c-sharpcorner.com/UploadFile/mahesh/Transformations0412192005054803AM/Transformations04.aspx) – musefan Mar 14 '12 at 09:05
  • Wow, yehah, it worked! Thanks a lot - so the matrix transformation is applied to all *following* `gBackground.Draw...` calls, right? – Marc Mar 14 '12 at 09:10
  • Ups, sorry, just read your previous comment. Ok, clear, thanks! – Marc Mar 14 '12 at 09:11
1

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.

tafa
  • 7,146
  • 3
  • 36
  • 40
  • But I don't want to rotate the background - I want to rotate the head? – Marc Mar 14 '12 at 09:02
  • Background is not going to be rotated. Anything drawn after you apply the transformation is going to be rotated, in your case it is the image `h`. – tafa Mar 14 '12 at 09:04
  • Thank you too, you're right as well, but I can only select one answer as right - and musefan wrote more, so I took his answer :) – Marc Mar 14 '12 at 09:13