MetaFile for large print

I am trying to convert multiple images to a multi-type image file. When I run the code below on more than one image, I get "General error that occurred in GDI +". error. If I have only one image, then it works fine and displays the file. If I changed the code to a bitmap and the list to a 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 buffer as a metafile, I used 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.

+4
source share
1 answer

I always avoided using the metafile save method when I want to rasterize it. Instead, I draw it in Bitmap, convert and save Bitmap.

Sort of:

  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 I receive a metafile from the clipboard, I usually use this code, since it is very important to copy 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(); } 
0
source

Source: https://habr.com/ru/post/1496436/


All Articles