#pragma once


namespace Bildbetrachter {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
   using namespace System::Drawing::Imaging;

	/// <summary>
	/// Zusammenfassung für Form1
	///
	/// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch
	///          die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern,
	///          das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt.
	///          Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen
	///          arbeiten, die diesem Formular zugewiesen sind.
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
   private:
      Image^ img;
      OpenFileDialog^ openFileDialog1;
      SaveFileDialog^ saveFileDialog1;

	public:
		Form1(void)
		{
			InitializeComponent();

         img = nullptr;
         
         openFileDialog1 = gcnew OpenFileDialog();
         openFileDialog1->InitialDirectory = ".";
         openFileDialog1->Filter = "Windows Bitmap (*.bmp) | *.bmp"
                                 + "| JPEG (*.jpg) | *.jpg"
                                 + "| PNG (*.png) | *.png"
                                 + "| Windows Metafile (*.wmf) | *.wmf"
                                 + "| Grafikdatei (*.bmp;*.jpg;*.png;*.wmf) "
                                                 + "| *.bmp;*.jpg;*.png;*.wmf";
         openFileDialog1->FilterIndex = 5;
         openFileDialog1->Multiselect = false;
         openFileDialog1->RestoreDirectory = true;
         
         saveFileDialog1 = gcnew SaveFileDialog();
         saveFileDialog1->InitialDirectory = ".";
         saveFileDialog1->Filter = "Windows Bitmap (*.bmp) | *.bmp"
                                 + "| JPEG (*.jpg) | *.jpg"
                                 + "| PNG (*.png) | *.png";
         saveFileDialog1->FilterIndex = 1;
         saveFileDialog1->RestoreDirectory = true;
      }

	protected:
		/// <summary>
		/// Verwendete Ressourcen bereinigen.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
   private: System::Windows::Forms::MenuStrip^  menuStrip1;
   private: System::Windows::Forms::ToolStripMenuItem^  dateiToolStripMenuItem;
   private: System::Windows::Forms::ToolStripMenuItem^  oeffnenToolStripMenuItem;
   private: System::Windows::Forms::ToolStripMenuItem^  speichernToolStripMenuItem;
   private: System::Windows::Forms::ToolStripSeparator^  toolStripMenuItem1;
   private: System::Windows::Forms::ToolStripMenuItem^  beendenToolStripMenuItem;
   private: System::Windows::Forms::PictureBox^  pictureBox1;


	private:
		/// <summary>
		/// Erforderliche Designervariable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Erforderliche Methode für die Designerunterstützung.
		/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
		/// </summary>
		void InitializeComponent(void)
		{
         this->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
         this->dateiToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->oeffnenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->speichernToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->toolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripSeparator());
         this->beendenToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
         this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox());
         this->menuStrip1->SuspendLayout();
         (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->BeginInit();
         this->SuspendLayout();
         // 
         // menuStrip1
         // 
         this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(1) {this->dateiToolStripMenuItem});
         this->menuStrip1->Location = System::Drawing::Point(0, 0);
         this->menuStrip1->Name = L"menuStrip1";
         this->menuStrip1->Size = System::Drawing::Size(442, 24);
         this->menuStrip1->TabIndex = 0;
         this->menuStrip1->Text = L"menuStrip1";
         // 
         // dateiToolStripMenuItem
         // 
         this->dateiToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {this->oeffnenToolStripMenuItem, 
            this->speichernToolStripMenuItem, this->toolStripMenuItem1, this->beendenToolStripMenuItem});
         this->dateiToolStripMenuItem->Name = L"dateiToolStripMenuItem";
         this->dateiToolStripMenuItem->Size = System::Drawing::Size(46, 20);
         this->dateiToolStripMenuItem->Text = L"&Datei";
         // 
         // oeffnenToolStripMenuItem
         // 
         this->oeffnenToolStripMenuItem->Name = L"oeffnenToolStripMenuItem";
         this->oeffnenToolStripMenuItem->Size = System::Drawing::Size(152, 22);
         this->oeffnenToolStripMenuItem->Text = L"Ö&ffnen";
         this->oeffnenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::oeffnenToolStripMenuItem_Click);
         // 
         // speichernToolStripMenuItem
         // 
         this->speichernToolStripMenuItem->Name = L"speichernToolStripMenuItem";
         this->speichernToolStripMenuItem->Size = System::Drawing::Size(152, 22);
         this->speichernToolStripMenuItem->Text = L"&Speichern";
         this->speichernToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::speichernToolStripMenuItem_Click);
         // 
         // toolStripMenuItem1
         // 
         this->toolStripMenuItem1->Name = L"toolStripMenuItem1";
         this->toolStripMenuItem1->Size = System::Drawing::Size(149, 6);
         // 
         // beendenToolStripMenuItem
         // 
         this->beendenToolStripMenuItem->Name = L"beendenToolStripMenuItem";
         this->beendenToolStripMenuItem->Size = System::Drawing::Size(152, 22);
         this->beendenToolStripMenuItem->Text = L"&Beenden";
         this->beendenToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::beendenToolStripMenuItem_Click);
         // 
         // pictureBox1
         // 
         this->pictureBox1->Dock = System::Windows::Forms::DockStyle::Fill;
         this->pictureBox1->Location = System::Drawing::Point(0, 24);
         this->pictureBox1->Name = L"pictureBox1";
         this->pictureBox1->Size = System::Drawing::Size(442, 225);
         this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;
         this->pictureBox1->TabIndex = 2;
         this->pictureBox1->TabStop = false;
         // 
         // Form1
         // 
         this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
         this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
         this->ClientSize = System::Drawing::Size(442, 249);
         this->Controls->Add(this->pictureBox1);
         this->Controls->Add(this->menuStrip1);
         this->MainMenuStrip = this->menuStrip1;
         this->Name = L"Form1";
         this->Text = L"Bildbetrachter";
         this->menuStrip1->ResumeLayout(false);
         this->menuStrip1->PerformLayout();
         (cli::safe_cast<System::ComponentModel::ISupportInitialize^  >(this->pictureBox1))->EndInit();
         this->ResumeLayout(false);
         this->PerformLayout();

      }
#pragma endregion
   private: 
      void oeffnenToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
         {
             // Bild laden
             img = Image::FromFile(openFileDialog1->FileName);
             
             // Rendering des Bildes anstoßen
             pictureBox1->Image = img;
         }
      }
      void speichernToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         if (img == nullptr)
            return;

         // voreingestellten Dateityp an Bildformat anpassen
         if (img->RawFormat->Equals(ImageFormat::Bmp))
            saveFileDialog1->FilterIndex = 1;    
         else if (img->RawFormat->Equals(ImageFormat::Jpeg))
            saveFileDialog1->FilterIndex = 2;
         else 
            saveFileDialog1->FilterIndex = 3;    


         if (saveFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
         {
            if (saveFileDialog1->FileName == openFileDialog1->FileName)
            {
               MessageBox::Show("Zurückschreiben in die alte Datei wird nicht unterstützt");
               return;
            }

            switch (saveFileDialog1->FilterIndex)
            {
            case 1 : img->Save(saveFileDialog1->FileName, ImageFormat::Bmp);    
               break;
            case 2 : img->Save(saveFileDialog1->FileName, ImageFormat::Jpeg);    
               break;
            case 3 : img->Save(saveFileDialog1->FileName, ImageFormat::Png);    
               break;
            }         
         }            
      }
      void beendenToolStripMenuItem_Click(Object^  sender, EventArgs^  e) 
      {
         this->Close();
      }

};
}

