Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert a graphics in an other graphics

Status
Not open for further replies.

troz

Programmer
Jul 26, 2002
5
FR
Hello,

How can I insert the image of an object Graphics in an other ?
By example, I have two panels : I draw some lines on the first one. How can I copy it to the second panel ? (or only a part of it) ?

Thanks


Troz
 
there is the solution, found on

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);

private void button1_Click(object sender, System.EventArgs e)
{
Graphics g1 = panel1.CreateGraphics();
Graphics g2 = panel2.CreateGraphics();

//Drawing on panel1 (g1)
//...

//Copy it to panel2
Size s = panel1.Size;
Image memImage = new Bitmap(s.Width, s.Height, g1);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = g1.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
BitBlt(dc2, 0, 0, panel1.ClientRectangle.Width,
panel1.ClientRectangle.Height, dc1, 0, 0, 13369376);
g1.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
g2.DrawImage(memImage,0,0);
}

It works with any Graphics object (you can copy a form, a MSChart ...)

Troz
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top