Click to See Complete Forum and Search --> : Who can figure this out?!(CImage & Pictureboxes)


CPPMe
July 27th, 2004, 06:35 PM
Hi evrybody,

Im currently making a basic paint program in vc++.net that uses win32 API and GDI+. Im currently writing a save function. I created the sve file dialog(SaveFile) and the picturebox that gets saved (pictureBox1). Here is the code:

...
private: System::Void btnSave_Click(System::Object * sender, System::EventArgs * e)
{
if (SaveFile->ShowDialog() == DialogResult::OK)
{
CImage myImage = pictureBox1->Image; //line 229
myImage.Save(SaveFile->get_FileName,ImageFormat::Bmp); //line 330
}
}
...
When i try to compile the code i get this errror:

c:\Projects\Rectangle\Form1.h(229) : error C2440: 'initializing' : cannot convert from 'System::Drawing::Image __gc *' to 'ATL::CImage'
No constructor could take the source type, or constructor overload resolution was ambiguous
c:\Projects\Rectangle\Form1.h(230) : error C2664: 'HRESULT ATL::CImage::Save(IStream *,const GUID &) throw() const' : cannot convert parameter 1 from 'System::String __gc *(void)' to 'IStream *'
There is no context in which this conversion is possible

Does anyone have any idea of what is going on?I dont=). If you do, please help a newb out

Thanks again,
Tim

Vanaj
July 27th, 2004, 06:41 PM
Can you post the code for c:\Projects\Rectangle\Form1.h(229) and c:\Projects\Rectangle\Form1.h(230)

CPPMe
July 27th, 2004, 06:58 PM
just changed it, look at commenting on source

Vanaj
July 27th, 2004, 07:03 PM
just changed it, look at commenting on source

I dont see line 229 or 230 of Form1.H in your code.

CPPMe
July 27th, 2004, 07:14 PM
///////////////////////////////////////
//Form1.h
///////////////////////////////////////

private: System::Void btnSave_Click(System::Object * sender, System::EventArgs * e)
{
if (SaveFile->ShowDialog() == DialogResult::OK)
{
CImage myImage = pictureBox1->Image; //line 229!!!
myImage.Save(SaveFile->get_FileName,ImageFormat::Bmp); //line 330!!!
}
}
...

It was also on the first post, just gotta look more closely

Vanaj
July 27th, 2004, 07:33 PM
How are the following defined ?

pictureBox1->Image
SaveFile->get_FileName

CPPMe
July 27th, 2004, 08:03 PM
private: System::Windows::Forms::SaveFileDialog * SaveFile;
private: System::Windows::Forms::PictureBox * pictureBox1;

Vanaj
July 27th, 2004, 08:10 PM
private: System::Windows::Forms::SaveFileDialog * SaveFile;
private: System::Windows::Forms::PictureBox * pictureBox1;

OK..now how is Image defined in pictureBox1 ?
OK..now how is get_FileName defined in SaveFileDialog ?

Vanaj
July 27th, 2004, 10:06 PM
c:\Projects\Rectangle\Form1.h(229) : error C2440: 'initializing' : cannot convert from 'System::Drawing::Image __gc *' to 'ATL::CImage'
No constructor could take the source type, or constructor overload resolution was ambiguous
pictureBox1->ImageIs returning the following typeSystem::Drawing::Image __gc *But CImage myImage =Is looking for the typeATL::CImage


c:\Projects\Rectangle\Form1.h(230) : error C2664: 'HRESULT ATL::CImage::Save(IStream *,const GUID &) throw() const' : cannot convert parameter 1 from 'System::String __gc *(void)' to 'IStream *'
There is no context in which this conversion is possible

SaveFile->get_FileName Is returning the following typeSystem::String __gc *(void)ButmyImage.Save()is looking for the following types of param passed to it.HRESULT Save(IStream* pStream,REFGUID guidFileType)
HRESULT Save(LPCTSTR pszFileName,REFGUID guidFileType= GUID_NULL)


Looks like some converting or casting is needed prior to assignments or function calls. I'm sure someone here can supply you with this info.Maybe if one of the Moderators move this to VC++ .NET you might get a better response.

CPPMe
July 28th, 2004, 12:04 AM
After much trial and error, i came up with the following code:


if (SaveFile->ShowDialog() == DialogResult::OK)
{
CString fileName = SaveFile->FileName->ToString();
pictureBox1->get_Image()->Save(fileName);
}



It builds fine, and starts fine, however, when i try to save, a little dialog box, titled "Rectangles"(the name of my app) and says: "An unhandled exception has occured..." after i select the details thing, i get the following error :




************** Exception Text **************
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams)
at System.Drawing.Image.Save(String filename, ImageFormat format)
at Rectangle1.Form1.btnSave_Click(Object sender, EventArgs e) in c:\projects\rectangle\form1.h:line 230
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Vanaj
July 28th, 2004, 12:11 AM
if (SaveFile->ShowDialog() == DialogResult::OK)
{
CString fileName = SaveFile->FileName->ToString();
pictureBox1->get_Image()->Save(fileName);
}
Does the Saved filename have an extention of a valid image name. (jpg or bmp)

CPPMe
July 28th, 2004, 12:46 AM
I just changed the pictureBox1->get_Image()->Save(fileName); to Save(fileName,ImageFormat::Bmp);
But, i still get that same stupid error!!!!!,

Here is the whole function, maybe that will help:


private: System::Void btnSave_Click(System::Object * sender, System::EventArgs * e)
{

SaveFile->Filter = S"Image files (*.bmp)|*.bmp|All files (*.*)|*.*";
if (SaveFile->ShowDialog() == DialogResult::OK)
{
CString fileName = SaveFile->FileName->ToString();
(READ COMMENTING)-pictureBox1->get_Image()->Save(fileName,ImageFormat::Bmp);
}
else{
Close();
}
}


Hope this helps me help you help me. =)

Thanks sooo much Vanaj!,
Im.. ...err Were so close, i can smell it! :D

Vanaj
July 28th, 2004, 01:29 AM
Lets do one thing at a time, try removing/comment out the Save() statement.

CPPMe
July 28th, 2004, 02:15 AM
Oooops, lol, the new save statement is:

pictureBox1->get_Image()->Save(fileName,ImagFormat::Bmp);


sry about the confusion

Vanaj
July 28th, 2004, 02:20 AM
Oooops, lol, the new save statement is:

pictureBox1->get_Image()->Save(fileName,ImagFormat::Bmp);

sry about the confusion
Got that, I wanted you to try removing just the Save() part i.e.
pictureBox1->get_Image();and see if it still crashes.

CPPMe
July 28th, 2004, 02:52 AM
Builds and runs fine.

THANK YOU SO MUCH, Were gonna figure this out eventually =)

Vanaj
July 28th, 2004, 03:08 AM
Now this NEW code should work.

CImage myImage = pictureBox1->getImage();
myImage.Save(fileName,ImagFormat::Bmp);

CPPMe
July 28th, 2004, 03:20 AM
I inserted the code and modified it to:


CImage myImage = pictureBox1->get_Image;
myImage.Save(fileName)


I get the following error:

c:\Projects\Rectangle\Form1.h(243) : error C2440: 'initializing' : cannot convert from 'overloaded-function' to 'ATL::CImage'
No constructor could take the source type, or constructor overload resolution was ambiguous


I think it has something to do with the var type, i think im gonna need some kind of conversion, but I dont know for sure

THANKS AGAIN THANKS THANKS THANKS

Vanaj
July 28th, 2004, 03:35 AM
OK now try the function I put in my code. get_Image and getImage are not the same functions and do not return the same thing, as best as I can undersatnd the limited MSDN doc's on this subject.

CPPMe
July 28th, 2004, 03:39 AM
The same thing happened to me, just re-write it.

Vanaj
July 28th, 2004, 03:43 AM
What code is on line 243 where it is crashing ?

CPPMe
July 28th, 2004, 03:45 AM
here it is:

#pragma once

#include "atlimage.h"

#using <System.dll>
#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>


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


int startX;
int startY;
int dMode = 0;

public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
}

protected:
void Dispose(Boolean disposing)
{
if (disposing && components)
{
components->Dispose();
}
__super::Dispose(disposing);
}
private: System::Windows::Forms::PictureBox * pictureBox1;

private: System::Windows::Forms::Button * btnRect;
private: System::Windows::Forms::Button * btnLine;
private: System::Windows::Forms::Button * btnCircle;
private: System::Windows::Forms::Button * btnClose;
private: System::Windows::Forms::Button * btnSave;
private: System::Windows::Forms::SaveFileDialog * SaveFile;
private: System::Windows::Forms::ColorDialog * colorDialog1;
private: System::Windows::Forms::Button * btnColor;
private: Color cLor;




private: System::ComponentModel::IContainer * components;



private:
/// <summary>
/// Required designer variable.
/// </summary>


/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->pictureBox1 = new System::Windows::Forms::PictureBox();
this->btnClose = new System::Windows::Forms::Button();
this->btnRect = new System::Windows::Forms::Button();
this->btnLine = new System::Windows::Forms::Button();
this->btnCircle = new System::Windows::Forms::Button();
this->btnSave = new System::Windows::Forms::Button();
this->SaveFile = new System::Windows::Forms::SaveFileDialog();
this->colorDialog1 = new System::Windows::Forms::ColorDialog();
this->btnColor = new System::Windows::Forms::Button();
this->SuspendLayout();
//
// pictureBox1
//
this->pictureBox1->BackColor = System::Drawing::Color::White;
this->pictureBox1->BorderStyle = System::Windows::Forms::BorderStyle::FixedSingle;
this->pictureBox1->Location = System::Drawing::Point(16, 16);
this->pictureBox1->Name = S"pictureBox1";
this->pictureBox1->Size = System::Drawing::Size(264, 352);
this->pictureBox1->TabIndex = 0;
this->pictureBox1->TabStop = false;
this->pictureBox1->MouseUp += new System::Windows::Forms::MouseEventHandler(this, pictureBox1_MouseUp);
this->pictureBox1->MouseDown += new System::Windows::Forms::MouseEventHandler(this, pictureBox1_MouseDown);
//
// btnClose
//
this->btnClose->Location = System::Drawing::Point(288, 336);
this->btnClose->Name = S"btnClose";
this->btnClose->Size = System::Drawing::Size(136, 24);
this->btnClose->TabIndex = 1;
this->btnClose->Text = S"&Close";
this->btnClose->Click += new System::EventHandler(this, close_Click);
//
// btnRect
//
this->btnRect->Location = System::Drawing::Point(288, 16);
this->btnRect->Name = S"btnRect";
this->btnRect->Size = System::Drawing::Size(136, 24);
this->btnRect->TabIndex = 2;
this->btnRect->Text = S"&Rectangle";
this->btnRect->Click += new System::EventHandler(this, btnRect_Click);
//
// btnLine
//
this->btnLine->Location = System::Drawing::Point(288, 56);
this->btnLine->Name = S"btnLine";
this->btnLine->Size = System::Drawing::Size(136, 24);
this->btnLine->TabIndex = 3;
this->btnLine->Text = S"&Line";
this->btnLine->Click += new System::EventHandler(this, btnLine_Click);
//
// btnCircle
//
this->btnCircle->Location = System::Drawing::Point(288, 96);
this->btnCircle->Name = S"btnCircle";
this->btnCircle->Size = System::Drawing::Size(136, 24);
this->btnCircle->TabIndex = 4;
this->btnCircle->Text = S"&Circle";
this->btnCircle->Click += new System::EventHandler(this, btnCircle_Click);
//
// btnSave
//
this->btnSave->Location = System::Drawing::Point(288, 296);
this->btnSave->Name = S"btnSave";
this->btnSave->Size = System::Drawing::Size(136, 24);
this->btnSave->TabIndex = 5;
this->btnSave->Text = S"&Save";
this->btnSave->Click += new System::EventHandler(this, btnSave_Click);
//
// SaveFile
//
this->SaveFile->DefaultExt = S"bmp";
this->SaveFile->Filter = S"\"Image files|*.bmp|All files|*.*\"";
this->SaveFile->Title = S"Select Save Directory";
//
// btnColor
//
this->btnColor->Location = System::Drawing::Point(288, 136);
this->btnColor->Name = S"btnColor";
this->btnColor->Size = System::Drawing::Size(136, 24);
this->btnColor->TabIndex = 6;
this->btnColor->Text = S"Color";
this->btnColor->Click += new System::EventHandler(this, btnColor_Click);
//
// Form1
//
this->AutoScaleBaseSize = System::Drawing::Size(5, 13);
this->ClientSize = System::Drawing::Size(432, 382);
this->Controls->Add(this->btnColor);
this->Controls->Add(this->btnSave);
this->Controls->Add(this->btnCircle);
this->Controls->Add(this->btnLine);
this->Controls->Add(this->btnRect);
this->Controls->Add(this->btnClose);
this->Controls->Add(this->pictureBox1);
this->Name = S"Form1";
this->Text = S"Rectangles";
this->Load += new System::EventHandler(this, Form1_Load);
this->ResumeLayout(false);

}


private: System::Void pictureBox1_MouseDown(System::Object * sender, System::Windows::Forms::MouseEventArgs * e)
{
startX = e->X;
startY = e->Y;
}

private: System::Void pictureBox1_MouseUp(System::Object * sender, System::Windows::Forms::MouseEventArgs * e)
{
Graphics *ThisGraphics = Graphics::FromHwnd(pictureBox1->Handle);
if(dMode==1){
System::Drawing::Rectangle myRectangle =
System::Drawing::Rectangle(Point(startX, startY),
Drawing::Size(e->X-startX, e->Y-startY));
ThisGraphics->DrawRectangle(new Pen(cLor), myRectangle);
}
if(dMode==2){
ThisGraphics->DrawEllipse(new Pen(cLor),startX, startY, e->X-startX, e->Y-startY);

}
if(dMode==3){
ThisGraphics->DrawLine(new Pen(cLor), startX, startY, e->X, e->Y);
}
}



private: System::Void close_Click(System::Object * sender, System::EventArgs * e)
{
Close();
}

private: System::Void btnRect_Click(System::Object * sender, System::EventArgs * e)
{
dMode = 1;
}

private: System::Void btnCircle_Click(System::Object * sender, System::EventArgs * e)
{
dMode=2;
}

private: System::Void btnLine_Click(System::Object * sender, System::EventArgs * e)
{
dMode=3;
}



private: System::Void btnSave_Click(System::Object * sender, System::EventArgs * e)
{

SaveFile->Filter = S"Image files (*.bmp)|*.bmp|All files (*.*)|*.*";

if (SaveFile->ShowDialog() == DialogResult::OK)
{
CString fileName = SaveFile->FileName->ToString();
CImage myImage = pictureBox1->get_Image;
myImage.Save(fileName);
}
else{
Close();
}
}

private: System::Void Form1_Load(System::Object * sender, System::EventArgs * e)
{
cLor = Color::Green;
}

private: System::Void btnColor_Click(System::Object * sender, System::EventArgs * e)
{
if(colorDialog1->ShowDialog() == DialogResult::OK)
{
cLor = colorDialog1->Color;
}
}

};
}

CPPMe
July 28th, 2004, 03:49 AM
I figured out something, i had ...get_Image;, changed it to ..get_Image;.

Now i have a more clear error:

c:\Projects\Rectangle\Form1.h(233) : error C2440: 'initializing' : cannot convert from 'System::Drawing::Image __gc *' to 'ATL::CImage'
No constructor could take the source type, or constructor overload resolution was ambiguous


Line 233:


CImage myImage = pictureBox1->get_Image();

Vanaj
July 28th, 2004, 04:04 AM
Got a bad case of Teflon Brain...can't think anymore. I'll look somemore tomorrow.

CPPMe
July 28th, 2004, 04:06 AM
THANKS soooo sooo much,
Im gonna stay up tonight figuring out, ill post if i do.

Thanks again,
Tim

Yves M
July 28th, 2004, 07:38 AM
[ moved the thread to a more appropriate forum ]

CPPMe
July 28th, 2004, 01:43 PM
A.) How?
B.) What forum is the 'appropriate' forum?

Vanaj
July 28th, 2004, 01:55 PM
Move to the VC++ .NET forum, I asked Yves M to look at the post and see if it should be moved as there was little to no responses in the other forum. I thought there might be a better chance of getting a faster correct answer in the .NET forum than just the plain VC++ forum. I was just trying to get you some faster/better help. :blush:

CPPMe
July 28th, 2004, 02:14 PM
How do you move a thread?

Vanaj
July 28th, 2004, 02:35 PM
Can only be done by a Moderator, that is why I asked Yves M to take a look and have him make the call on the move.

CPPMe
July 28th, 2004, 02:37 PM
Thanx! =D
TYVM!