GDI+ Nested Graphics objects
With System.Drawing.Graphics
, how can I have "nested graphics objects".
The main purpose is to have several clipped regions.
This is somehow the thing I want to do:
The whole screen is a Graphics object
Red area is another Graphics inside it and clipped
Green area is another one, clipped
Inside Graphics objects can be anything NOT only DrawString
The code should look like this:
using (var bmp = new System.Drawing.Bitmap(200, 200))
using (var mainG = System.Drawing.Graphics.FromImage(bmp))
using (var redG = ???)
using (var greenG = ???)
{
redG.SetClip(new RectangleF(...));
greenG.SetClip(new RectangleF(...));
开发者_C百科
// fill redG and greenG
}
NOTE: the result should go to a meta file and be vector graphic, so creating bitmaps and placing them around the mainG is NOT an option.
Assuming it's okay for the two vector contexts to be seperate while they are drawn into, you can use System.Drawing.Imaging.Metafile to catch the vector operations and then combine them into the bigger context.. Something like this:
using (Graphics gRef = this.CreateGraphics())
{
IntPtr hdc = gRef.GetHdc();
using (System.Drawing.Imaging.Metafile mf =
new System.Drawing.Imaging.Metafile(hdc,
System.Drawing.Imaging.EmfType.EmfPlusDual))
{
gRef.ReleaseHdc();
using (Graphics redG = Graphics.FromImage(mf))
{
redG.SetClip(new RectangleF(...));
// .. draw on redG
}
// repeat for greenG
// .. save and or combine as desired
}
}
}
An alternative approach would be to study the Enhanced Metafile format (http://msdn.microsoft.com/en-us/library/cc230724.aspx) and try to reproduce clipping masks manually.
精彩评论