예제 #1
0
파일: FormMain.cs 프로젝트: tmp7701/Gorgon
        /// <summary>
        /// Function to convert the image to use our custom codec.
        /// </summary>
        private void ConvertImage()
        {
            // The path to our image file for our custom codec.
            string tempPath = Path.ChangeExtension(Path.GetTempPath().FormatDirectory(Path.DirectorySeparatorChar) + Path.GetRandomFileName(), "Useless");
            // The new texture holding image data read by the custom codec.
            GorgonTexture2D newTexture = null;

            try
            {
                // Save the current texture using our useless new custom codec.
                _image.Save(tempPath, _customCodec);

                newTexture = _graphics.Textures.FromFile <GorgonTexture2D>("UselessTexture", tempPath, _customCodec);

                // Free the old texture and assign to the new one.
                _image.Dispose();
                _image = newTexture;
            }
            catch
            {
                // Clean up the new texture should we have an exception (this shouldn't happen, better safe than sorry).
                if (newTexture != null)
                {
                    newTexture.Dispose();
                }

                throw;
            }
            finally
            {
                try
                {
                    File.Delete(tempPath);
                }
                // ReSharper disable once EmptyGeneralCatchClause
                catch
                {
                    // Intentionally left blank.
                    // If we can't clean up the temp file, then it's no big deal right now.
                }
            }
        }