How to implement Save As for drawing in a C ++ form using Visual Studio

  • Thread starter Thread starter I65
  • Start date Start date
I

I65

Guest
Hello. Please help me.

I use Visual Studio 2015 Community, C++ Windows Forms.

I need: Appearance of any drawing, lines, points, polygons, shapes straight on the Form (Windows Form). Polygon apears directly on surface this Form. I use FillPolygon(), SolidBrush(), TexturedBrush(). I resize the Form and result of FillPolygon() disappears. Is there any method or property for to prevent disappearance?

I draw something and want to save the last changes using Save As, but the last changes are not saved. How to make the last changes saved, by using Save As? Just Save As. I set OverWritePrompt for pictureBox in true.

Is it better to draw in pictureBox exactly to draw, not use .jpeg, gif, tiff images?
I created a ShowDialog(). How do I create an ability to see the saved last step?

Every change of any image in the Form, anywhere, should Save As.

I need to understand the exact algorithm, get the code of the Save As method.

Where can I see the function 'Cancel the last step' (Undo Ctrl+Z) ?

It will be better to get the code with comments on C++. I'll try to understand the code in С# too. Please, in detail not briefly as usual the experienced programmers explain to beginners. I will be happy to receive links of exact url.


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();
// 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;
}

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
}






Continue reading...
 
Back
Top