Пример #1
0
        public static Bitmap LoadBitmap(string filename, ref bool isSaveSupported)
        {
            if (string.IsNullOrEmpty(filename))
            {
                return(null);
            }
            Bitmap fileBitmap = null;

            LOG.InfoFormat("Loading image from file {0}", filename);
            // Fixed lock problem Bug #3431881
            using (Stream imageFileStream = File.OpenRead(filename)) {
                // And fixed problem that the bitmap stream is disposed... by Cloning the image
                // This also ensures the bitmap is correctly created

                if (filename.EndsWith(".ico"))
                {
                    // Icon logic, try to get the Vista icon, else the biggest possible
                    using (Image tmpImage = ExtractVistaIcon(imageFileStream)) {
                        if (tmpImage != null)
                        {
                            fileBitmap      = CloneImageToBitmap(tmpImage);
                            isSaveSupported = false;
                        }
                    }
                    if (fileBitmap == null)
                    {
                        // No vista icon, try normal icon
                        imageFileStream.Position = 0;
                        // We create a copy of the bitmap, so everything else can be disposed
                        using (Icon tmpIcon = new Icon(imageFileStream, new Size(1024, 1024))) {
                            using (Image tmpImage = tmpIcon.ToBitmap()) {
                                fileBitmap      = ImageHelper.CloneImageToBitmap(tmpImage);
                                isSaveSupported = false;
                            }
                        }
                    }
                }
                if (fileBitmap == null)
                {
                    // We create a copy of the bitmap, so everything else can be disposed
                    using (Image tmpImage = Image.FromStream(imageFileStream, true, true)) {
                        if (tmpImage is Metafile)
                        {
                            isSaveSupported = false;
                        }
                        fileBitmap = ImageHelper.CloneImageToBitmap(tmpImage);
                    }
                }
            }
            return(fileBitmap);
        }