外部プログラムをどの程度制御できるか、または画像ボックスにどのように描画するかはわかりませんが、createGraphics を使用している場合は機能しません。
private void button1_Click(object sender, EventArgs e)
{
//here I am calling the graphics object of the Picture Box, this will draw to the picture box
//But the DrawToBitmap, will not reflect this change, and once the Picturebox needs to be updated, this will disappear.
Graphics g = pictureBox1.CreateGraphics();
g.DrawRectangle(Pens.Blue, 10, 10, 20, 20);
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(pictureBox1.Width, pictureBox1.Height);
Rectangle bounds = new Rectangle(Left, Top, Width, Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
//Draws whatever is in the PictureBox to the Forms BackgroundImage
this.BackgroundImage = bmp;
//It will not draw the Blue rectangle
}
外部プログラムがビットマップに描画する場合、そのビットマップをピクチャボックスの背景に設定できます
Bitmap buffer;
public Form1()
{
InitializeComponent();
buffer = new Bitmap(pictureBox1.Width, pictureBox1.Height);
}
private void button1_Click(object sender, EventArgs e)
{
//draw to the bitmap named buffer
using (Graphics g = Graphics.FromImage(buffer))
{
g.DrawRectangle(Pens.Blue, 10, 10, 20, 20);
}
//assign the picturebox image to buffer
pictureBox1.Image = buffer;
//Now this will show the blue rectangle
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(pictureBox1.Width, pictureBox1.Height);
Rectangle bounds = new Rectangle(Left, Top, Width, Height);
pictureBox1.DrawToBitmap(bmp, pictureBox1.ClientRectangle);
this.BackgroundImage = bmp;
}
編集 三回目のチャーム権
これはスクリーン ショットを撮り、ピクチャ ボックスを切り取ってから、フォームの背景を変更して、それが機能したことを証明します。
追加する必要があります
using System.Drawing.Imaging;
ピクセル形式用です。
private void button1_Click(object sender, EventArgs e)
{
using (Graphics G = pictureBox1.CreateGraphics())
{
G.DrawRectangle(Pens.Blue, 10, 10, 10, 10);
}
Bitmap BMP = new Bitmap(Screen.PrimaryScreen.Bounds.Width,
Screen.PrimaryScreen.Bounds.Height,
PixelFormat.Format32bppArgb);
using (Graphics GFX = Graphics.FromImage(BMP))
{
GFX.CopyFromScreen(Screen.PrimaryScreen.Bounds.X,
Screen.PrimaryScreen.Bounds.Y,
0, 0,
Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
Bitmap YourPictureBoxImage = new Bitmap(pictureBox1.Width,pictureBox1.Height);
using (Graphics g = Graphics.FromImage(YourPictureBoxImage))
{
Point np = pictureBox1.PointToScreen(new Point(0, 0));
g.DrawImage(BMP,new Rectangle(0,0,100,100),new Rectangle(np,pictureBox1.Size),GraphicsUnit.Pixel);
}
this.BackgroundImage = YourPictureBoxImage;
}