4

我正在尝试将多个图像转换为多 tiff 图像文件。当我在多个图像上运行以下代码时,我收到“GDI+ 中发生一般错误”。错误。如果我只有一张图像,那么它可以正常工作并输出文件。如果我将代码更改为位图并将列表更改为位图,则该代码适用于多个图像。

public List<Metafile> metaFileList = new List<Metafile>();

private void writeImagesToEnhancedMetaMulTiff()
{
        ImageCodecInfo info = null;
        foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
            if (ice.MimeType == "image/tiff")
                info = ice;

        System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;

        EncoderParameters ep = new EncoderParameters(1);
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
        Metafile pages = null;

        int frames = 0;

        foreach (Metafile metaFileItem in metaFileList)
        {
            if (frames == 0)
            {
                pages = metaFileItem;
                pages.Save(@"E:\output_MetaFile.tif", info, ep);
            }
            else
            {
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                pages.SaveAdd(metaFileItem, ep);
            }
            if (frames >= metaFileList.Count() - 1)
            {
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
            }
            frames++;
        }
    }

要访问获取剪贴板作为元文件,我一直在使用以下代码:

    public System.Drawing.Imaging.Metafile GetEnhMetafileOnClipboard(IntPtr hWnd)
    {
        System.Drawing.Imaging.Metafile meta = null;
        if(OpenClipboard(hWnd))
        {
            try
            {
                if (IsClipboardFormatAvailable(CF_ENHMETAFILE) != 0)
                {
                    IntPtr hmeta = GetClipboardData(CF_ENHMETAFILE);
                    meta = new System.Drawing.Imaging.Metafile(hmeta, true);
                    metaFileList.Add(meta);
                }
            }
            finally
            {
                CloseClipboard();
            }
        }
        return meta;
    }

如果您需要我提供更多代码,请告诉我。

谢谢。

4

1 回答 1

0

当我想光栅化它时,我总是避免使用 Metafile 的 Save 方法。相反,我将其绘制为位图并转换并保存位图。

就像是:

                    GraphicsUnit uPix = GraphicsUnit.Pixel;

                    RectangleF bounds = meta.GetBounds(ref uPix);
                    Bitmap dstBitmap = new Bitmap((int)bounds.Width, (int)bounds.Height, PixelFormat.Format32bppArgb);


                    Graphics g = Graphics.FromImage(dstBitmap);
                    g.Clear(Color.White);
                    g.DrawImage(meta, dstBitmap.GetBounds(ref uPix), meta.GetBounds(ref uPix), uPix);

                    dstBitmap.Save(@"E:\output_MetaFile.tif",info,ep);

从剪贴板获取元文件时,我通常使用此代码,因为在元文件的情况下从剪贴板复制数据非常重要:

if(iData.GetDataPresent(DataFormats.EnhancedMetafile))
        {

            // Get the meta data from the clipboard
            IntPtr hwnd = this.Handle;
            if(OpenClipboard(hwnd) == false) return false;
            IntPtr hToTempPastedEmf = GetClipboardData(14); //CF_ENHMETAFILE=14

            // Get the size of the meta data
            int iSize = GetEnhMetaFileBits(hToTempPastedEmf,0,IntPtr.Zero);                             
            // Copy the meta file from the clipboard (MUST remove all references to the clipboard)
            IntPtr hToPastedEmf = CopyEnhMetaFile(hToTempPastedEmf, new IntPtr(0));
            Metafile meta = new Metafile(hToPastedEmf,false); 

            CloseClipboard();

        }
于 2013-08-16T05:45:20.897 回答