Beispiel #1
0
        private void threadItemIcons_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] _files;

            e.Result = DialogResult.Abort;

            try
            {
                _files = Directory.GetFiles(_InputFolder, "dragitem*.dds");
                string _outPath = _OutputFolder;

                if (!Directory.Exists(_outPath))
                {
                    Directory.CreateDirectory(_outPath);
                }

                _ItemIcons = _files.Length * _ItemIconsPerFile;

                int    _count = 1;
                string _iconFileName;

                for (int _itemIconSheet = 0; _itemIconSheet < 500; _itemIconSheet++)
                {
                    if (File.Exists(_iconFileName = _InputFolder + @"\dragitem" + (_itemIconSheet + 1).ToString() + ".dds"))
                    {
                        int _iconIndex = 0;
                        int _x         = 0;
                        int _y         = 0;

                        byte[] _fileBytes = GetFileBytes(_iconFileName);

                        DDSImage _iconSheet = DDSImage.Load(_fileBytes);

                        while ((_x + _ItemIconSize.X) < _iconSheet.Images[0].Width)
                        {
                            string _outFile = _outPath + @"\" + ((_itemIconSheet * _ItemIconsPerFile) + _iconIndex + 500).ToString() + ".png";

                            Bitmap   _icon    = new Bitmap(_ItemIconSize.X, _ItemIconSize.Y, _iconSheet.Images[0].PixelFormat);
                            Graphics _blitter = Graphics.FromImage(_icon);

                            _blitter.DrawImage(_iconSheet.Images[0], 0, 0, new Rectangle(_x, _y, _ItemIconSize.X, _ItemIconSize.Y), GraphicsUnit.Pixel);
                            _blitter.Dispose();
                            _icon.Save(_outFile);
                            _icon.Dispose();

                            _iconIndex++;
                            _y += _ItemIconSize.Y + _ItemIconPadding.Y;

                            if ((_y + _ItemIconSize.Y) >= _iconSheet.Images[0].Height)
                            {
                                _y  = 0;
                                _x += _ItemIconSize.X + _ItemIconPadding.X;
                            }

                            _TotalItemIconCount++;
                            threadItemIcons.ReportProgress(_count++);

                            if (threadItemIcons.CancellationPending)
                            {
                                e.Result = DialogResult.Cancel;

                                return;
                            }
                        }
                    }
                }
            }
            catch
            {
                return;
            }


            e.Result = DialogResult.OK;

            return;
        }
Beispiel #2
0
        public Image GetImage()
        {
            if (this._IsImageChecked)
            {
                return(this._Image);
            }

            this.ImageSubformat = "";
            this.ImageFormat    = "";
            this._AlphaBits     = -1;

            if (!Util.IsImage(this.Filename))
            {
                this._IsImageChecked = true;
                this._Image          = null;

                return(null);
            }

            byte[] _bytes = this.GetContents();

            if (_bytes == null)
            {
                this._IsImageChecked = true;

                this.Status = Result.OutOfMemory;

                return(null);
            }

            Bitmap _loading = null;

            MemoryStream _stream = new MemoryStream(_bytes);             // MemoryStream should remain open for the lifetime of the Bitmap.

            // Natively Supported Image File?
            try
            {
                _loading = new Bitmap(_stream);

                this.ImageFormat = System.IO.Path.GetExtension(this.Filename).ToLower();
            }
            catch
            {
                _loading = null;

                _stream.Seek(0, SeekOrigin.Begin);
            }

            if (_loading == null)
            {
                // TGA image?

                try
                {
                    TargaImage _tga = new TargaImage(_stream);

                    _loading = _tga.Image;

                    this.ImageFormat = ".tga";
                }
                catch
                {
                    _loading = null;
                }
            }

            if (_loading == null)
            {
                // DDS Texture?

                try
                {
                    DDSImage _dds = DDSImage.Load(_bytes);

                    _loading = _dds.Images[0];

                    this.ImageFormat    = ".dds";
                    this.ImageSubformat = _dds.FormatName;
                }
                catch
                {
                    _loading = null;
                }
            }

            this._IsImageChecked = true;

            this._Image = _loading;

            return(this._Image);
        }