2
0

EncoderParameters.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // System.Drawing.Imaging.EncoderParameters.cs
  3. //
  4. // Author:
  5. // Ravindra ([email protected])
  6. // Vladimir Vukicevic ([email protected])
  7. //
  8. // (C) 2004 Novell, Inc. http://www.novell.com
  9. //
  10. //
  11. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.Runtime.InteropServices;
  34. namespace System.Drawing.Imaging
  35. {
  36. public sealed class EncoderParameters : IDisposable
  37. {
  38. private EncoderParameter[] parameters;
  39. public EncoderParameters () {
  40. parameters = new EncoderParameter[1];
  41. }
  42. public EncoderParameters (int count) {
  43. parameters = new EncoderParameter[count];
  44. }
  45. public EncoderParameter[] Param {
  46. get {
  47. return parameters;
  48. }
  49. set {
  50. parameters = value;
  51. }
  52. }
  53. public void Dispose () {
  54. // Nothing
  55. GC.SuppressFinalize(this);
  56. }
  57. #if !TARGET_JVM
  58. internal IntPtr ToNativePtr () {
  59. IntPtr result;
  60. IntPtr ptr;
  61. // 4 is the initial int32 "count" value
  62. result = Marshal.AllocHGlobal (4 + parameters.Length * EncoderParameter.NativeSize());
  63. ptr = result;
  64. Marshal.WriteInt32 (ptr, parameters.Length);
  65. ptr = (IntPtr) ((int) ptr + 4);
  66. for (int i = 0; i < parameters.Length; i++) {
  67. parameters[i].ToNativePtr (ptr);
  68. ptr = (IntPtr) ((int) ptr + EncoderParameter.NativeSize());
  69. }
  70. return result;
  71. }
  72. /* The IntPtr passed in here is a blob returned from
  73. * GdipImageGetEncoderParameterList. Its internal pointers
  74. * (i.e. the Value pointers in the EncoderParameter entries)
  75. * point to areas within this block of memeory; this means
  76. * that we need to free it as a whole, and also means that
  77. * we can't Marshal.PtrToStruct our way to victory.
  78. */
  79. internal static EncoderParameters FromNativePtr (IntPtr epPtr) {
  80. if (epPtr == IntPtr.Zero)
  81. return null;
  82. IntPtr ptr = epPtr;
  83. int count = Marshal.ReadInt32 (ptr);
  84. ptr = (IntPtr) ((int) ptr + 4);
  85. if (count == 0)
  86. return null;
  87. EncoderParameters result = new EncoderParameters (count);
  88. for (int i = 0; i < count; i++) {
  89. result.parameters[i] = EncoderParameter.FromNativePtr (ptr);
  90. ptr = (IntPtr) ((int) ptr + EncoderParameter.NativeSize());
  91. }
  92. return result;
  93. }
  94. #endif
  95. }
  96. }