473,387 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Form Capture

Can anyone help me to capture a Form into a Bitmap in C#?

Thank you in advance
Nov 16 '05 #1
1 5962
Hi there...

I think this might help you... First of all we need to use PInvoke...
using System.Runtime.InteropServices;

Second we need to have some API functions' prototypes (GDI+) to call from
within our program

class APICalls {
[DllImport("Gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hdc);

[DllImport("Gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int
width, int height);

[DllImport("Gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr
hgdiobj);

[DllImport("Gdi32.dll")]
public static extern int BitBlt(IntPtr hdcdest, int nxdest, int
nydest, int nwidth, int nheight, IntPtr
hdcsrc, int nxsrc, int
nysrc, int raster);
}

and finally some code to "capture" our form (this could be placed in
button's click...)

Graphics graph = Graphics.FromHwnd(Handle); // Get a graphics object
from form's Hwnd
Region rgn = new Region(m_rectangle); // Get a Region. m_rectangle is a
Rectangle struct (client area)
graph.SetClip(rgn, CombineMode.Exclude); // Clip that region
IntPtr hdc = graph.GetHdc(); // Get a HDC (Handle to a Device Context)
from our graph object
IntPtr dchandle = APICalls.CreateCompatibleDC(hdc); // Create a
compatible DC
// Create a compatible bitmap from our HDC
IntPtr bmphandle = APICalls.CreateCompatibleBitmap(hdc,
m_rectangle.Width, m_rectangle.Height);
IntPtr select = APICalls.SelectObject(dchandle, bmphandle); // Select
that object (bitmap)
// Perform a BitBlt (Bit-Block Transfer) and get our snapshot from our
form
int success = APICalls.BitBlt(dchandle, m_rectangle.X, m_rectangle.Y,
m_rectangle.Width,
m_rectangle.Height, hdc,
0, 0, 13369376); // SRCCOPY
Image imagen = Bitmap.FromHbitmap(bmphandle); // Save our bitmap into an
Image object

// Dispose resources here...

try {
rgn.Dispose();
graph.Dispose();
} catch {}

and that's it... Hope this may help you...

Regards,
--
Angel J. Hernández M.
MCSD

"Viktor" <vi****@ucla.edu> escribió en el mensaje
news:3e****************************@phx.gbl...
Can anyone help me to capture a Form into a Bitmap in C#?

Thank you in advance

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: NeoAsimov | last post by:
Hello, There is what I want to do. I worked on the problem since 6 hours but I had a problem (probably stupid) and I dont know how to resolve it. I need to have an Mouse Event Click when...
3
by: Jason | last post by:
I'm trying to implement some pretty basic behavior in c#/.net 1.1 Forms but can't seem to find all the requisite pieces. When a user clicks on the background of the form, I want the app to start...
1
by: ray well | last post by:
hi, i need to preview the keys in my app in order to process F1-F10. i set keypreview of my form to true, and it does capture the keystrokes from all over the forms controls which i then...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
18
by: Colin McGuire | last post by:
Hi - this was posted last weekend and unfortunately not resolved. The solutions that were posted almost worked but after another 5 days of working on the code everynight, I am not further ahead....
0
by: Henry C. Wu | last post by:
Hi, I have a form that has a video "inserted" at the form's Handle. Like so: '//Create Capture Window capGetDriverDescriptionA(0, lpszName, 100, lpszVer, 100) '// Retrieves driver info lwndC =...
4
by: jxiang | last post by:
I created a child form that is much bigger than the MDI form in VB.Net. I am trying to capture the whole child form and save as an image or sent to printer. I tried to use BitBlt to capture the...
2
by: Elmo Watson | last post by:
Control1 is on Form1 on Form two - I'd like to access the instanciation of that control (Control1), using different properties & methods Is there a way, in VB.Net, to refer to a control on...
15
by: Jack | last post by:
Hi, I have a asp form where one element is a list box which lists four years starting from 2004. This list is drawn from a database table which has YearID and Year as two fields as shown below:...
1
by: BlackFireNova | last post by:
I'm using Access 2003, on Windows XP Pro. I have a subform which is launched from a command button on my main form. When this form opens, I have 2 fields which I have inserted VBA Code into to...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.