MetaFile到Multi-Tiff图像

时间:2013-08-11 17:32:09

标签: c# .net

我正在尝试将多个图像转换为多图像文件。当我在多个图像上运行下面的代码时,我得到了一个" 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;
    }

如果您需要我提供更多代码,请与我们联系。

感谢。

1 个答案:

答案 0 :(得分: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);

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

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

        }