Beispiel #1
0
        /*
         *   Open the dialog box to let user chose a picture
         */
        public Bitmap LoadImageFromDisk(IInterfaceInOut input, PictureBox pictureBoxForImageLoaded)
        {
            this.pictureBoxForImageLoaded = pictureBoxForImageLoaded;

            OpenFileDialog filedialog = new OpenFileDialog
            {
                Title  = input.GetDialogTitle(),
                Filter = input.GetDialogFilter()
            };

            // check if valid input
            if (filedialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // import image
                StreamReader streamReader = new StreamReader(filedialog.FileName);
                originalImageFromFile = (Bitmap)Image.FromStream(streamReader.BaseStream);
                streamReader.Close();

                LoadImageFromFile();
            }
            else
            {
                throw new Exception("User cancelled image selection dialog.");
            }

            return(loadedImage);
        }
Beispiel #2
0
        /*
         *   Open the dialog box to let user save the picture
         */
        public void SaveImageToDisk(Image loadedImage, IInterfaceInOut output)
        {
            SaveFileDialog filedialog = new SaveFileDialog
            {
                Title  = output.GetDialogTitle(),
                Filter = output.GetDialogFilter()
            };

            // check if valid input
            if (filedialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                // select right extension
                String      fileExtension = Path.GetExtension(filedialog.FileName).ToUpper();
                ImageFormat imgFormat;

                switch (fileExtension)
                {
                case "BMP":
                    imgFormat = ImageFormat.Bmp;
                    break;

                case "JPG":
                    imgFormat = ImageFormat.Jpeg;
                    break;

                case "PNG":
                default:
                    imgFormat = ImageFormat.Png;
                    break;
                }

                // export image
                StreamWriter streamWriter = new StreamWriter(filedialog.FileName, false);
                loadedImage.Save(streamWriter.BaseStream, imgFormat);
                streamWriter.Flush();
                streamWriter.Close();
            }
        }