private void FileExportImageMenuItem_Click(object sender, EventArgs e) { using (var dialog = new SaveFileDialog()) { dialog.Filter = "PNG Images|*.png|JPEG Images|*.jpg|BMP Images|*.bmp|Enhanced Metafiles (EMF)|*.emf|All Files|*.*||"; dialog.Title = "Export Image"; dialog.InitialDirectory = PathHelper.SafeGetDirectoryName(Settings.LastExportImageFileName); if (dialog.ShowDialog() == DialogResult.OK) { Settings.LastExportImageFileName = dialog.InitialDirectory; var format = ImageFormat.Png; var ext = Path.GetExtension(dialog.FileName); if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".jpg") == 0 || StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".jpeg") == 0) { format = ImageFormat.Jpeg; } else if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".bmp") == 0) { format = ImageFormat.Bmp; } else if (StringComparer.InvariantCultureIgnoreCase.Compare(ext, ".emf") == 0) { format = ImageFormat.Emf; } var size = m_canvas.ComputeCanvasBounds(true).Size *m_canvas.ZoomFactor; size.X = Numeric.Clamp(size.X, 16, 8192); size.Y = Numeric.Clamp(size.Y, 16, 8192); try { if (format == ImageFormat.Emf) { // export as a metafile using (var nativeGraphics = Graphics.FromHwnd(m_canvas.Handle)) { using (var stream = new MemoryStream()) { try { var dc = nativeGraphics.GetHdc(); using (var metafile = new Metafile(stream, dc)) { using (var imageGraphics = Graphics.FromImage(metafile)) { using (var graphics = XGraphics.FromGraphics(imageGraphics, new XSize(size.X, size.Y))) { m_canvas.Draw(graphics, true, size.X, size.Y); } } var handle = metafile.GetHenhmetafile(); var copy = CopyEnhMetaFile(handle, dialog.FileName); DeleteEnhMetaFile(copy); } } finally { nativeGraphics.ReleaseHdc(); } } } } else { // export as an image using (var bitmap = new Bitmap((int)Math.Ceiling(size.X), (int)Math.Ceiling(size.Y))) { using (var imageGraphics = Graphics.FromImage(bitmap)) { using (var graphics = XGraphics.FromGraphics(imageGraphics, new XSize(size.X, size.Y))) { m_canvas.Draw(graphics, true, size.X, size.Y); } } bitmap.Save(dialog.FileName, format); } } } catch (Exception ex) { MessageBox.Show(Program.MainForm, string.Format("There was a problem exporting the map:\n\n{0}", ex.Message), Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } }
private static void Annotate(XGraphics graphics, Palette palette, LineSegment lineSegment, TextBlock text, StringAlignment alignment) { Vector point; var delta = lineSegment.Delta; switch (alignment) { case StringAlignment.Near: default: point = lineSegment.Start; delta.Negate(); break; case StringAlignment.Center: point = lineSegment.Mid; break; case StringAlignment.Far: point = lineSegment.End; break; } var bounds = new Rect(point, Vector.Zero); bounds.Inflate(Settings.TextOffsetFromConnection); var angle = (float)-(Math.Atan2(delta.Y, delta.X) / Math.PI * 180.0); var compassPoint = CompassPoint.East; if (Numeric.InRange(angle, 0, 45)) { compassPoint = CompassPoint.NorthWest; } else if (Numeric.InRange(angle, 45, 90)) { compassPoint = CompassPoint.SouthEast; } else if (Numeric.InRange(angle, 90, 135)) { compassPoint = CompassPoint.SouthWest; } else if (Numeric.InRange(angle, 135, 180)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, 0, -45)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, -45, -90)) { compassPoint = CompassPoint.NorthEast; } else if (Numeric.InRange(angle, -90, -135)) { compassPoint = CompassPoint.NorthWest; } else if (Numeric.InRange(angle, -135, -180)) { compassPoint = CompassPoint.SouthEast; } var pos = bounds.GetCorner(compassPoint); XStringFormat format = new XStringFormat(); Drawing.SetAlignmentFromCardinalOrOrdinalDirection(format, compassPoint); if (alignment == StringAlignment.Center && Numeric.InRange(angle, -10, 10)) { // HACK: if the line segment is pretty horizontal and we're drawing mid-line text, // move text below the line to get it out of the way of any labels at the ends, // and center the text so it fits onto a line between two proximal rooms. pos = bounds.GetCorner(CompassPoint.South); format.Alignment = XStringAlignment.Center; format.LineAlignment = XLineAlignment.Near; } text.Draw(graphics, Settings.LineFont, palette.LineBrush, pos, Vector.Zero, format); }