EncoderParameters.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / CommonUI / System / Drawing / Advanced / EncoderParameters.cs / 1 / EncoderParameters.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

/*************************************************************************\ 
* 
* Copyright (c) 1998-1999, Microsoft Corp.  All Rights Reserved.
* 
* Module Name:
*
*   EncoderParameters.cs
* 
* Abstract:
* 
*   Native GDI+ EncoderParameters structure. 
*
* Revision History: 
*
*   9/22/1999 davidx
*       Created it.
* 
\**************************************************************************/
 
namespace System.Drawing.Imaging { 
    using System.Text;
    using System.Runtime.InteropServices; 

    using System.Diagnostics;
    using System.Diagnostics.CodeAnalysis;
 
    using System;
    using System.Drawing.Internal; 
    using Marshal = System.Runtime.InteropServices.Marshal; 
    using System.Drawing;
 
    //[StructLayout(LayoutKind.Sequential)]
    /// 
    /// 
    ///    [To be supplied.] 
    /// 
    public sealed class EncoderParameters : IDisposable { 
        EncoderParameter[] param; 

        ///  
        /// 
        ///    [To be supplied.]
        /// 
        public EncoderParameters(int count) { 
             param = new EncoderParameter[count];
        } 
 
        /// 
        ///  
        ///    [To be supplied.]
        /// 
        public EncoderParameters() {
             param = new EncoderParameter[1]; 
        }
 
        ///  
        /// 
        ///    [To be supplied.] 
        /// 
        public EncoderParameter[] Param {
            //
 
            get {
                return param; 
            } 
            set {
                param = value; 
            }
        }

        ///  
        ///     Copy the EncoderParameters data into a chunk of memory to be consumed by native GDI+ code.
        /// 
        ///     We need to marshal the EncoderParameters info from/to native GDI+ ourselve since the definition of the managed/unmanaged classes 
        ///     are different and the native class is a bit weird. The native EncoderParameters class is defined in GDI+ as follows:
        /// 
        ///      class EncoderParameters {
        ///          UINT Count;                      // Number of parameters in this structure
        ///          EncoderParameter Parameter[1];   // Parameter values
        ///      }; 
        ///
        ///     We don't have the 'Count' field since the managed array contains it. In order for this structure to work with more than one 
        ///     EncoderParameter we need to preallocate memory for the extra n-1 elements, something like this: 
        ///
        ///         EncoderParameters* pEncoderParameters = (EncoderParameters*) malloc(sizeof(EncoderParameters) + (n-1) * sizeof(EncoderParameter)); 
        ///
        ///     Also, in 64-bit platforms, 'Count' is aligned in 8 bytes (4 extra padding bytes) so we use IntPtr instead of Int32 to account for
        ///     that (See VSW#451333).
        ///  
        internal IntPtr ConvertToMemory() {
            int size = Marshal.SizeOf(typeof(EncoderParameter)); 
 
            IntPtr memory = Marshal.AllocHGlobal(param.Length * size  + Marshal.SizeOf(typeof(IntPtr)));
 
            if (memory == IntPtr.Zero){
                throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.OutOfMemory);
            }
 
            Marshal.WriteIntPtr(memory, (IntPtr) param.Length);
 
            long arrayOffset = (long) memory + Marshal.SizeOf(typeof(IntPtr)); 

            for (int i=0; i 
        ///     Copy the native GDI+ EncoderParameters data from a chunk of memory into a managed EncoderParameters object.
        ///     See ConvertToMemory for more info. 
        /// 
        [SuppressMessage("Microsoft.Performance", "CA1808:AvoidCallsThatBoxValueTypes")]
        internal static EncoderParameters ConvertFromMemory(IntPtr memory) {
            if (memory == IntPtr.Zero) { 
                throw SafeNativeMethods.Gdip.StatusException(SafeNativeMethods.Gdip.InvalidParameter);
            } 
 
            int count = Marshal.ReadIntPtr(memory).ToInt32();
 
            EncoderParameters p = new EncoderParameters(count);
            int size = Marshal.SizeOf(typeof(EncoderParameter));
            long arrayOffset = (long)memory + Marshal.SizeOf(typeof(IntPtr));
 
            for (int i=0; i
        public void Dispose() { 
            foreach (EncoderParameter p in param) {
                if( p != null ){
                    p.Dispose();
                } 
            }
            param = null; 
        } 
    }
} 


// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK