トップ回答者
SaveFileDialogのオブジェクト参照について

質問
-
reira_leila01と申します。
SaveFileDialogに関して質問があります。C++/CLIでアプリケーション製作の練習を行っておりますが、
名前を付けて保存というボタンをクリックすることで
保存ダイアログを開くプログラムを作ったのですが、
いざダイアログに名前を入力して保存ボタンを押すと”'System.NullReferenceException' のハンドルされていない例外が System.Drawing.dll で発生しました。”
"追加情報: オブジェクト参照がオブジェクト インスタンスに設定されていません。"
とエラーを出してしまいます。
どこをどう直せばよいのか教えていただけないでしょうか?
何か記述を付け加えないといけない箇所があるのでしょうか?宜しくお願いします。
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
// Displays a SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog ^ saveFileDialog1 = gcnew SaveFileDialog();
saveFileDialog1->Filter =
"JPeg Image|*.jpg|Bitmap Image|*.bmp|Gif Image|*.gif";
saveFileDialog1->Title = "Save an Image File";
saveFileDialog1->ShowDialog();
// If the file name is not an empty string, open it for saving.
if(saveFileDialog1->FileName != "")
{
// Saves the Image through a FileStream created by
// the OpenFile method.
System::IO::FileStream^ fs =
safe_cast<System::IO::FileStream^>(saveFileDialog1->OpenFile());
// Saves the Image in the appropriate ImageFormat based on
// the file type selected in the dialog box.
// Note that the FilterIndex property is one based.
switch(saveFileDialog1->FilterIndex)
{
case 1 :
this->pictureBox1->Image->Save(fs,
System::Drawing::Imaging::ImageFormat::Jpeg);
break;
case 2 :
this->pictureBox1->Image->Save(fs,
System::Drawing::Imaging::ImageFormat::Bmp);
break;
case 3 :
this->pictureBox1->Image->Save(fs,
System::Drawing::Imaging::ImageFormat::Gif);
break;
}
fs->Close();
}
}
private: System::Void pictureBox1_Click(System::Object^sender, System::EventArgs^ e) {
Graphics^ g =pictureBox1->CreateGraphics();
// Create pen.
Pen^ blackPen = gcnew Pen( Color::Red,16.0f );// Create points that define line.
Point point1 = Point(0,0);
Point point2 = Point(200,200);// Draw line to screen.
g->DrawLine( blackPen, point1, point2 );Point point3=Point(400,20);
g->DrawLine(blackPen,point2,point3);
Point point4=Point(420,300);
g->DrawLine(blackPen,point3,point4);
}
}
回答
-
保存時に pictureBox1->Image が null でした。
画像を書き込む際に pictureBox1 自体にではなく、 pictureBox1->Imageに書き込めばよいと思います。
下記サンプルコードです。private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) { Bitmap^ bmpPicBox = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height); pictureBox1->Image = bmpPicBox; Graphics^ g = Graphics::FromImage(pictureBox1->Image); // Create pen. Pen^ blackPen = gcnew Pen( Color::Red,16.0f ); // Create points that define line. Point point1 = Point(0,0); Point point2 = Point(200,200); // Draw line to screen. g->DrawLine( blackPen, point1, point2 ); Point point3=Point(400,20); g->DrawLine(blackPen,point2,point3); Point point4=Point(420,300); g->DrawLine(blackPen,point3,point4); }
参考サイト1: http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=30771&forum=7
参考サイト2: http://imagingsolution.blog107.fc2.com/blog-entry-108.html- 編集済み kenjinoteMVP 2016年4月13日 9:39
- 回答としてマーク reira_leila01 2016年4月13日 10:47
-
kenjinote様。
うまくいきました。
ほんとうにありがとうございます。
- 回答としてマーク reira_leila01 2016年4月13日 10:47
すべての返信
-
保存時に pictureBox1->Image が null でした。
画像を書き込む際に pictureBox1 自体にではなく、 pictureBox1->Imageに書き込めばよいと思います。
下記サンプルコードです。private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) { Bitmap^ bmpPicBox = gcnew Bitmap(pictureBox1->Width, pictureBox1->Height); pictureBox1->Image = bmpPicBox; Graphics^ g = Graphics::FromImage(pictureBox1->Image); // Create pen. Pen^ blackPen = gcnew Pen( Color::Red,16.0f ); // Create points that define line. Point point1 = Point(0,0); Point point2 = Point(200,200); // Draw line to screen. g->DrawLine( blackPen, point1, point2 ); Point point3=Point(400,20); g->DrawLine(blackPen,point2,point3); Point point4=Point(420,300); g->DrawLine(blackPen,point3,point4); }
参考サイト1: http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=30771&forum=7
参考サイト2: http://imagingsolution.blog107.fc2.com/blog-entry-108.html- 編集済み kenjinoteMVP 2016年4月13日 9:39
- 回答としてマーク reira_leila01 2016年4月13日 10:47
-
エラーはどこで出ているのでしょうか?
'System.NullReferenceException' は、オブジェクト(^ が付くもの全部が対象)が nullptr 状態になっているのにアクセスしている場合に出ます。
このコードだと、pictureBox1 か、pictureBox1->Image あたりが nullptr になっているのでは?という気もしますが、エラーの状況がわからないので正しいかどうかわかりません。
あと、直接エラーとは関係ありませんが、saveFileDialog1->ShowDialog() の戻り値で、保存するかどうかをチェックするほうがいいと思いますよ。
そのために ShowDialog には戻り値がありますので。
どういう風に判定すればいいかは、リファレンス(VS上でF1キーを押せば出てきます)をみればわかります。
とっちゃん@わんくま同盟, Visual Studio and Development Technologies http://blogs.wankuma.com/tocchann/default.aspx
-
kenjinote様。
うまくいきました。
ほんとうにありがとうございます。
- 回答としてマーク reira_leila01 2016年4月13日 10:47