简体   繁体   中英

MetaFile to Multi-Tiff Image

I am trying to convert multiple images to a multi-tiff image file. When i run the code below on more than one image i get a "A generic error occurred in GDI+." error. If i have just one image then it works fine and outputs the file. If i change the code to bitmap and the list to bitmap then the code works fine with multiple images.

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++;
        }
    }

To access the get the clipboard as a metafile i have been using this code:

    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;
    }

Let me know if you need me to provide more code.

Thanks.

I have allways avoided using the Save method of Metafile when I want to rasterize it. Instead I draw it to a Bitmap and convert and save the Bitmap.

Something like:

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

When getting a Metafile from the clipboard I ususally use this code since it is very important to copy the data from the clipboard in the case of Metafiles:

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

        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM