生成多个图形对象以并行使用

时间:2018-08-02 20:39:52

标签: c# gdi+ metafile

我正在尝试创建可用于绘制图元文件的图形对象,但我不确定执行此操作的最佳方法是什么。最终,图元文件将包含在Word文档中,我们希望它们在打印时看起来不错。

起初我有这样的东西:

var ms = new MemoryStream();
using (var mfG = System.Drawing.Graphics.FromHwndInternal(IntPtr.Zero))
using (Metafile mf = new Metafile(ms, mfG.GetHdc(), EmfType.EmfPlusOnly, "Chart"))
using (var g = System.Drawing.Graphics.FromImage(mf))
{
    // do some drawing 
}

这可行,但是我想并行处理多个图像,在这种情况下,由于它们都试图使用同一图形对象,因此会产生错误。

所以我尝试了(基于在线创建图形对象的一些建议):

using (var mfG = new System.Drawing.Printing.PrinterSettings().CreateMeasurementGraphics())

但是它仍然有相同的问题。显然,CreateMeasurementGraphics每次都会为您提供相同的图形对象。

我可以用lock包装代码,以使其他线程等待。也许我应该。但是,有没有更好的方法来生成独立的Graphics对象,这些对象会生成可打印的图元文件?

1 个答案:

答案 0 :(得分:0)

我不知道,我的答案是否对您有帮助,但是您无法从多个线程绘制到同一个Graphics对象。

您可以做什么:您可以使用链接的Graphics对象创建多个MetaFile,并可以在Parallel中绘制不同的文件。显然,每个线程必须使用其自己的MetaFile <-> Graphics二重奏。

最后,您可以使用单个Graphics对象和方法“ DrawImageUnscaled”来组合图像。

示例(将尝试将文件写入D:。您可能要更改目标位置):

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MetaFileDrawTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Pen[] p = new Pen[] { Pens.Black, Pens.Red, Pens.Green };
            // Draw to three MetaFiles in Parallel.
            Parallel.For(0, 3, i =>
            {
                using (var ms = new MemoryStream())
                {
                    using (var mfG = System.Drawing.Graphics.FromHwndInternal(IntPtr.Zero))
                    {
                        using (Metafile mf = new Metafile(ms, mfG.GetHdc(), EmfType.EmfPlusOnly, "Chart"))
                        {
                            using (var g = System.Drawing.Graphics.FromImage(mf))
                            {
                                // Draw a slightly different line each time to see a difference...
                                g.DrawLine(p[i], 10 * i, 10, 100, 100);
                            }

                            // Save the Metafile to the disc. (You might keep it in memory as well)
                            mf.Save("D:\\test" + i + ".wmf");
                        }
                    }
                }
            });

            // Draw the three metafiles to the form (just to see how they look like)
            for (int i = 0; i <= 2; i++)
            {
                e.Graphics.TranslateTransform(100 * (i + 1), 0);
                e.Graphics.DrawImageUnscaled(Image.FromFile("D:\\test" + i + ".wmf"), 0, 0);
                e.Graphics.ResetTransform();
            }

            // Create a file metafile to draw all single images to (one by one)
            using (var ms2 = new MemoryStream())
            {
                using (var mfG = System.Drawing.Graphics.FromHwndInternal(IntPtr.Zero))
                {
                    using (Metafile mf = new Metafile(ms2, mfG.GetHdc(), EmfType.EmfPlusOnly, "Chart"))
                    {
                        using (var g = System.Drawing.Graphics.FromImage(mf))
                        {
                            g.DrawImageUnscaled(Image.FromFile("D:\\test0.wmf"), 0, 0);  
                            g.DrawImageUnscaled(Image.FromFile("D:\\test1.wmf"), 0, 0);
                            g.DrawImageUnscaled(Image.FromFile("D:\\test2.wmf"), 0, 0);
                        }
                        // Save the combined file to disc
                        mf.Save("D:\\complete.wmf");
                        // draw the combined file to the form.
                        e.Graphics.DrawImageUnscaled(mf, 0, 0);
                    }
                }
            }
        }
    }