0

GDI+ メタファイルに問題があります。グラフィックごとにメタファイルを保存したい。ポイント数が 10000 で、保存されたメタファイルを開くことができる場合にうまく機能します。ただし、ポイント数が多い場合 (例: count = 10000000)、mspaint.exe でメタファイルを開くことができません。

見逃したものはありますか?メタファイルのレコード サイズは制限されていますか? ちなみに、drawrectanglesこの問題もあります。

これが私のコードです:

private void button1_Click(object sender, EventArgs e)  
{  
    int width = 1489;  
    int height = 471;

    Graphics offScreenBufferGraphics;
    Metafile m;
    using (MemoryStream stream = new MemoryStream())
    {
        using (offScreenBufferGraphics = Graphics.FromHwndInternal(IntPtr.Zero))
        {
            IntPtr deviceContextHandle = offScreenBufferGraphics.GetHdc();
            m = new Metafile(
            stream,
            deviceContextHandle,
            new RectangleF(0, 0, width, height),
            MetafileFrameUnit.Pixel,
            EmfType.EmfPlusOnly);
            offScreenBufferGraphics.ReleaseHdc();
        }
    }

    using (Graphics g = Graphics.FromImage(m))
    {
        // Set everything to high quality
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.PixelOffsetMode = PixelOffsetMode.HighQuality;
        g.CompositingQuality = CompositingQuality.HighQuality;

        MetafileHeader metafileHeader = m.GetMetafileHeader();
        g.ScaleTransform(
            metafileHeader.DpiX / g.DpiX,
            metafileHeader.DpiY / g.DpiY);

        g.PageUnit = GraphicsUnit.Pixel;
        g.SetClip(new RectangleF(0, 0, width, height));

        // clears the image and colors the entire background
        g.Clear(Color.White);

        // draw lines
        using (Pen pen = new Pen(Color.Black, 1f))
        {
            Random rnd = new Random(DateTime.Now.Millisecond);
            List<PointF> polyPoints = new List<PointF>();
            const int count = 10000;
            for (int i = 1; i <= 10000000; i++)
            {
                polyPoints.Add(new PointF(rnd.Next(1000),rnd.Next(1000)));
            }
            g.DrawLines(pen, polyPoints.ToArray());
            // while
        } // using
    } // using

    // Get a handle to the metafile
    IntPtr iptrMetafileHandle = m.GetHenhmetafile();

    // Export metafile to an image file
    CopyEnhMetaFile(iptrMetafileHandle, @"F:\CacheToDisk\test2.emf");

    // Delete the metafile from memory
    DeleteEnhMetaFile(iptrMetafileHandle);
}

[DllImport("gdi32.dll")]
static extern IntPtr CopyEnhMetaFile(  // Copy EMF to file
    IntPtr hemfSrc,   // Handle to EMF
    String lpszFile // File
);

[DllImport("gdi32.dll")]
static extern int DeleteEnhMetaFile(  // Delete EMF
    IntPtr hemf // Handle to EMF
);
4

1 に答える 1

0

限界のようです。DrawLinesの代わりにDrawPathを使用すると、正しく動作します。

于 2014-04-02T13:11:19.053 に答える