PageRenderTime 55ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/Source/HelixToolkit.Wpf/Exporters/BitmapExporter.cs

#
C# | 126 lines | 65 code | 17 blank | 44 comment | 3 complexity | 11e706c2fb7384717f5be331281c1a05 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // --------------------------------------------------------------------------------------------------------------------
  2. // <copyright file="BitmapExporter.cs" company="Helix 3D Toolkit">
  3. // http://helixtoolkit.codeplex.com, license: Ms-PL
  4. // </copyright>
  5. // --------------------------------------------------------------------------------------------------------------------
  6. namespace HelixToolkit.Wpf
  7. {
  8. using System;
  9. using System.IO;
  10. using System.Windows.Controls;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Media.Media3D;
  14. /// <summary>
  15. /// Exports a Viewport3D to a .bmp or .png file.
  16. /// </summary>
  17. public class BitmapExporter : IExporter
  18. {
  19. #region Constructors and Destructors
  20. /// <summary>
  21. /// Initializes a new instance of the <see cref="BitmapExporter"/> class.
  22. /// </summary>
  23. /// <param name="path">
  24. /// The path.
  25. /// </param>
  26. public BitmapExporter(string path)
  27. {
  28. this.FileName = path;
  29. this.OversamplingMultiplier = 2;
  30. }
  31. #endregion
  32. #region Public Properties
  33. /// <summary>
  34. /// Gets or sets the background brush.
  35. /// </summary>
  36. /// <value>The background.</value>
  37. public Brush Background { get; set; }
  38. /// <summary>
  39. /// Gets or sets the name of the file.
  40. /// </summary>
  41. /// <value>The name of the file.</value>
  42. public string FileName { get; set; }
  43. /// <summary>
  44. /// Gets or sets the oversampling multiplier.
  45. /// </summary>
  46. /// <value>The oversampling multiplier.</value>
  47. public int OversamplingMultiplier { get; set; }
  48. #endregion
  49. #region Public Methods
  50. /// <summary>
  51. /// Exports the specified viewport.
  52. /// </summary>
  53. /// <param name="viewport">
  54. /// The viewport.
  55. /// </param>
  56. public void Export(Viewport3D viewport)
  57. {
  58. int m = this.OversamplingMultiplier;
  59. var background = this.Background;
  60. if (background == null)
  61. {
  62. background = Brushes.Transparent;
  63. }
  64. var bmp = Viewport3DHelper.RenderBitmap(viewport, background, m);
  65. BitmapEncoder encoder;
  66. string ext = Path.GetExtension(this.FileName);
  67. switch (ext.ToLower())
  68. {
  69. case ".jpg":
  70. var jpg = new JpegBitmapEncoder();
  71. jpg.Frames.Add(BitmapFrame.Create(bmp));
  72. encoder = jpg;
  73. break;
  74. case ".png":
  75. var png = new PngBitmapEncoder();
  76. png.Frames.Add(BitmapFrame.Create(bmp));
  77. encoder = png;
  78. break;
  79. default:
  80. throw new InvalidOperationException("Not supported file format.");
  81. }
  82. using (Stream stm = File.Create(this.FileName))
  83. {
  84. encoder.Save(stm);
  85. }
  86. }
  87. /// <summary>
  88. /// Exports the specified visual.
  89. /// </summary>
  90. /// <param name="visual">
  91. /// The visual.
  92. /// </param>
  93. public void Export(Visual3D visual)
  94. {
  95. throw new NotImplementedException();
  96. }
  97. /// <summary>
  98. /// Exports the specified model.
  99. /// </summary>
  100. /// <param name="model">
  101. /// The model.
  102. /// </param>
  103. public void Export(Model3D model)
  104. {
  105. throw new NotImplementedException();
  106. }
  107. #endregion
  108. }
  109. }