EncoderParameters.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. using System;
  11. using System.Runtime.InteropServices;
  12. namespace System.Drawing.Imaging
  13. {
  14. public sealed class EncoderParameters : IDisposable
  15. {
  16. private EncoderParameter[] parameters;
  17. public EncoderParameters () {
  18. parameters = new EncoderParameter[1];
  19. }
  20. public EncoderParameters (int count) {
  21. parameters = new EncoderParameter[count];
  22. }
  23. public EncoderParameter[] Param {
  24. get {
  25. return parameters;
  26. }
  27. set {
  28. parameters = value;
  29. }
  30. }
  31. public void Dispose () {
  32. // Nothing
  33. }
  34. internal IntPtr ToNativePtr () {
  35. IntPtr result;
  36. IntPtr ptr;
  37. // 4 is the initial int32 "count" value
  38. result = Marshal.AllocHGlobal (4 + parameters.Length * EncoderParameter.NativeSize());
  39. ptr = result;
  40. Marshal.WriteInt32 (ptr, parameters.Length);
  41. ptr = (IntPtr) ((int) ptr + 4);
  42. for (int i = 0; i < parameters.Length; i++) {
  43. parameters[i].ToNativePtr (ptr);
  44. ptr = (IntPtr) ((int) ptr + EncoderParameter.NativeSize());
  45. }
  46. return result;
  47. }
  48. /* The IntPtr passed in here is a blob returned from
  49. * GdipImageGetEncoderParameterList. Its internal pointers
  50. * (i.e. the Value pointers in the EncoderParameter entries)
  51. * point to areas within this block of memeory; this means
  52. * that we need to free it as a whole, and also means that
  53. * we can't Marshal.PtrToStruct our way to victory.
  54. */
  55. internal static EncoderParameters FromNativePtr (IntPtr epPtr) {
  56. if (epPtr == IntPtr.Zero)
  57. return null;
  58. IntPtr ptr = epPtr;
  59. int count = Marshal.ReadInt32 (ptr);
  60. ptr = (IntPtr) ((int) ptr + 4);
  61. if (count == 0)
  62. return null;
  63. EncoderParameters result = new EncoderParameters (count);
  64. for (int i = 0; i < count; i++) {
  65. result.parameters[i] = EncoderParameter.FromNativePtr (ptr);
  66. ptr = (IntPtr) ((int) ptr + EncoderParameter.NativeSize());
  67. }
  68. return result;
  69. }
  70. }
  71. }