Quantcast
Channel: Windows Forms General forum
Viewing all articles
Browse latest Browse all 12583

Use Graphic.DrawLines(Pen pen, Points points) for Metafile, the picture can not open when the count of points is large.

$
0
0

Hi guys,

I have a problem with a GDI+ metafile. I want to save a metafile by graphic. It works well when the point count is 10000 and the saved metafile can be opened. But when the point count is large (e.g. count = 100000), the metafile cannot be opened by mspaint.exe.

Is metafile record size limited? By the way, drawrectangles also has this issue.

Please have a look the following codes.

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 <= count; 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
		);
    }



Viewing all articles
Browse latest Browse all 12583

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>