"Chris" <ct******@hotmail.com> wrote in message
news:Og**************@TK2MSFTNGP09.phx.gbl...
I need to get an image of a control (ListView) so i can save it to a file.
It's pretty easy to get the Graphics with the CreateGraphics() function on
every control, but I don't know how I might save the Graphics object as an
image. Has anyone done this?
A while ago I used Win32 API GDI32.dll. It goes like this:
*****************************************
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
[DllImport("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 const Int32 SRCCOPY = 0xCC0020;
private Bitmap memImage;
private void button1_Click(object sender, System.EventArgs e)
{
Graphics graphic = this.listView1.CreateGraphics();
Size s = this.listView1.Size;
memImage = new Bitmap(s.Width, s.Height, graphic);
Graphics memGraphic = Graphics.FromImage(memImage);
IntPtr dc1 = graphic.GetHdc();
IntPtr dc2 = memGraphic.GetHdc();
BitBlt(dc2, 0, 0, this.listView1.ClientRectangle.Width,
this.listView1.ClientRectangle.Height,dc1, 0, 0, SRCCOPY);
graphic.ReleaseHdc(dc1);
memGraphic.ReleaseHdc(dc2);
memImage.Save("ListView.bmp",ImageFormat.Bmp);
}
*****************************************
You must be careful because if your control will be hidden (e.g. moved away
from the screen), or will be covered with some other form/control, then as a
result you will get black image or screen capture of that on top control.
RGDS PSG