Draw manipulated graphic into another graphic

我与影子孤独终老i 提交于 2019-12-24 00:19:26

问题


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?


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/9698444/draw-manipulated-graphic-into-another-graphic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!