ハンスが述べたように、クリックイベントとペイントイベントを混同しているという奇妙な声明があるようです。
このメソッドがクリック イベントに接続されている場合:
button3.Click += button3_Click;
button3_Click のメソッド引数は次のようになります
private void button3_Click(object sender, EventArgs e) { // code here // }
あなたが実際に何をしようとしているのかわからない。(つまり、画像ボックスに描画された画像の上にこの画像を描画しようとしていますか? 100、100 の x、y 座標は、画像ボックスまたは別のコントロール オブジェクトの位置に対する相対座標ですか?)
それでも、次のコードを試してみてください。現在の状況よりも良い軌道に乗ることができます。
上で説明したように、ペイント イベントではなくクリック イベントを使用していると仮定すると、コードは次のようになります。
これを行う 2 つの方法を紹介します。最初は「間違った方法」ですが、これは上記の方法に近いものです。
private void button3_Click(object sender, EventArgs e)
{
// If this is only an exercise this is acceptable, otherwise you should
// store the image as a resource
Image newImage = Image.FromFile(@"C:\Users\Administrator\Documents\logoGreyHybridText.png");
Graphics graphics = controlToDrawOver.CreateGraphics();
// Note rectangle x,y coordinates are relative to
// the 'controlToDrawOver' object.
Rectangle rectangleAreaToDrawImage = new Rectangle(100, 100, 450, 150);
graphics.DrawImage(newImage, rectangleAreaToDrawImage);
}
* 重要な注意事項 *
上記のコードに関係なく、コードが示すように、コントロールのクリック イベント ハンドラーではなく、コントロールのペイント イベント ハンドラーで描画する必要があります。コントロールが再描画されるたびに、変更は失われます。
したがって、「より良い方法」は次のとおりです。
ボタンがクリックされるのを待つ必要がある場合は、次のようにすることができます。
private void button3_Click(object sender, EventArgs e)
{
controlToDrawOver.Paint -= ControlPaintEventHandler;
controlToDrawOver.Paint += ControlPaintEventHandler;
}
private void ControlPaintEventHandler(object sender, PaintEventArgs e)
{
// If this is only an exercise this is acceptable, otherwise you should
// store the image as a resource
Image newImage = Image.FromFile(@"C:\Users\Administrator\Documents\logoGreyHybridText.png");
// Note rectangle x,y coordinates are relative to
// the 'controlToDrawOver' object.
Rectangle rectangleAreaToDrawImage = new Rectangle(100, 100, 450, 150);
e.Graphics.DrawImage(newImage, rectangleAreaToDrawImage);
}