GdiPlusFileType.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Resources;
  2. using SmartCoalApplication.Base.Enum;
  3. using SmartCoalApplication.Core;
  4. using SmartCoalApplication.SystemLayer;
  5. using System;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.IO;
  9. namespace SmartCoalApplication
  10. {
  11. /// <summary>
  12. /// Implements FileType for generic GDI+ codecs.
  13. /// </summary>
  14. /// <remarks>
  15. /// GDI+ file types do not support custom headers.
  16. /// </remarks>
  17. public class GdiPlusFileType : FileType
  18. {
  19. private ImageFormat imageFormat;
  20. public ImageFormat ImageFormat
  21. {
  22. get
  23. {
  24. return this.imageFormat;
  25. }
  26. }
  27. protected override void OnSave(Document input, Stream output, SaveConfigToken token, Surface scratchSurface, ProgressEventHandler callback)
  28. {
  29. GdiPlusFileType.Save(input, output, scratchSurface, this.ImageFormat, callback);
  30. }
  31. public static void Save(Document input, Stream output, Surface scratchSurface, ImageFormat format, ProgressEventHandler callback)
  32. {
  33. // flatten the document
  34. scratchSurface.Clear(ColorBgra.FromBgra(0, 0, 0, 0));
  35. using (RenderArgs ra = new RenderArgs(scratchSurface))
  36. {
  37. input.Render(ra, true);
  38. }
  39. using (Bitmap bitmap = scratchSurface.CreateAliasedBitmap())
  40. {
  41. LoadProperties(bitmap, input);
  42. bitmap.Save(output, format);
  43. }
  44. }
  45. public static void LoadProperties(Image dstImage, Document srcDoc)
  46. {
  47. Bitmap asBitmap = dstImage as Bitmap;
  48. if (asBitmap != null)
  49. {
  50. // Sometimes GDI+ does not honor the resolution tags that we
  51. // put in manually via the EXIF properties.
  52. float dpiX;
  53. float dpiY;
  54. switch (srcDoc.DpuUnit)
  55. {
  56. case MeasurementUnit.Centimeter:
  57. dpiX = (float)Document.DotsPerCmToDotsPerInch(srcDoc.DpuX);
  58. dpiY = (float)Document.DotsPerCmToDotsPerInch(srcDoc.DpuY);
  59. break;
  60. case MeasurementUnit.Inch:
  61. dpiX = (float)srcDoc.DpuX;
  62. dpiY = (float)srcDoc.DpuY;
  63. break;
  64. default:
  65. case MeasurementUnit.Pixel:
  66. dpiX = 1.0f;
  67. dpiY = 1.0f;
  68. break;
  69. }
  70. try
  71. {
  72. asBitmap.SetResolution(dpiX, dpiY);
  73. }
  74. catch (Exception)
  75. {
  76. // Ignore error
  77. }
  78. }
  79. Metadata metaData = srcDoc.Metadata;
  80. foreach (string key in metaData.GetKeys(Metadata.ExifSectionName))
  81. {
  82. string blob = metaData.GetValue(Metadata.ExifSectionName, key);
  83. PropertyItem pi = PdnGraphics.DeserializePropertyItem(blob);
  84. try
  85. {
  86. dstImage.SetPropertyItem(pi);
  87. }
  88. catch (ArgumentException)
  89. {
  90. // Ignore error: the image does not support property items
  91. }
  92. }
  93. }
  94. public override Document OnLoad(Stream input)
  95. {
  96. using (Image image = PdnResources.LoadImage(input))
  97. {
  98. Document document = Document.FromImage(image);
  99. return document;
  100. }
  101. }
  102. public static ImageCodecInfo GetImageCodecInfo(ImageFormat format)
  103. {
  104. ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
  105. foreach (ImageCodecInfo icf in encoders)
  106. {
  107. if (icf.FormatID == format.Guid)
  108. {
  109. return icf;
  110. }
  111. }
  112. return null;
  113. }
  114. public GdiPlusFileType(string name, ImageFormat imageFormat, bool supportsLayers, string[] extensions)
  115. : this(name, imageFormat, supportsLayers, extensions, false)
  116. {
  117. }
  118. public GdiPlusFileType(string name, ImageFormat imageFormat, bool supportsLayers, string[] extensions, bool savesWithProgress)
  119. : base(name,
  120. (supportsLayers ? FileTypeFlags.SupportsLayers : 0) |
  121. FileTypeFlags.SupportsLoading |
  122. FileTypeFlags.SupportsSaving |
  123. (savesWithProgress ? FileTypeFlags.SavesWithProgress : 0),
  124. extensions)
  125. {
  126. this.imageFormat = imageFormat;
  127. }
  128. }
  129. }