Beispiel #1
0
        public Bitmap(String filename)
        {
            IntSecurity.DemandReadFileIO(filename);

            //GDI+ will read this file multiple times.  Get the fully qualified path
            //so if our app changes default directory we won't get an error
            //
            filename = Path.GetFullPath(filename);

            IntPtr bitmap = IntPtr.Zero;

            int status = SafeNativeMethods.Gdip.GdipCreateBitmapFromFile(filename, out bitmap);

            if (status != SafeNativeMethods.Gdip.Ok)
            {
                throw SafeNativeMethods.Gdip.StatusException(status);
            }

            status = SafeNativeMethods.Gdip.GdipImageForceValidation(new HandleRef(null, bitmap));

            if (status != SafeNativeMethods.Gdip.Ok)
            {
                SafeNativeMethods.Gdip.GdipDisposeImage(new HandleRef(null, bitmap));
                throw SafeNativeMethods.Gdip.StatusException(status);
            }

            SetNativeImage(bitmap);

            EnsureSave(this, filename, null);
        }
Beispiel #2
0
        /// <include file='doc\Image.uex' path='docs/doc[@for="Image.Save2"]/*' />
        /// <devdoc>
        ///    <para>
        ///       Saves this <see cref='System.Drawing.Image'/> to the specified file in the specified format
        ///       and with the specified encoder parameters.
        ///    </para>
        /// </devdoc>
        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.Ok;

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

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

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

            if (status != SafeNativeMethods.Ok)
            {
                throw SafeNativeMethods.StatusException(status);
            }
        }
Beispiel #3
0
        /// <include file='doc\Image.uex' path='docs/doc[@for="Image.FromFile1"]/*' />
        /// <devdoc>
        ///    <para>[To be supplied.]</para>
        /// </devdoc>
        public static Image FromFile(String filename,
                                     bool useEmbeddedColorManagement)
        {
            // The File.Exists() below will do the demand for the FileIOPermission
            // for us. So, we do not need an additional demand anymore.
            //
            if (!File.Exists(filename))
            {
                // I have to do this so I can give a meaningful
                // error back to the user. File.Exists() cal fail because of either
                // a failure to demand security or because the file does not exist.
                // Always telling the user that the file does not exist is not a good
                // choice. So, we demand the permission again. This means that we are
                // going to demand the permission twice for the failure case, but that's
                // better than always demanding the permission twice.
                //
                IntSecurity.DemandReadFileIO(filename);

                throw new FileNotFoundException(filename);
            }

            //GDI+ will read this file multiple times.  Get the fully qualified path
            //so if our app changes default directory we won't get an error
            //
            filename = Path.GetFullPath(filename);

            IntPtr image = IntPtr.Zero;
            int    status;

            if (useEmbeddedColorManagement)
            {
                status = SafeNativeMethods.GdipLoadImageFromFileICM(filename, out image);
            }
            else
            {
                status = SafeNativeMethods.GdipLoadImageFromFile(filename, out image);
            }

            if (status != SafeNativeMethods.Ok)
            {
                throw SafeNativeMethods.StatusException(status);
            }

            status = SafeNativeMethods.GdipImageForceValidation(new HandleRef(null, image));

            if (status != SafeNativeMethods.Ok)
            {
                SafeNativeMethods.GdipDisposeImage(new HandleRef(null, image));
                throw SafeNativeMethods.StatusException(status);
            }

            Image img = CreateImageObject(image);

            EnsureSave(img, filename, null);

            return(img);
        }