capturing control as image

  • Thread starter Thread starter Chris
  • Start date Start date
C

Chris

Hi,
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?

Chris
 
Chris,

The graphics object doesn't contain any picture. It is only bunch of drawing
methos and properties. The important thing is where those drawing methods
draw. In theory controls don't have to worry where the result of the drawng
they perform goes.

If you want to capture the image you should create a graphics object out of
image (Graphics.FromImage method). Then you have to force the controls to
paint themselves using that graphics object. And here is the unfotunate
part. The control doesn't have any public method where you can provide a
graphics object and the control will paint itself. Windows OS has messages
WM_PRINT and WM_PRINTCLIENT that could be used for that purpose. The only
thing is that the windows'controls are not obligated to implement those
messages. The standard windows controls behave correctly, though.

Anyways I tried once to use them with .NET and I couldn't have them worked.
Frankly I didn't try harder.
 
Chris said:
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
 
thanks for your imput guys. I think this is what I'm looking for. I'll
have to try it later, but I think it will work. Thanks again,

Chris


psg said:
Chris said:
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
 
Back
Top