将操纵的图形绘制到另一个图形中

我想把一个被操纵的图形绘制成另一个:

// 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); 

输出是头部img的背景img – 但是头部img没有旋转。

我错过了什么?

图形对象的Transform属性就是属性。 它不会采取任何操作,只会告诉图形对象应该如何绘制图像。

所以你想要做的是在你正在绘制的图形对象上设置Transform属性 – 在这种情况下它应该应用于你的gBackground对象,就像这样……

 gBackground.Transform = matrix; 

然后当你来回调用gBackground对象上的DrawImage方法时,它会考虑你已应用的Transform属性。

请记住,此属性更改将在所有后续DrawImage调用中保留,因此您可能需要重新设置或更改值,然后再进行绘制(如果您需要)


为了更清楚,你的最终代码应该是这样的……

 // 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); 

将转换应用于Graphics对象仅在您要使用该特定对象时才有用。 你没有对变量gHead做任何事情。 尝试将该转换应用于gBackground