0
我有一個GDI +圖元文件出現問題。我想通過圖形保存圖元文件。當點數爲10000並且保存的圖元文件可以打開時,它運行良好。但是,當點數很大(例如count = 10000000)時,mspaint.exe無法打開圖元文件。對圖元文件使用Graphic.DrawLines(Pen pen,Points points),當點數很大時圖片無法打開
有什麼我錯過了嗎?元文件記錄大小是否有限?順便說一句,drawrectangles
也有這個問題。
這裏是我的代碼:
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 <= 10000000; 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
);