将操纵的图形绘制到另一个图形中 [英] 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);
输出是带有头部img的背景img-但头部img没有旋转.
Output is the background img with the head img - but the head img is not rotated.
我想念什么?
推荐答案
图形对象的Transform
属性正是该属性.它不执行任何操作,仅告诉图形对象应该如何绘制图像.
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.
因此,您要做的是在要绘制的图形对象上设置Transform
属性-在这种情况下,应将其应用于gBackground
对象,就像这样...
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;
然后,当您要在gBackground
对象上调用DrawImage
方法时,它将考虑您已应用的Transform
属性.
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.
请记住,此属性更改将在所有随后的DrawImage
调用中持续存在,因此您可能需要在进行更多绘图之前重置它或更改值(如果需要)
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);
这篇关于将操纵的图形绘制到另一个图形中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!