笔记-CppCLR_WinForms操作pictureBox打开,保存,打印图片

1.打开对话框选择图片,显示图片及显示调整

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
    if (openFileDialogPictureShow->ShowDialog() == System::Windows::Forms::DialogResult::OK)
    {
        this->image = Image::FromFile(openFileDialogPictureShow->FileName);
        this->pictureBox1->Image = this->image;
        this->Text = "Picture View(" + openFileDialogPictureShow->FileName + ")";
        if (this->image->Size.Width>this->pictureBox1->Size.Width|| this->image->Size.Height>this->pictureBox1->Size.Height) {

            this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::Zoom;

        }
        else
        {
            this->pictureBox1->SizeMode = System::Windows::Forms::PictureBoxSizeMode::CenterImage;

        }


    }

}

2.打开保存文件对话框选择保存路径,输入保存文件名保存图片

private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) 
{
    if (this->image == nullptr)
    {
        System::Windows::Forms::MessageBox::Show("没有图片", "错误", System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::Error);
        return;
    }
    saveFileDialog->FileName = "IMG_" + System::DateTime::Now.ToString("yyyy-MM-dd_HH-mm-ss");

    if (saveFileDialog->ShowDialog() == System::Windows::Forms::DialogResult::OK)
    {

        this->image->Save(saveFileDialog->FileName, System::Drawing::Imaging::ImageFormat::Jpeg);

        System::Windows::Forms::MessageBox::Show("保存成功", "提示", System::Windows::Forms::MessageBoxButtons::OK,
            System::Windows::Forms::MessageBoxIcon::None);
    }

}

3.调出打印机选项并打印图片

private: System::Void button7_Click(System::Object^  sender, System::EventArgs^  e) 
{   
    this->printDocument1->DocumentName = "IMG_" + System::DateTime::Now.ToString("yyyy-MM-dd_HH-mm-ss");
    //设置Document属性PrintDialog到PrintDocument添加到窗体。
    this->printDialog1->Document = this->printDocument1;
    if (printDialog1->ShowDialog()==System::Windows::Forms::DialogResult::OK)
    {

        this->printDocument1->Print();
    }

}

你可能感兴趣的:(Windows窗体应用)