• 締切済み

pictureboxの画像を削除するには?

Visual C# 2008を使用しています。 ボタンを押すとpictureboxに画像が表示されるプログラムを作成しました。 今度は、その表示された画像をクリックし、別のボタンを押すことでその画像が削除できるようにしたいと思っているのですが、うまくプログラムできません。 誰か教えていただけないでしょうか? ソースコードは以下のようになっています。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace algorithm { public partial class Form1 : Form { int space = 10; public Form1() { InitializeComponent(); } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { } private void kihonsyori_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\基本処理箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 50; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void groupBox1_Enter(object sender, EventArgs e) { } private void pictureBox1_Click(object sender, EventArgs e) { } private void hanpuku_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\反復箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 80; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void sentaku_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) { Bitmap img = new Bitmap(@"C:\Documents and Settings\admin\My Documents\Visual Studio 2008\Projects\algorithm\algorithm\picture\選択箱.png"); Graphics g = pictureBox1.CreateGraphics(); g.DrawImage(img, 0, space, 0.4F * img.Width, 0.3F * img.Height); space += 80; img.Dispose(); g.Dispose(); } else { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; } } private void sakujyo_Click(object sender, EventArgs e) { pictureBox1.Image = null; space = 0; } } }

みんなの回答

回答No.2

 こんばんは。御礼頂きました。  サムネイルの様な処理をしたいのでしょうか。  不可能ではないのですが、プログラム内でビットマップの配列と、それぞれの位置を保持しておき、ピクチャーボックスのクリックイベントで、マウスの辺り判定を計測するなど、モグラ叩きの様なプログラムをする必要があります。  どちらにしろ、ピクチャーボックスは複数の絵を出すのに向いていないのは確かです。  リストビューでは駄目なのでしょうか。   http://oshiete1.goo.ne.jp/qa5400139.html  上記URLの様にビットマップを並べて表示出来ますし、一貫した操作方法で追加、削除が行えます。

回答No.1

 こんにちは。  別口で実験台のC#のプロジェクトを作成しても良いのならば、以下で試せませんか。  pictureBox1, button1(読み込み), button2(削除)が必要です。参考程度で。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //スタートアップ private void Form1_Load(object sender, EventArgs e) { this.pictureBox1.Image = new Bitmap("test.bmp"); this.pictureBox1.Invalidate(); this.button2.Enabled = false; } //読み込みボタン private void button1_Click(object sender, EventArgs e) { if (this.pictureBox1.Image != null) this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = new Bitmap("test.bmp"); this.pictureBox1.Invalidate(); } //削除ボタン private void button2_Click(object sender, EventArgs e) { this.pictureBox1.Image.Dispose(); this.pictureBox1.Image = null; this.pictureBox1.Invalidate(); this.button2.Enabled = false; } //ピクチャーボックスをクリックする private void pictureBox1_Click(object sender, EventArgs e) { if (this.pictureBox1.Image == null) return; this.button2.Enabled ^= true; } } }

kiyo061
質問者

お礼

回答ありがとうございます。 書いていただいたプログラムを参考にして、プログラムを組みなおしてみました。 しかし、picturebox自体をクリックしてもボタンが押せるようになってしまいます。picturebox内で画像のない場所をクリックしてもボタンが押せてしまうという状況です。 pictureboxに複数の画像を表示させ、一つ一つの画像をクリックできるようにしたいと思っています。 うまく参考にできず申し訳ないのですが、教えていただけないでしょうか?

関連するQ&A

  • C# 簡単なシューティング 自機移動について

    かなりの初心者で困っています。 簡単なシューティングを作ろうとおもっています。 使用ソフトはVisualC#2005です。 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //自機(右移動) private void button3_Click(object sender, EventArgs e) { if (timer1.Enabled == false) { timer2.Enabled = false; timer1.Enabled = true; } else { timer1.Enabled = false; timer3.Enabled = false; timer4.Enabled = false; } } private void timer1_Tick(object sender, EventArgs e) { pictureBox1.Left = pictureBox1.Left + 2; } //自機(左) private void button4_Click(object sender, EventArgs e) { if (timer2.Enabled == false) { timer1.Enabled = false; timer2.Enabled = true; } else { timer2.Enabled = false; timer3.Enabled = false; timer4.Enabled = false; } } private void timer2_Tick(object sender, EventArgs e) { pictureBox1.Left = pictureBox1.Left - 2; } //自機(上) private void button1_Click(object sender, EventArgs e) { if (timer3.Enabled == false) { timer4.Enabled = false; timer3.Enabled = true; } else { timer1.Enabled = false; timer2.Enabled = false; timer3.Enabled = false; } } private void timer3_Tick(object sender, EventArgs e) { pictureBox1.Top = pictureBox1.Top - 2; } //自機(下) private void button2_Click(object sender, EventArgs e) { if (timer4.Enabled == false) { timer3.Enabled = false; timer4.Enabled = true; } else { timer1.Enabled = false; timer2.Enabled = false; timer4.Enabled = false; } } private void timer4_Tick(object sender, EventArgs e) { pictureBox1.Top = pictureBox1.Top + 2; } } } 自機は画像(PictureBox)でボタンによって上下左右に移動します。 FormのSizeは800,630です。 自機の移動がボタンなのでキー入力によって操作できるようにしたいのですが、 それと、自機の移動範囲を画面からでないようにしたいです。 あまりC#を理解できてない上でつくったのでおかしな点が多々あると思います。 教えていただける方がいると助かります。

  • フォーム間のデータ受け渡し

    現在VIsual Studio 2005のフォームアプリケーションを使ってプログラミングしています。ボタンを押すことで新たな子フォームを作成し、親フォームから子フォームへグローバル関数で宣言しているbmp[],picture[],red[]などのデータを渡したいのですがどうすればいいのかわからず困っております。子フォームから親フォームへテキストボックスなどの値を渡す方法などはわかったのですが、それをどう応用していいのかもわからない状況です。最終的には親フォームのbmp[0]におけるred[0]が1(画像処理されている)なら子フォームでbmp[0]を表示させたいと思っています。わかる方がいましたらどうかご教授ください。よろしくお願いします。以下がプログラムとなっております。 *** 親フォーム *** #pragma once #include "pic2.h" namespace pic { 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::IO; // 省略 // public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // bmp = nullptr; Array::Resize( bmp, 20 ); Array::Resize( picture, 20 ); Array::Resize( bmpr, 20 ); this->red = gcnew array<int>(20); } // 省略 // private: System::Windows::Forms::PictureBox^ pictureBox1; private:array< Bitmap^>^ bmp; // 原画像格納 // private:array< Bitmap^>^ bmpr; // 処理画像格納 // private:array< PictureBox^>^ picture; private:array< int>^ red; // 処理:1 不処理:0 // // 省略 // #pragma endregion private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { bmp[0] = gcnew Bitmap("ファイル名",true); } private: System::Void button1_Click_1(System::Object^ sender, System::EventArgs^ e) { pic2 ^p2 = gcnew pic2(); p2->ShowDialog();     /* ボタンを押すことで新たなフォーム作成 */ } private: System::Void pictureBox1_Click(System::Object^ sender, System::EventArgs^ e) { int x,y; int w = bmpr[0]->Width; int h = bmpr[0]->Height; if(red[0] == 1){ bmp[0] = gcnew Bitmap("ファイル名",true); pictureBox1->Image = bmp[0]; red[0] = 0; return; } if(red[0] == 0){ // 画像処理 // pictureBox1->Image = bmpr[0]; red[0] = 1; } } }; *** 子フォーム *** #pragma once //#include "Form1.h" #include "pic3.h" using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; namespace pic { /// <summary> /// pic2 の概要 /// public ref class pic2 : public System::Windows::Forms::Form { public: pic2(void) { InitializeComponent(); // //TODO: ここにコンストラクタ コードを追加します // } // 省略 // } #pragma endregion private: System::Void pic2_Load(System::Object^ sender, System::EventArgs^ e) { if(親フォームのred[0]==1ならば){ pictureBox1->Image = bmp[0] } } private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {   // 新たな子フォームpic3作成 // } private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) { this->Close(); } }; }

  • pictureBoxに表示する方法

    始めまして、プログラム作成初心者です。 今、pictureBox1に選択した画像を表示し、ボタンを押すと pictureBox2にpictureBox1の画像を白黒にして表示するプログラムを作成しています。 http://www.bobpowell.net/grayscale.htm のprivate void Form1_Load(object sender, System.EventArgs e) を参考に作ってみようと思っているのですが、 これをpictureBox2に表示するようなプログラムが良くわかりません。 分かられる方、よろしくお願いいたします。

  • pictureboxの表示について

    いつもお世話になります。 C#について質問です。VisualC#2008を使っています。 private PictureBox[] pictArray = new PictureBox[100]; private void Box_Load() { PictureBox pict; int nIdx = 0; while (nIdx < pictArray.Length) { pict = new PictureBox(); pictArray[nIdx] = pict; pict.Click += new EventHandler(pict_Click); nIdx++; } } のように、あるメソッドで配列にpictureboxの画像を代入(?)していく処理をしています。このメソッドを別のメソッドで呼び出し、 pictArray[1]に格納されているpicturebox pictArray[2]に格納されているpicturebox  …といった具合に、次々と表示させていきたいと考えています。しかし1つしかpictureboxが表示されません。具体的には以下のようなプログラムを組んでいるところです。 private void pict_Click(object sender, EventArgs e) { Box_Load(); kihon_pict.Location = new Point(10, yy); this.panel1.Controls.Add(pict); yy += 40; } 誰か解決策を教えていただけないでしょうか? よろしくお願いします。

  • C# pictureBox(a)

    Visual C# 2008にて int a = 3; pictureBox(a).Image = Properties.Resources.Icon1_2; みたいな代入の方法がどうしても解りません Properties.Resources.Icon1_2は リソースのIcon1_2.bmpです 過去のQ&A QNo6332332を読んでも解りません。 pictureBoxを貼り付けてダブルクリック Tagを1.などに設定 private void pictureBox1_Click(object sender, EventArgs e) { PictureBox pict = sender as PictureBox; MessageBox.Show(pict.Name); MessageBox.Show(pict.Tag.ToString()); int picb = int.Parse(pict.Tag.ToString()); pictureBox(picb).Image = Properties.Resources.Icon1_2; } 上のようにしたい アドバイスをお願いできませんでしょうか

  • C# panelのなかのpctureBox

    panel1のなかのpictureBox1 QNo.8077421の続き http://okwave.jp/qa8077421.html Form1にPicturBox1をはりつけてダブルクリックでイベントハンドラを作る Properties.Resources.IconH;は、*.bmp private void pictureBox1_Click(object sender, EventArgs e) { pictureBox1.Image = Properties.Resources.IconH; } これは、OK private void pictureBox1_Click(object sender, EventArgs e) { int a = 1; Control c = this.Controls["PictureBox"+a.ToString()]; ((PictureBox)c).Image = Properties.Resources.IconH; } これも、OK ここからがうまくいきません。 panel1を貼り付けPicturBox1をpanel1のなかに。 pictureBox1.Image = Properties.Resources.IconH; これは、OK ここからです。 int a = 1; Control c = this.Controls["PictureBox"+a.ToString()]; ((PictureBox)c).Image = Properties.Resources.IconH; これは、 NG NullReferenceException はハンドルされませんでした。 オブジェクト参照がオブジェクト インスタンスに設定されていません。 ((PictureBox)c).Image = Properties.Resources.IconH;これ これをつかいたい。 アドバイスをお願いできませんでしょうか。

  • c# イベントハンドラ 統一

    trackBarを複数配置しているのですが、そのtrackBarごとに private void trackBar1_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value,'赤'); } private void trackBar2_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value, '青'); } private void trackBar3_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value, '緑'); } private void trackBar4_Scroll(object sender, EventArgs e) { previewcolor(this.trackBar1.Value, '透'); } こんな風に別々に記述する形になって非常にスッキリしないです。 これをジェネリクス?やデリゲート?などを使ってすっきりできないのでしょうか? visual stdio c# 2008を使ってます。 宜しくお願いします。

  • VC++フォームアプリケーション でのビットマップ処理

    私は今Visual Studio 2005(C++)のフォームアプリケーションでプログラムを組んでいるのですが、ビットマップ処理ができません。以下のプログラムにどのような変更を加えればできるようになるのでしょうか? ご存知の方がいたら教えてください。 #pragma once ~フォームのデザイン部分のプログラム(自動)なので省略いたします~ #pragma endregion private: Bitmap^ bmp; private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) { } private: System::Void 開くOToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { OpenFileDialog^ myDLG = gcnew OpenFileDialog(); myDLG->AddExtension = true; myDLG->DefaultExt = "bmp"; myDLG->FileName = "*.bmp"; myDLG->ShowReadOnly = false; myDLG->Filter = "ビットマップ(*.bmp) | *.bmp | jpeg(*.jpg;*.jpeg) | *.jpg;*.jpeg"; if(myDLG->ShowDialog() == System::Windows::Forms::DialogResult::OK){ this->Text = myDLG->FileName; pictureBox1->Image = gcnew Bitmap(myDLG->FileName); Invalidate(); } } private: System::Void 保存SToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { SaveFileDialog^ myDLG = gcnew SaveFileDialog(); myDLG->AddExtension = true; myDLG->DefaultExt = "bmp"; myDLG->FileName = "*.bmp"; myDLG->Filter = "ビットマップ(*.bmp) | *.bmp | jpeg(*.jpg;*.jpeg) | *.jpg;*.jpeg | すべてのファイル(*.*) | *.*"; if(myDLG->ShowDialog() != System::Windows::Forms::DialogResult::OK){ return; } pictureBox1->Image->Save(myDLG->FileName); Invalidate(); } private: System::Void 閉じるCToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { if(MessageBox::Show("アプリケーションを終了しますか?","画像描画",MessageBoxButtons::OKCancel,MessageBoxIcon::Question) == System::Windows::Forms::DialogResult::OK){ this->Close(); } } private: System::Void 編集ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { } private: System::Void 回転180ToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) { } } よろしくお願いします。

  • VC# 追加フォーム生成時、フォームに描画できない

    前略 ・VC#(.NET 2008)のプログラムでおしえてください。 ・メインのWindowsフォームからボタンクリックで 追加したサブWindowsフォーム上のピクチャーボックス上に何も操作なしでに描画したいのですができません。CreateGraphics()でオブジェクトを生成して描画しています。どのようにしたらよいのかおしえてください。フォームのイベントとして、Paint,Load,shown,Activated等いろいろやってもだめでした。  尚、サブフォームにボタンをもうけ このメソッドの中に描画コマンドを書き、ボタンを操作すると描画できます。下記は円を描こうとしていますが、shapeコンポーネントでは描けな複雑な描画をしたいと思っています。 ・以下に 下記の動作となるソースコードを記載します。 (1)起動するとメインフォームForm1のpictureBox1に 赤い円が描かれる (2)ボタンbutton1をクリックするとForm2が表示される。  Form2上のラベルlabel1とlabel2の文字色は青色に変わっています。  しかし、円は何故か描かれていません (3)Form2上のボタンをクリックすると赤い円がForm2上に描かれます。  どこをどのように直せば追加のサブフォームForm2が表示された時に Form2上のpictureBox1に円が描がかれているのでしょうか。以上、よろしくお願いします //----------------------------------------------------------- //メインフォーム using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace temp2 { public partial class Form1 : Form { public Form1(){ InitializeComponent(); } private void button1_Click(object sender, EventArgs e){ Form2 form2 = new Form2(); form2.ShowDialog(); } private void Form1_Paint(object sender, PaintEventArgs e){ Graphics g = pictureBox1.CreateGraphics(); g.DrawEllipse(Pens.Red, 0, 0, 200, 200); } } } //サブフォーム using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace temp2 { public partial class Form2 : Form { public Form2(){ InitializeComponent(); } private void pictureBox1_Paint(object sender, PaintEventArgs e){ label1.ForeColor = Color.Blue; Graphics g1 = pictureBox1.CreateGraphics(); g1.DrawEllipse(Pens.Blue, 0, 0, 50, 50); label2.ForeColor = Color.Blue; } private void button1_Click(object sender, EventArgs e){ Graphics g2 = pictureBox1.CreateGraphics(); g2.DrawEllipse(Pens.Blue, 0, 0, 50, 50); } private void Form2_Paint(object sender, PaintEventArgs e){ } private void Form2_Load(object sender, EventArgs e){ } private void Form2_Shown(object sender, EventArgs e){ } } } 以上

  • C#のpictureBoxの描画のタイミング

    C#のpictureBoxの描画について質問します。 drawにpictureBoxの描画コードが有りますがあり、Form1() の InitializeComponent();の 後(コードA)では、描画できませんが、button1_Click(コードB)では、描画できるようです。 Q1)プログラムの起動時に、描画できる方法はありますか? public Form1() { InitializeComponent(); 処理コード draw(); //コードA } private void button1_Click(object sender,EventArgs e) { draw(); //コードB }

専門家に質問してみよう