Вывод изображения в интерфейс
Скачать 0.69 Mb.
|
МИНОБРНАУКИ РОССИИ САНКТ-ПЕТЕРБУРГСКИЙ ГОСУДАРСТВЕННЫЙ ЭЛЕКТРОТЕХНИЧЕСКИЙ УНИВЕРСИТЕТ «ЛЭТИ» ИМ. В.И. УЛЬЯНОВА (ЛЕНИНА) Кафедра ТВ ОТЧЕТ по лабораторной работе №14 по дисциплине «Информатика» Тема: ВЫВОД ИЗОБРАЖЕНИЯ В ИНТЕРФЕЙС. ВЗАИМОДЕЙСТВИЕ С МЫШЬЮ.
Санкт-Петербург 2022 Цель работы: знакомство с принципами загрузки и работы с изображениями, а также основами взаимодействия с мышью в графическом интерфейсе. Упражнение 1. #pragma once namespace WinFormsTest { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); } protected: MyForm() { if (components) { delete components;} } private: Bitmap^ img1; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; private: System::Windows::Forms::PictureBox^ pictureBox1; private: System::Windows::Forms::PictureBox^ pictureBox2; private: System::Windows::Forms::PictureBox^ pictureBox3; private: System::Windows::Forms::PictureBox^ pictureBox4; private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog()); this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox()); this->pictureBox3 = (gcnew System::Windows::Forms::PictureBox()); this->pictureBox4 = (gcnew System::Windows::Forms::PictureBox()); (cli::safe_cast (cli::safe_cast (cli::safe_cast (cli::safe_cast this->SuspendLayout(); // button1 this->button1->Location = System::Drawing::Point(430, 3); this->button1->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(228, 37); this->button1->TabIndex = 0; this->button1->Text = L"Выберите картинку"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // openFileDialog1 this->openFileDialog1->FileName = L"openFileDialog1"; // pictureBox1 this->pictureBox1->Location = System::Drawing::Point(279, 62); this->pictureBox1->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(195, 213); this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->pictureBox1->TabIndex = 1; this->pictureBox1->TabStop = false; // pictureBox2 this->pictureBox2->Location = System::Drawing::Point(30, 62); this->pictureBox2->Name = L"pictureBox2"; this->pictureBox2->Size = System::Drawing::Size(195, 213); this->pictureBox2->TabIndex = 2; this->pictureBox2->TabStop = false; // pictureBox3 this->pictureBox3->Location = System::Drawing::Point(533, 62); this->pictureBox3->Name = L"pictureBox3"; this->pictureBox3->Size = System::Drawing::Size(195, 213); this->pictureBox3->SizeMode = System::Windows::Forms::PictureBoxSizeMode::AutoSize; this->pictureBox3->TabIndex = 3; this->pictureBox3->TabStop = false; // pictureBox4 this->pictureBox4->Location = System::Drawing::Point(767, 62); this->pictureBox4->Name = L"pictureBox4"; this->pictureBox4->Size = System::Drawing::Size(195, 213); this->pictureBox4->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom; this->pictureBox4->TabIndex = 4; this->pictureBox4->TabStop = false; // MyForm this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(974, 323); this->Controls->Add(this->pictureBox4); this->Controls->Add(this->pictureBox3); this->Controls->Add(this->pictureBox2); this->Controls->Add(this->button1); this->Controls->Add(this->pictureBox1); this->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->Name = L"MyForm"; this->Text = L"MyForm"; (cli::safe_cast (cli::safe_cast (cli::safe_cast (cli::safe_cast this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { pictureBox1->Image = Image::FromFile(openFileDialog1->FileName); pictureBox2->Image = Image::FromFile(openFileDialog1->FileName); pictureBox3->Image = Image::FromFile(openFileDialog1->FileName); pictureBox4->Image = Image::FromFile(openFileDialog1->FileName); } } }; } Упражнение 2. namespace WinFormsTest { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); } protected: MyForm() { if (components) { delete components; } } private: Bitmap^ img1; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; private: System::Windows::Forms::PictureBox^ pictureBox1; private: System::Windows::Forms::PictureBox^ pictureBox2; private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog()); this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); this->pictureBox2 = (gcnew System::Windows::Forms::PictureBox()); (cli::safe_cast (cli::safe_cast this->SuspendLayout(); // button1 this->button1->Location = System::Drawing::Point(409, 3); this->button1->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(228, 37); this->button1->TabIndex = 0; this->button1->Text = L"Выберите картинку"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // openFileDialog1 this->openFileDialog1->FileName = L"openFileDialog1"; // pictureBox1 this->pictureBox1->Location = System::Drawing::Point(205, 62); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(237, 236); this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->pictureBox1->TabIndex = 2; this->pictureBox1->TabStop = false; // pictureBox2 this->pictureBox2->Location = System::Drawing::Point(609, 62); this->pictureBox2->Name = L"pictureBox2"; this->pictureBox2->Size = System::Drawing::Size(223, 236); this->pictureBox2->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->pictureBox2->TabIndex = 4; this->pictureBox2->TabStop = false; // MyForm this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(938, 323); this->Controls->Add(this->pictureBox2); this->Controls->Add(this->pictureBox1); this->Controls->Add(this->button1); this->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->Name = L"MyForm"; this->Text = L"MyForm"; (cli::safe_cast (cli::safe_cast this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { Bitmap^ img1 = gcnew Bitmap(openFileDialog1->FileName); Bitmap^ img2 = gcnew Bitmap(img1->Width, img1->Height); pictureBox1->Image = img1; for (int i = 0; i < img1->Width; i++) for (int j = 0; j < img1->Height; j++) { Color cl1 = img1->GetPixel(i, j); Color cl2 = Color::FromArgb(cl1.R * 0.299 + cl1.G* 0.587 + cl1.B * 0.114, cl1.R * 0.299 + cl1.G * 0.587 + cl1.B * 0.114, cl1.R * 0.299 + cl1.G * 0.587 + cl1.B * 0.114); img2->SetPixel(i, j, cl2); } pictureBox2->Image = img2; } } }; } Упражнение 3. namespace WinFormsTest { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void) { InitializeComponent(); } protected: MyForm() { if (components) { delete components; } } private: Bitmap^ img1; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; private: System::Windows::Forms::PictureBox^ pictureBox1; private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog()); this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); (cli::safe_cast this->SuspendLayout(); // button1 this->button1->Location = System::Drawing::Point(409, 3); this->button1->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(228, 37); this->button1->TabIndex = 0; this->button1->Text = L"Выберите картинку"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // openFileDialog1 this->openFileDialog1->FileName = L"openFileDialog1"; // pictureBox1 this->pictureBox1->Location = System::Drawing::Point(325, 46); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(392, 265); this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->pictureBox1->TabIndex = 2; this->pictureBox1->TabStop = false; this->pictureBox1->MouseClick += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::pictureBox1_MouseClick); this->pictureBox1->MouseMove += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::pictureBox1_MouseClick); // MyForm this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(938, 323); this->Controls->Add(this->pictureBox1); this->Controls->Add(this->button1); this->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->Name = L"MyForm"; this->Text = L"MyForm"; (cli::safe_cast this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { img1 = gcnew Bitmap(openFileDialog1->FileName); pictureBox1->Image = img1; } } private: System::Void pictureBox1_MouseClick(System::Object ^ sender, System::Windows::Forms::MouseEventArgs ^ e) { for (int i = -5; i < 5; i++) for (int j = -5; j < 5; j++) { if ((e->X + i > 0) && (e->X + i < img1->Width) && (e->Y + j > 0) && (e->Y + j < img1->Height)) { if (e->Button == System::Windows::Forms::MouseButtons::Left) { img1->SetPixel(e->X + i, e->Y + j, Color::White); } } } pictureBox1->Image = img1; } }; } Упражнение 4. namespace WinFormsTest { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; public ref class MyForm : public System::Windows::Forms::Form { public: MyForm(void){ InitializeComponent(); } protected: MyForm() { if (components){delete components;} } private: Bitmap^ img1; private: System::Windows::Forms::Button^ button1; private: System::Windows::Forms::OpenFileDialog^ openFileDialog1; private: System::Windows::Forms::PictureBox^ pictureBox1; private: System::ComponentModel::Container ^components; #pragma region Windows Form Designer generated code void InitializeComponent(void) { this->button1 = (gcnew System::Windows::Forms::Button()); this->openFileDialog1 = (gcnew System::Windows::Forms::OpenFileDialog()); this->pictureBox1 = (gcnew System::Windows::Forms::PictureBox()); (cli::safe_cast this->SuspendLayout(); // button1 this->button1->Location = System::Drawing::Point(357, 3); this->button1->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(228, 37); this->button1->TabIndex = 0; this->button1->Text = L"Выберите картинку"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click); // openFileDialog1 this->openFileDialog1->FileName = L"openFileDialog1"; // pictureBox1 this->pictureBox1->Location = System::Drawing::Point(204, 46); this->pictureBox1->Name = L"pictureBox1"; this->pictureBox1->Size = System::Drawing::Size(534, 359); this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::StretchImage; this->pictureBox1->TabIndex = 2; this->pictureBox1->TabStop = false; this->pictureBox1->MouseClick += gcnew System::Windows::Forms::MouseEventHandler(this, &MyForm::pictureBox1_MouseClick); // MyForm this->AutoScaleDimensions = System::Drawing::SizeF(8, 16); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(938, 417); this->Controls->Add(this->pictureBox1); this->Controls->Add(this->button1); this->Margin = System::Windows::Forms::Padding(4, 3, 4, 3); this->Name = L"MyForm"; this->Text = L"MyForm"; (cli::safe_cast this->ResumeLayout(false); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { if (openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK) { img1 = gcnew Bitmap(openFileDialog1->FileName); pictureBox1->Image = img1; } } private: System::Void pictureBox1_MouseClick(System::Object ^ sender, System::Windows::Forms::MouseEventArgs ^ e) { if (e->Button == System::Windows::Forms::MouseButtons::Left) { for (int i = 0; i < img1->Width; i++) { img1->SetPixel(i, e->Y, Color::Red); } for (int j = 0; j < img1->Height; j++) { img1->SetPixel(e->X, j, Color::Red); } } pictureBox1->Image = img1; } };} Вывод: в данной лабораторной работе я ознакомился с принципами загрузки и работы с изображениями, а также основами взаимодействия с мышью в графическом интерфейсе. |