Ejemplo n.º 1
0
        public void SaveAdd(Image image, EncoderParameters encoderParams) {
            IntPtr encoder = IntPtr.Zero;

            if (image == null) {
                throw new ArgumentNullException("image");
            }
            if (encoderParams != null) {
                encoder = encoderParams.ConvertToMemory();
            }

            rawData = null;
            int status = SafeNativeMethods.Gdip.GdipSaveAddImage(new HandleRef(this, nativeImage), new HandleRef(image, image.nativeImage), new HandleRef(encoderParams, encoder));

            if (encoder != IntPtr.Zero){
                Marshal.FreeHGlobal(encoder);
            }
            if (status != SafeNativeMethods.Gdip.Ok) {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }
        }
Ejemplo n.º 2
0
        public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams) {
            if (stream == null){
                throw new ArgumentNullException("stream");
            }
            if (encoder == null) {
                throw new ArgumentNullException("encoder");
            }

            IntPtr encoderParamsMemory = IntPtr.Zero;

            if (encoderParams != null) {
                rawData = null;
                encoderParamsMemory = encoderParams.ConvertToMemory();
            }

            int status = SafeNativeMethods.Gdip.Ok;

            try {
                Guid g = encoder.Clsid;
                bool saved = false;

                if (rawData != null) {
                    ImageCodecInfo rawEncoder = RawFormat.FindEncoder();
                    if (rawEncoder != null && rawEncoder.Clsid == g) {
                        stream.Write(rawData, 0, rawData.Length);
                        saved = true;
                    }
                }

                if (!saved) {
                    status = SafeNativeMethods.Gdip.GdipSaveImageToStream(new HandleRef(this, nativeImage),
                                                                     new UnsafeNativeMethods.ComStreamFromDataStream(stream),
                                                                     ref g,
                                                                     new HandleRef(encoderParams, encoderParamsMemory));
                }
            }
            finally {
                if (encoderParamsMemory != IntPtr.Zero) {
                    Marshal.FreeHGlobal(encoderParamsMemory);
                }
            }
            
            if (status != SafeNativeMethods.Gdip.Ok) {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }
        }
Ejemplo n.º 3
0
        public void Save(string filename, ImageCodecInfo encoder, EncoderParameters encoderParams) {
            if (filename == null)
                throw new ArgumentNullException("filename");
            if (encoder == null)
                throw new ArgumentNullException("encoder");

            IntSecurity.DemandWriteFileIO(filename);

            IntPtr encoderParamsMemory = IntPtr.Zero;

            if (encoderParams != null) {
                rawData = null;
                encoderParamsMemory = encoderParams.ConvertToMemory();
            }
            int status = SafeNativeMethods.Gdip.Ok;

            try {
                Guid g = encoder.Clsid;
                bool saved = false;

                if (rawData != null) {
                    ImageCodecInfo rawEncoder = RawFormat.FindEncoder();
                    if (rawEncoder != null && rawEncoder.Clsid == g) {
                        using (FileStream fs = File.OpenWrite(filename)) {
                            fs.Write(rawData, 0, rawData.Length);
                            saved = true;
                        }
                    }
                }

                if (!saved) {
                    status = SafeNativeMethods.Gdip.GdipSaveImageToFile(new HandleRef(this, nativeImage),
                                                             filename,
                                                             ref g,
                                                             new HandleRef(encoderParams, encoderParamsMemory));
                }
            }
            finally {
                if (encoderParamsMemory != IntPtr.Zero) {
                    Marshal.FreeHGlobal(encoderParamsMemory);
                }
            }

            if (status != SafeNativeMethods.Gdip.Ok) {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }
        }
 public void SaveAdd(EncoderParameters encoderParams)
 {
     IntPtr zero = IntPtr.Zero;
     if (encoderParams != null)
     {
         zero = encoderParams.ConvertToMemory();
     }
     this.rawData = null;
     int status = SafeNativeMethods.Gdip.GdipSaveAdd(new HandleRef(this, this.nativeImage), new HandleRef(encoderParams, zero));
     if (zero != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(zero);
     }
     if (status != 0)
     {
         throw SafeNativeMethods.Gdip.StatusException(status);
     }
 }
 public void Save(string filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
 {
     if (filename == null)
     {
         throw new ArgumentNullException("filename");
     }
     if (encoder == null)
     {
         throw new ArgumentNullException("encoder");
     }
     System.Drawing.IntSecurity.DemandWriteFileIO(filename);
     IntPtr zero = IntPtr.Zero;
     if (encoderParams != null)
     {
         this.rawData = null;
         zero = encoderParams.ConvertToMemory();
     }
     int status = 0;
     try
     {
         Guid clsid = encoder.Clsid;
         bool flag = false;
         if (this.rawData != null)
         {
             ImageCodecInfo info = this.RawFormat.FindEncoder();
             if ((info != null) && (info.Clsid == clsid))
             {
                 using (FileStream stream = File.OpenWrite(filename))
                 {
                     stream.Write(this.rawData, 0, this.rawData.Length);
                     flag = true;
                 }
             }
         }
         if (!flag)
         {
             status = SafeNativeMethods.Gdip.GdipSaveImageToFile(new HandleRef(this, this.nativeImage), filename, ref clsid, new HandleRef(encoderParams, zero));
         }
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             Marshal.FreeHGlobal(zero);
         }
     }
     if (status != 0)
     {
         throw SafeNativeMethods.Gdip.StatusException(status);
     }
 }
 public void Save(Stream stream, ImageCodecInfo encoder, EncoderParameters encoderParams)
 {
     if (stream == null)
     {
         throw new ArgumentNullException("stream");
     }
     if (encoder == null)
     {
         throw new ArgumentNullException("encoder");
     }
     IntPtr zero = IntPtr.Zero;
     if (encoderParams != null)
     {
         this.rawData = null;
         zero = encoderParams.ConvertToMemory();
     }
     int status = 0;
     try
     {
         Guid clsid = encoder.Clsid;
         bool flag = false;
         if (this.rawData != null)
         {
             ImageCodecInfo info = this.RawFormat.FindEncoder();
             if ((info != null) && (info.Clsid == clsid))
             {
                 stream.Write(this.rawData, 0, this.rawData.Length);
                 flag = true;
             }
         }
         if (!flag)
         {
             status = SafeNativeMethods.Gdip.GdipSaveImageToStream(new HandleRef(this, this.nativeImage), new System.Drawing.UnsafeNativeMethods.ComStreamFromDataStream(stream), ref clsid, new HandleRef(encoderParams, zero));
         }
     }
     finally
     {
         if (zero != IntPtr.Zero)
         {
             Marshal.FreeHGlobal(zero);
         }
     }
     if (status != 0)
     {
         throw SafeNativeMethods.Gdip.StatusException(status);
     }
 }