如何使用“另存为”对图像进行最后保存?

时间:2018-07-29 15:05:27

标签: c++-cli

请帮助我。我是C ++,C#的菜鸟。我使用Visual Studio 2015社区,C ++ Windows窗体。 我的问题:我花了很多时间在Windows窗体上实现“另存为”,以及如何实现“另存为”。 在msdn,stackoverflow,google上搜索信息并没有帮助我。

我需要:直接在窗体(Windows窗体)上显示任何图形,线条,点,多边形,形状。多边形直接在此表格的表面上消失。我使用FillPolygon(),SolidBrush(),TexturedBrush()。 我调整窗体的大小,并且FillPolygon()的结果消失了。有什么方法或属性可以防止失踪吗?

如何使充满颜色的多边形不消失?我知道,最好使用pictureBox进行绘制,是否有任何方法可以将FillPolygon(),DrawLine(),DrawEllipse()实现到pictureBox中?如果有,该如何实施?

我画了些东西,想用“另存为”保存最近的更改,但是最后的更改没有保存。如何使用“另存为”保存最近的更改?只需另存为。我将pictureBox的OverWritePrompt设置为true。

最好直接在pictureBox中绘制图片而不使用.jpeg,gif,tiff图像吗?
我创建了一个ShowDialog()。如何创建查看已保存的最后一步的功能?

表单中任何位置的任何图像的任何更改都应另存为。我不是要编写CorelDraw,Photoshop,而是要编写另存为的代码。我需要了解确切的算法,获取另存为方法的代码。

我在哪里可以看到“取消最后一步”功能(撤消Ctrl + Z)?

获取带有C ++注释的代码会更好。我也会尝试理解С#中的代码。请详细介绍经验丰富的程序员,而不是像往常一样简短地向初学者讲解。我很高兴收到确切网址的链接。

我的代码:

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::Drawing2D; 
using namespace System::Drawing::Imaging;                   
using namespace System::IO;

public ref class Creat : public System::Windows::Forms::Form
    {
    private:
        Bitmap ^nak;    // variable Bitmap image

    PictureBox^ pictureBox4;

public:
    void SaveAs(String^ filename)          // Save As method
    {
        throw gcnew System::NotImplementedException();
    }
   // constructor       
        Creat(void)
        {
            InitializeComponent();          
            //TODO: Add the constructor code here

   // setting the openDialog1 component

   openFileDialog1->AddExtension = true;
   openFileDialog1->Filter = "Image Files(*.bmp)|*.bmp|Image Files(*.JPG)|*.JPG|Image Files(*.jpeg)|*.jpeg|Image Files(*.GIF)|*.GIF|Image Files(*.emf)|*.emf|Image Files(*.ico)|*.ico|Image Files(*.png)|*.png|Image Files(*tif)|*.tif|Image Files(*.wmf)|*.wmf|Image Files(*.exif)|*.exif|All files(*.*)|*.*";
   openFileDialog1->FilterIndex = 2;
   openFileDialog1->DefaultExt = "jpg";
   openFileDialog1->Title = "Open Document";            
   openFileDialog1->Multiselect = false;
   openFileDialog1->CheckFileExists = true;

    // setting the saveFileDialog1 component

    saveFileDialog1->AddExtension = true;           
    saveFileDialog1->Filter = "Image Files(*.bmp)|*.bmp|Image Files(*.JPG)|*.JPG|Image Files(*.jpeg)|*.jpeg|Image Files(*.GIF)|*.GIF|Image Files(*.emf)|*.emf|Image Files(*.ico)|*.ico|Image Files(*.png)|*.png|Image Files(*.tif)|*.tif|Image Files(*.wmf)|*.wmf|Image Files(*.exif)|*.exif|All files(*.*)|*.*";
    saveFileDialog1->FilterIndex = 2;
    saveFileDialog1->DefaultExt = "jpg";            
    saveFileDialog1->Title = "Save Document";
    saveFileDialog1->CheckFileExists = true;

    fn = String::Empty;         // fn- file name
    imageChanged = false;
}

protected:
/// Clean up any resources being used.
        /// </summary>
        ~Creat()
        {
            if (components)
            {
                delete components;
            }
        }
private: System::Windows::Forms::Button^  button1; 
private: System::Windows::Forms::PictureBox^  pictureBox1;
...
private: System::Windows::Forms::SaveFileDialog^  saveFileDialog1;

...
private: System::Windows::Forms::OpenFileDialog^  openFileDialog1;
private:

/// Required designer variable.

System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code

/// Required method for Designer support - do not modify

    void InitializeComponent(void)
    {
       ...
       // saveFileDialog1

       this->saveFileDialog1->Filter = resources->GetString(L"saveFileDialog1.Filter");

        // openFileDialog1

        this->openFileDialog1->FileName = L"openFileDialog1";
        this->openFileDialog1->Filter = resources->GetString(L"openFileDialog1.Filter");
    ...
    }
    #pragma endregion

    String^ fn;                 // fn - file name
    bool imageChanged;   // true - if I made changes to the image

    // The image has changed in PictureBox'e 'or in the Form

    private: System::Void pictureBox1_BackgroundImageChanged(System::Object^  sender, System::EventArgs^  e) 
    {
        imageChanged = true;                        
    }

    // Writes an image to a file. 
    //Returns 0 or -1 if the user in the Save window clicks Cancel

    private: int ImageToFile()
    {
        System::Windows::Forms::DialogResult dr;
        int r = 0;                                  

        if (fn == String::Empty)
        {
            // this is a new document, ask the user for the file name.  
            // display the Save dialog

            dr = saveFileDialog1->ShowDialog();
            if (dr == System::Windows::Forms::DialogResult::OK)
            {
                fn = saveFileDialog1->FileName;
                r = 0;
            }
                else
                r = -1; // user did not save. click on the Cancel 
                        //button
        }

        // save file

        if (r == 0)
        {
            try
            {
                // get information about the file fn
                System::IO::FileInfo^ fi = gcnew System::IO::FileInfo(fn);

                System::IO::FileStream^ fs = safe_cast<System::IO::FileStream^>(saveFileDialog1->OpenFile());

                // stream for writing
                System::IO::StreamWriter^ sw = fi->CreateText();
                sw->Write(pictureBox1->Image);
                sw->Close();     // close the stream
                imageChanged = false;
                r = 0;
            }
            catch (System::IO::IOException^ e)
            {
                MessageBox::Show(e->ToString(), "BeautifulLady", MessageBoxButtons::OK, MessageBoxIcon::Information);
            }
        }
        return r;
    }

    // Check if there are any changes to the Image and save the Image 
    //in the file.
    // Returns: 0 or -1 if the user declines to save (click Cancel)

private: int SaveImg()
{
    System::Windows::Forms::DialogResult dr;
    int r;
    r = 0;

    if (imageChanged) // The image has been changed. Do you want to 
     // save it?
    {
        dr = MessageBox::Show("The image has been changed. Do you want to save it?", "BeautifulLady", MessageBoxButtons::YesNoCancel, MessageBoxIcon::Warning);

        switch (dr)
        {
        case System::Windows::Forms::DialogResult::Yes: 
            r = ImageToFile();    
            break;
        case System::Windows::Forms::DialogResult::No:
            r = 0;
            break;
        case System::Windows::Forms::DialogResult::Cancel:
            r = -1;
            break;
        }
    }
    return r;
}

        // Open document

private: void OpenDocument()
{
    System::Windows::Forms::DialogResult dr;
    int r;
    r = SaveImg();

    if (r == 0)
    {
        openFileDialog1->FileName = String::Empty;

        // showing the Open window

        dr = openFileDialog1->ShowDialog();
        if (dr == System::Windows::Forms::DialogResult::OK)
        {
            fn = openFileDialog1->FileName;

                // show file name in the title of the window

            this->Text = fn;

    // large object (Blob) - image file

    String^ sBlobFile = "C:\\Users\\Igor\\Pictures\\model.jpg";    

try
{
    // I read data from a file
    System::IO::StreamReader^ sr = gcnew System::IO::StreamReader(fn);

    FileStream^ fsBlobData = gcnew FileStream(sBlobFile, System::IO::FileMode::Open, System::IO::FileAccess::Read);

    System::IO::FileStream^ fs = gcnew System::IO::FileStream(fn, System::IO::FileMode::Open);
    Byte *bytBlobdata = new Byte[fsBlobData->Length];

//  fsBlobData->Read(bytBlobdata, 0, bytBlobdata->Length);
//  sBlobFile->Read(bytBlobdata, 0, bytBlobdata->Length);
//  fsBlobData.Close();
//  MemoryStream stmBlobData = new MemoryStream(bytBlobData);
//  FileStream dumpFile = new FileStream("C:\Dump.jpg", 
//  FileMode.Create, FileAccess.ReadWrite);
//  stmBlobData.WriteTo(dumpFile);
//  stmBlobData.Close();
//  pictureBox1->Image = fs->BeginRead();
    fs->Close();
    File::Delete(sBlobFile);

    FileStream^ dumpFile = gcnew 
FileStream("C:\\Users\\Igor\\Pictures\\Dump.jpg", 
                                                System::IO::FileMode::Create, System::IO::FileAccess::ReadWrite);

//  System::IO::BinaryReader^ br = gcnew System::IO::BinaryReader(fn);    

    Image^ img = Image::FromStream(fs);
//  Bitmap^ bmp = Image::FromStream(fs);
//  pictureBox1->Text = sr->ReadToEnd();                        
//  pictureBox1->SelectionStart = pictureBox1->TextLength;          

    img->Save(fn, System::Drawing::Imaging::ImageFormat::Jpeg);
    imageChanged = false;
}
catch (System::IO::FileLoadException^ e)
    {
       MessageBox::Show("Error of reading the image file\n" + e->Message, "BeautifulLady", MessageBoxButtons::OK,
                        MessageBoxIcon::Error);                  
    }
}           
}
}
    // Save the document

    private: void SaveDocument()
    {
        System::Windows::Forms::DialogResult dr;
        int r;
        r = SaveImg();

            if (r == 0)
            {
                this->Text = fn;
                imageChanged = false;

                // show Save dialog
                dr = saveFileDialog1->ShowDialog();
            }           
        }

// create a document

private: void NewDocument()
{
    int r;
    r = SaveImg();

    if (r == 0)
    {
        this->Text = "New Image";

            // to paint over the previous one

            Color ^col = gcnew Color();
            Graphics ^im = this->CreateGraphics();
            im->Clear(col->White);
            imageChanged = false;
            fn = String::Empty;
        }
    }

// User attempts to close the program window

private: System::Void Creat_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) 
{
    int r;
    r = SaveImg();
        if (r != 0)
        {
            e->Cancel = true;
        }
    }

// function to save the polygon

private: void Save_Restore(PaintEventArgs^ e)
{
    Graphics^ og = this->CreateGraphics();  

    // I create a polygon
    SolidBrush ^sb = gcnew SolidBrush(Color::Blue);

    Point pa1 = Point(850, 200);
    Point pa2 = Point(900, 250);
    Point pa3 = Point(930, 270);
    Point pa4 = Point(950, 280);
    Point pa5 = Point(850, 200);

    array<Point>^ pP = { pa1,pa2,pa3,pa4,pa5, };
    og->FillPolygon(sb, pP);

    // I save the polygon
    SaveImg();
    SaveDocument();
    GraphicsState^ creaPolState = e->Graphics->Save();
}

// Save (click the Save button)

private: System::Void button16_Click(System::Object^  sender, System::EventArgs^  e) 
{
    saveFileDialog1->ShowDialog();
    Bitmap^ bitmap = gcnew Bitmap(Convert::ToInt32(1024), Convert::ToInt32(1024), System::Drawing::Imaging::PixelFormat::Format32bppArgb);
        Graphics^ g = Graphics::FromImage(bitmap);

    g = this->CreateGraphics();

    // I create a polygon

    SolidBrush ^sb = gcnew SolidBrush(Color::Blue);

        Point pa1 = Point(150, 200);
        Point pa2 = Point(200, 250);
        Point pa3 = Point(230, 270);
        Point pa4 = Point(150, 200);        

        array<Point>^ pP = { pa1,pa2,pa3,pa4 };
        g->FillPolygon(sb, pP);

        g->DrawImage(bitmap, (sb, (pa1, pa2, pa3, pa4)));       

        bitmap->Save("C:\\Users\\Igor\\Pictures\\savepol.jpg", System::Drawing::Imaging::ImageFormat::Jpeg);

        SaveImg();
        SaveDocument();
private: String^ full_name_of_image;    // full pathname + name
private: Bitmap^ bitmap_for_draw;   // bitmap for drawing 

        // button to display the image in pictureBox1

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e)
{
        // Get image

    nak = gcnew Bitmap(Application::StartupPath + "\\model.jpg", true);  
    pictureBox1->Image = nak; // PictureBox to display the image                        

        // "Open" dialog box

    OpenFileDialog^ opf = gcnew OpenFileDialog();
    opf->Filter = "Image Files(*.BMP)|*.BMP|Image Files(*.JPG)|*.JPG|Image Files(*.GIF)|*.GIF|Image Files(*.PNG)|*.PNG|All files(*.*)|*.*";

if (opf->ShowDialog() == System::Windows::Forms::DialogResult::Yes)
{           
    try
    {
        full_name_of_image = opf->FileName;
        bitmap_for_draw = gcnew Bitmap(opf->FileName);
        this->pictureBox1->SizeMode = PictureBoxSizeMode::CenterImage;
        bitmap_for_draw = nak;
        pictureBox1->Image = bitmap_for_draw;
        pictureBox1->Invalidate();
    }
    catch (IOException^)
    {
        System::Windows::Forms::DialogResult^ result;
        result = MessageBox::Show("Impossible to open selected file\n," + "probably the file does not exists.", "Beautiful Lady", MessageBoxButtons::OK, MessageBoxIcon::Information);
    }
  }
}

// Drawing on model in pictureBox1

bool Draw;          // drawing variable in pictureBox1
private: System::Void button19_Click(System::Object^  sender, System::EventArgs^  e)
{
    Graphics^ g = pictureBox1->CreateGraphics();        

}

// User moves the mouse and draws on the model

private: System::Void pictureBox1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
{
     Graphics^ gr = pictureBox1->CreateGraphics();
     SolidBrush ^sb = gcnew SolidBrush(Color::AliceBlue);
     if (Draw)
     {
         gr->FillRectangle(sb, e->X, e->Y, 1, 1);   // brush thickness 
         // filling with rectangles
     }
}   
// User pressed the mouse button

private: System::Void pictureBox1_MouseDown(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
{
    Draw = true;
}   

// User let go of the mouse     
private: System::Void pictureBox1_MouseUp(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
{
    Draw = false;
}

// to use the Save method.'Save Model' button

private: System::Void button15_Click(System::Object^  sender, System::EventArgs^  e)
{           
    Bitmap ^myImage = gcnew 
    Bitmap("C:\\Users\\Igor\\Pictures\\orgn.jpg");

    if (pictureBox1->Image != nullptr)
    {
        full_name_of_image = "C:\\Users\\Igor\\Pictures\\orgn1.jpg";
        String^ format = full_name_of_image->Substring(full_name_of_image->Length - 5, 4);  

        SaveFileDialog^ sd = gcnew SaveFileDialog();
        sd->Title = "Save image As";
        sd->CheckPathExists = true;
        sd->CheckFileExists = true;
        sd->AddExtension = true;
        sd->Filter = "Image Files(*.BMP)|*.BMP|Image Files(*.JPG)|*.JPG|Image Files(*.GIF)|*.GIF|Image Files(*.PNG)|*.PNG|All files(*.*)|*.*";
        sd->FilterIndex = 2;
        sd->DefaultExt = "jpg";
        sd->OverwritePrompt = true;
        sd->ShowHelp = true;

        // if user choses to save

    if (sd->ShowDialog() == System::Windows::Forms::DialogResult::Yes)
    {
        this->saveFileDialog1->ShowDialog();
        fn = saveFileDialog1->FileName;
        SaveImg();
        SaveDocument();

        try
        {
            //sd->FileName = "C:\\Users\\Igor\\Pictures\\orgn1.jpg";
            myImage->Save(sd->FileName, System::Drawing::Imaging::ImageFormat::Jpeg);                   
        }
        catch(IOException^)
        {
            MessageBox::Show("Impossible to save this image", "Beautiful Lady", MessageBoxButtons::OK, MessageBoxIcon::Information);
        }   
        pictureBox1->Image->Save(sd->FileName, System::Drawing::Imaging::ImageFormat::Jpeg);
     }

    }
}

// draws the model

private: System::Void Creat_Paint(System::Object^ sender, System::Windows::Forms::PaintEventArgs^ e)
{   
    // for not to write e->Graphics every time

    System::Drawing::Graphics ^g = e->Graphics;

    // I draw a model at 508 horizontally, 184 vertically
    Graphics ^nak = this->CreateGraphics();         
 }

// The SizeChanged event occurs when the form size is changed

private: System::Void Creat_SizeChanged(System::Object^  sender, System::EventArgs^  e) 
{
    // The Refresh method: the form needs to be redrawn
    this->Refresh();        // update form
}

0 个答案:

没有答案