Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm General / December 2004

Tip: Looking for answers? Try searching our database.

Not able to take screen shots of "S to make presentation

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rajat Tandon - 03 Dec 2004 17:32 GMT
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 facing. Please please help me to solve this as
soon as possible.

So here we go ...

I am not able to take the screen shot of the windows form based "Smart
Client" application at run time.This is urgently required for making the
presentation .

Till now I have tried 4 methods...
--------------------------------
1) Using IrfanView software
2) Using Snag-it
3) Using Camtasia_Capture&menumaker
4) Using the program which is given below...

using System;
using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Reflection;

using Microsoft.Win32 ;

using System.Runtime.InteropServices;

namespace WindowsApplication1

{

/// <summary>

/// Summary description for Form1.

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.Windows.Forms.Timer timer1;

private System.ComponentModel.IContainer components;

private System.Windows.Forms.Button button1;

int i;

public Form1()

{

//

// Required for Windows Form Designer support

//

InitializeComponent();

i=0;

//

// TODO: Add any constructor code after InitializeComponent call

//

}

/// <summary>

/// Clean up any resources being used.

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

components.Dispose();

}

}

base.Dispose( disposing );

}

[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 Capture_Screen(string filename)

{

Graphics g1 = this.CreateGraphics();

Image MyImage = new Bitmap(this.ClientRectangle.Width,

this.ClientRectangle.Height, g1);

MyImage = new Bitmap(this.ClientRectangle.Width,

this.ClientRectangle.Height, g1);

Graphics g2 = Graphics.FromImage(MyImage);

IntPtr dc1 = g1.GetHdc();

IntPtr dc2 = g2.GetHdc();

BitBlt(dc2, 0, 0, this.ClientRectangle.Width,

this.ClientRectangle.Height, dc1, 0, 0, 13369376);

g1.ReleaseHdc(dc1);

g2.ReleaseHdc(dc2);

MyImage.Save(@"d:\rajat\screen_captures\" + filename + ".jpg"
,System.Drawing.Imaging.ImageFormat.Jpeg);

}

#region Windows Form Designer generated code

/// <summary>

/// Required method for Designer support - do not modify

/// the contents of this method with the code editor.

/// </summary>

private void InitializeComponent()

{

this.components = new System.ComponentModel.Container();

this.timer1 = new System.Windows.Forms.Timer(this.components);

this.button1 = new System.Windows.Forms.Button();

this.SuspendLayout();

//

// timer1

//

this.timer1.Enabled = true;

this.timer1.Interval = 1000;

this.timer1.Tick += new System.EventHandler(this.timer1_Tick);

//

// button1

//

this.button1.Location = new System.Drawing.Point(80, 32);

this.button1.Name = "button1";

this.button1.TabIndex = 0;

this.button1.Text = "button1";

this.button1.Click += new System.EventHandler(this.button1_Click);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(232, 101);

this.Controls.Add(this.button1);

this.Name = "Form1";

this.Text = "Form1";

this.WindowState = System.Windows.Forms.FormWindowState.Maximized;

this.Load += new System.EventHandler(this.Form1_Load);

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// The main entry point for the application.

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void Form1_Load(object sender, System.EventArgs e)

{

this.Hide();

}

private void Connecticon_Click(object sender, System.EventArgs e)

{

}

private void timer1_Tick(object sender, System.EventArgs e)

{

string filename;

try

{

i=i+1;

filename= "img" + i.ToString() ;

Capture_Screen(filename);

}

catch(Exception exx)

{

MessageBox.Show(exx.Message.ToString());

this.Dispose();

Application.Exit();

}

}

private void button1_Click(object sender, System.EventArgs e)

{

}

}

}

Problem  Description
----------------------

These programs are taking the screenshots of the wallpaper behind the
windows application along with the mouse cursor .So it is apparent that
mouse is clicking on the particular points of screen where menus or button
exist in my application but doesn't take the screen shot of form. Isn't it
strange? Also these programs are taking the snapshot of other windows based
application.

I am able to take the snapshot of my application (which is not visible to
above programs) using "PrintScreen" and pasting it in the paint.Also I am
able to take the snapshots of some of the forms of application (using above
4 programs), which have a label to display the message and some background
image setup at runtime.

Let me tell you that my windows application uses the skins which can be
changed dynamically using XML and there are few background images setup at
runtime for the forms. I think that the problem is because of some property
of the application or windows form.

I believe that I have described the problem well enough to understand. Now
the ball is in your court. Please help me..........

Regards,

Rajat Tandon.
A DotNet Learner.
David Browne - 03 Dec 2004 18:11 GMT
> Hello there,
>
[quoted text clipped - 17 lines]
> 3) Using Camtasia_Capture&menumaker
> 4) Using the program which is given below...

How about
5) Hit [Prt Scr], launch mspaint and paste.

David
Fredo - 03 Dec 2004 18:27 GMT
I suspect that the poster meant programmatically when he said "at runtime"
so I doubt print screen is going to meet his needs.

But, that said, it occurs to me that a simple way (and I haven't tested
this, so I may be wrong), might be to simply send the print-screen keystroke
via the Win32 SendInput() API function and then get the bitmap out of the
clipboard.

Pete

> > Hello there,
> >
[quoted text clipped - 22 lines]
>
> David
Rajat Tandon - 03 Dec 2004 18:34 GMT
Hi David and everybody,

Thank you for early reply. Of course PrintScreen is an option but not a good
one, As I have to make presentation. Print Screen and paste will require me
to do this for thousands of time.Hence I want the automated way.

Looking for more replies

Bye
Rajat Tandon
A DotNet Learner

> > Hello there,
> >
[quoted text clipped - 22 lines]
>
> David
Scott McChesney - 03 Dec 2004 19:26 GMT
If you have Windows XP, you can use the "PrintWindow" API.  It will take a
snapshot of a window and put it into a DC.

Look in MSDN for info on the PrintWindow() API.  You should be able to
replace the BitBlt() call with a PrintWindow call, and then your code should
work.  It would probably look something like this (UNTESTED CODE):

[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]
private static extern bool PrintWindow
(
   IntPtr hWnd,
   IntPtr hDC,
   int nFlags
);

private void Capture_Screen(string filename)
{
   Image MyImage = new Bitmap(this.Width, this.Height);
   Graphics g = Graphics.FromImage(MyImage);
   IntPtr dc = g.GetHdc();

   PrintWindow(this.Handle,dc,0);
   g.ReleaseHdc(dc);
   MyImage.Save(@"d:\rajat\screen_captures\" + filename + ".jpg"
,System.Drawing.Imaging.ImageFormat.Jpeg);
}

If you don't have XP - well, I'm not sure what to tell you there.

HTH

- Scott

> Hello there,
>
[quoted text clipped - 340 lines]
> Rajat Tandon.
> A DotNet Learner.
Rajat Tandon - 03 Dec 2004 21:42 GMT
Scott, As you suggested about using the PrintWindow API . It is a good step
for Windows XP and to write my own code.Thank you for adding this to my
knowledge but I am more interested with the defect (if any) in my
application. Is there any property of the Window's form/.Net Framework which
is giving us trouble.

There are already a no. of good programs available to capture the screen and
I have already tried 3 of them as written
earlier(Camastasia,Snagit,Irfanview) and one of my own code.All of them were
unsuccessful because of some problem with the application itself. I am very
much sure that all of the new code will only add into the unsuccessful list.

Hence please suggest the possible causes of the failure in the application
itself...

> If you have Windows XP, you can use the "PrintWindow" API.  It will take a
> snapshot of a window and put it into a DC.
[quoted text clipped - 378 lines]
> > Rajat Tandon.
> > A DotNet Learner.
Scott McChesney - 04 Dec 2004 15:29 GMT
Well, I can't comment on why the commercial programs wouldn't work.  As for
the code you have, I don't see anything wrong with it on the surface, in the
sense that it should run.  I don't know of anything in the .NET framework
that would keep this from running, but I'm not a GDI+ expert.  In truth, you
probably would get a better response if you posted this to the
"microsoft.public.dotnet.framework.drawing" newsgroup, since this is right
up their alley.

That said, there is a "quirk" to the code here: you will only be capturing
the client area of the form.  Traditional screen-capture programs copy the
entire form.  If you want to capture the entire form, you'll probably have
to use the desktop as your source DC, and your source rectangle would be the
screen position & size of the entire form.

You also don't need the "MyImage = new
Bitmap(this.ClientRectangle.Width,this.ClientRectangle.Height, g1);" line -
you create and initialize your MyImage variable all in one line, so this
line does the same thing as the one before it.  But that shouldn't hurt
anything.

I don't know what you're seeing, or what you're getting in your JPG file, so
I don't know exactly what your problem is.  As I said, I see nothing in the
code that shouldn't work.  If you can provide some more detail as to what
you're experiencing, we may be able to help some more.

Beyond this, and posting to the other NG I mentioned, I would check out Bob
Powell's GDI+ site - he's about the smartest guy I've seen in the NG's when
it comes to this stuff.  A quick check of his site reveals a page that looks
like it will solve your problem:

http://www.bobpowell.net/capture.htm

Between that and my suggestion about using the screen as your source, you
should be able to get there with some small modifications to this code.

HTH

- Scott

> Scott, As you suggested about using the PrintWindow API . It is a good
> step
[quoted text clipped - 410 lines]
>> > Rajat Tandon.
>> > A DotNet Learner.
Rajat Tandon - 03 Dec 2004 21:46 GMT
Just to add, Right now I am working on Windows 2000 Professional.

> If you have Windows XP, you can use the "PrintWindow" API.  It will take a
> snapshot of a window and put it into a DC.
[quoted text clipped - 378 lines]
> > Rajat Tandon.
> > A DotNet Learner.
Rajat Tandon - 04 Dec 2004 03:53 GMT
I found out the solution of the problem...

It is as simple as

this.AllowTransparency = false; in the loading of forms which are creating
the trouble.

I read somewhere that there is some known bug related to windows 2000 which
is related to the transparency of application but it is still not clear.I
would appreciate if someone could explain this in detail or direct me
towards a good tutorial

Regards,
Rajat Tandon.

> Hello there,
>
[quoted text clipped - 340 lines]
> Rajat Tandon.
> A DotNet Learner.
Stefan Simek - 06 Dec 2004 17:24 GMT
As far as I have tried, the BitBlt method of grabbing doesn't capture
Layered windows (windows that have WS_EX_LAYERED flag set, .NET sets this
when color keying or semi-transparency is activated).

What's wrong about the PrtSc method, anyway? It seems to work all right...

HTH,
Stefan

> Hello there,
>
[quoted text clipped - 345 lines]
> Rajat Tandon.
> A DotNet Learner.

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.