patrik
Joined: 03 Aug 2007 Posts: 1
|
Posted: Fri Aug 03, 2007 9:57 pm Post subject: How To: Export to EMF insted of BMP |
|
|
Enhanced Metafiles (EMF) are vector files capable of high resolution graphics and small in size.
Open test.emf in Windows Picture and Fax Viewer and try the zoom
Change miBmp_Click in '\Samples C#\DocumentViewer\Viewer.cs' to the following.
Code: | private void miBmp_Click(object sender, System.EventArgs e)
{
int page = this.pagePreview.Page;
// Reuse the renderer from the preview
DocumentRenderer renderer = this.pagePreview.Renderer;
PageInfo info = renderer.FormattedDocument.GetPageInfo(page);
// Create an image
int dpi = 150;
int dx, dy;
if (info.Orientation == PdfSharp.PageOrientation.Portrait)
{
dx = (int)(info.Width.Inch * dpi);
dy = (int)(info.Height.Inch * dpi);
}
else
{
dx = (int)(info.Height.Inch * dpi);
dy = (int)(info.Width.Inch * dpi);
}
// Image image = new Bitmap(dx, dy, PixelFormat.Format32bppRgb);
// Get the graphics object for creating the metafile
FileStream fileStream = new FileStream("test.emf", FileMode.OpenOrCreate, FileAccess.Write);
Graphics gr = CreateGraphics();
IntPtr hDC = gr.GetHdc();
Metafile image = new Metafile(fileStream, hDC, new Rectangle(0, 0, dx, dy), MetafileFrameUnit.Pixel, EmfType.EmfPlusOnly);
// Don't need this graphics object any more
gr.ReleaseHdc(hDC);
gr.Dispose();
// Create a Graphics object for the image and scale it for drawing with 72 dpi
Graphics graphics = Graphics.FromImage(image);
graphics.Clear(System.Drawing.Color.White);
float scale = dpi / 72f;
graphics.ScaleTransform(scale, scale);
// Create an XGraphics object and render the page
XGraphics gfx = XGraphics.FromGraphics(graphics, new XSize(info.Width.Point, info.Height.Point));
renderer.RenderPage(gfx, page);
gfx.Dispose();
// Close the file
fileStream.Close();
// image.Save("test.png", ImageFormat.Png);
Process.Start("mspaint", "test.emf"); // Use MSPaint, not Photoshop...
// Process.Start("mspaint", "test.png"); // Use MSPaint, not Photoshop...
}
|
|
|
oliverbock
Joined: 22 May 2009 Posts: 2
|
Posted: Fri May 22, 2009 6:14 am Post subject: |
|
|
Very useful when your application needs to render to more than PDF. e.g. Using one bit of drawing code (using XGraphics) can draw metafiles for the clipboard, PDFs for saving to file, and using normal Graphics objects to print and for screen display. |
|