Rasmlarni o‘zgartirish. Buning uchun fayllarni yuklash openFiledialog va fayllarni saqlash saveFiledialog komponentalaridan foydalanamiz. Buning uchun Brush – mo‘yqalamdan foydalanib, 1 ta komponenta, 3 ta button komponentasini oynaga qulay qilib joylashtiramiz. Button larning birinchisi, rasmni tahrirlashni tozalashni amalga oshiradi, ikkinchisi, tahrirlangan rasmni saqlash uchun, uchinchisi, rasmni tahrirlash uchun yuklashni amalga oshiradi. using namespace System::Drawing::Drawing2D; ni qo‘shishb qo‘yish kerak.
qalam. Rasmlarni o‘zgartirish uchun ishlatiladigan o‘zgaruvchilarni eʻlon qilamiz.
private: Bitmap ^ bmp_for_draw;
private: Point start_point; private: bool Draw; public: Pen^ pen_for_draw;
private: String ^ full_name_of_image;
qadam. Dastur oynasining Form1_Load hodisasiga dastlabki sozlamalarni
o‘rnatamiz.
this->Text = "Rasmni tahrirlash"; button1->Text = "Tozalash"; button2->Text= "Rasmni saqlash"; button3->Text = "Rasmni yuklash";
pen_for_draw = gcnew Pen(Color::Black, 4);
pen_for_draw->StartCap = System::Drawing::Drawing2D::LineCap::Round; pen_for_draw->EndCap = System::Drawing::Drawing2D::LineCap::Round;
Bunda komponentalarni sozlash va bir qalamni yaratib olish, uning StartCap, EndCap xususiyatlarini o‘rnatish amalga oshiriladi.
qadam. Komponentdagi rasmni tozalash uchun button1_Click hodisaga algoritmni yozamiz.
Graphics^ grp = pictureBox1->CreateGraphics(); grp->Clear(SystemColors::Window);
qadam. Sichqoncha komponentaning ustida kelganda chizishi uchun
pictureBox1_MouseDown hodisasiga quyidagi algoritmni kiritamiz
if (e->Button == System::Windows::Forms::MouseButtons::Left){
Draw = true;
start_point = e->Location;
}
qadam. Sichqoncha komponentaning ustida ketganda chizishni to‘xtatish uchun pictureBox1_MouseUp hodisasiga quyidagi algoritmni kiritamiz
if (e->Button == System::Windows::Forms::MouseButtons::Left){
Draw = false;
}
qadam. Komponentaga kerakli rasmni yuklash uchun button3_Click
hodisasiga quyidagi algoritmni yoziladi
OpenFileDialog ^ open_dialog = gcnew OpenFileDialog();
open_dialog->Filter = "Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*";
if (open_dialog->ShowDialog() == System::Windows::Forms::DialogResult::OK){ try{
full_name_of_image = open_dialog->FileName; bmp_for_draw = gcnew Bitmap(open_dialog->FileName);
//pictureBox1->Size = bmp_for_draw->Size;
pictureBox1->SizeMode = PictureBoxSizeMode::StretchImage; pictureBox1->Image = bmp_for_draw;
pictureBox1->Invalidate();
} catch(Exception^ e) {
System::Windows::Forms::DialogResult rezult = MessageBox::Show("Tanlangan faylni ochib bo'lmaydi "+ e->ToString(),"Diqqat", MessageBoxButtons::OK, MessageBoxIcon::Error);
}}
Bundagi asosi nuqson shundan iboratki, agar rasmning o‘lchamlari
komponentaning o‘lchamlari bilan bir xil bo‘lsa, yaxshi chizadi, bir xil bo‘lmasa, foydalanuvchi ekranining proporsiyasini olib chizadi.
qadam. Komponentadagi rasmni saqlash uchun button2_Click hodisasiga quyidagi algoritmni yoziladi
if (pictureBox1->Image != nullptr){
String ^format = full_name_of_image->Substring(full_name_of_image->Length - 4, 4); SaveFileDialog ^savedialog = gcnew SaveFileDialog();
savedialog->Title = "Rasmni saqlash ..."; savedialog->OverwritePrompt = true; savedialog->CheckPathExists = true;
savedialog->Filter = "Image Files(*.BMP)|*.BMP|Image Files(*.JPG)|*.JPG|Image Files(*.GIF)|*.GIF|Image Files(*.PNG)|*.PNG|All files (*.*)|*.*";
savedialog->ShowHelp = true;
// If selected, save
if (savedialog->ShowDialog() == System::Windows::Forms::DialogResult::OK){ try{
bmp_for_draw->Save(savedialog->FileName, System::Drawing::Imaging::ImageFormat::Jpeg);
} catch(Exception ^ e) {
System::Windows::Forms::MessageBox::Show("Impossible to save image"+e->ToString(), "FATAL ERROR", MessageBoxButtons::OK, MessageBoxIcon::Error);
} } }
qadam. Sichqoncha komponentaning ustida kelganda chizishi uchun
quyidagi algoritmni kiritamiz.
if (e->Button == System::Windows::Forms::MouseButtons::Left){ Graphics ^graf = Graphics::FromImage(pictureBox1->Image);
graf->DrawLine(pen_for_draw, start_point, e->Location); start_point = e->Location;
pictureBox1->Invalidate();
}
Dasturni ishlatganda quyidagi oyna chiqadi.
12.6-rasm. Rasmlarni tahrirlash.
Dostları ilə paylaş: |