Decku treba screenshot kontrole, ne celog ekrana...
Primer koji je dat je ok, treba ga samo modifikovati da koristi HDC kontrole umesto HDC-a ekrana:
Code:
using System;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Drawing.Imaging;
using System.Windows.Forms;
class GDI32
{
[DllImport("GDI32.dll")]
public static extern bool BitBlt(System.IntPtr hdcDest,int nXDest,int nYDest,int nWidth,int nHeight,System.IntPtr hdcSrc,int nXSrc,int nYSrc,int dwRop);
[DllImport("GDI32.dll")]
public static extern System.IntPtr CreateCompatibleBitmap(System.IntPtr hdc,int nWidth, int nHeight);[DllImport("GDI32.dll")]
public static extern System.IntPtr CreateCompatibleDC(System.IntPtr hdc);
[DllImport("GDI32.dll")]
public static extern bool DeleteDC(System.IntPtr hdc);
[DllImport("GDI32.dll")]
public static extern bool DeleteObject(System.IntPtr hObject);
[DllImport("GDI32.dll")]
public static extern int SelectObject(System.IntPtr hdc, System.IntPtr hgdiobj);
}
class Export
{
public static void SaveControlImage(Control ctrl, string path)
{
Graphics graph = Graphics.FromHwnd(ctrl.Handle);
System.IntPtr ctrlHdc = graph.GetHdc();
System.IntPtr hdcDest = GDI32.CreateCompatibleDC(ctrlHdc);
System.IntPtr hBitmap = GDI32.CreateCompatibleBitmap(ctrlHdc, ctrl.Width, ctrl.Height);
GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0, 0, ctrl.Width, ctrl.Height, ctrlHdc, 0, 0, 0x00CC0020);
Bitmap bmp = new Bitmap(Image.FromHbitmap(hBitmap), ctrl.Width, ctrl.Height);
bmp.Save(path, System.Drawing.Imaging.ImageFormat.Bmp);
graph.ReleaseHdc(ctrlHdc);
GDI32.DeleteDC(hdcDest);
GDI32.DeleteObject(hBitmap);
}
}
pozivom Export.SaveControlImage prebacice se area koji kontrola zauzima u BMP fajl. Postoji samo jedan problem sa ovim, diaply HDC nije multi-layer, sto ce reci uradice snapshot onoga sto se trenutno nalazi na erkanu na tim pozicijama (ako je tvoja aplikacija pokrivena nekom drugom dobices sliku iz te druge aplikacije

).