473,761 Members | 3,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Screen Capture (Screen Copying) example / the perils of Image/ Bitmapcopying to file

Beware newbies: I spent a day before I figured this out: copying a
bitmap (image) file to file is not quite like copying a text file--you
have to do some tricks (see below), like using a "Graphics" object to
wrap around the image (!). It's not so simple as shown in most
examples (where they have a simple image file and hard copy it into a
harddrive along the lines of : image.Save(@"C: \\temp\\myimage .pgn");
That will work, but it's a rare bird that needs this.

The below is adapted from Chris Sells book and from another thread
here. The only thing I have not figured out is whether you need to
use .Close on the stream that the image is stored /backed into--I
think it can be safely deleted, since it's redundant to the using
bracket, but I'll await expert commentary from here.

RL

http://tinyurl.com/5a7p5q (older thread that outlines problem)

I found the solution. It was indeed the pecularities of Image and
Bitmap (which is a subset of Image). It's very bizarre. I assumed
Bitmap and Image are like 'text files' except binary, but in fact you
have to 'wrap them' in a graphics object. See the below complete
code, which works, on a form of screen capture.

Adapted from Chris Sells Chapter 5 "Windows Forms 2.0 Programming"

//declare this member variable before class Form instantiation as a
member variable (that is, outside the class that has
InitializeCompo nent();), say just after your second bracket after the
namespace bracket "{" will do, after ' public partial class MyForm:
Form'. Don't initialize, just declare it: Bitmap
myBitmapScreenS hot;

//click on this button to capture what's painted on the screen.

private void ScreenCapture_b utton (object sender, EventArgs e)
{
Rectangle screenRect = Screen.PrimaryS creen.WorkingAr ea;
myBitmapScreenS hot = new Bitmap (screenRect.Wid th,
screenRect.Heig ht);
using (Graphics targetGraphics = Graphics.FromIm age
(myBitmapScreen Shot))
{
targetGraphics. CopyFromScreen( 0,0,0,0,new
Size(myBitmapSc reenShot.Width, myBitmapScreenS hot.Height));

}

// now click on this button to save the screenshot to file

private void MySAVEButton_Cl ick(object sender, EventArgs e)
{

String path;
Stream myStream;
saveFileDialog1 .InitialDirecto ry = "C:\
\imageScreenSav e.png";
saveFileDialog1 .Filter = "picture files (*.png)|*.png";
saveFileDialog1 .FilterIndex = 2;
saveFileDialog1 .RestoreDirecto ry = true;

using (Graphics displayGraphics = this.CreateGrap hics())
//create Bitmap to draw into based on existing Graphics
object (p. 222 Sells)

using (Image image = new Bitmap(myBitmap ScreenShot))

//wrap Graphics object around image to draw into

using (Graphics imageGraphics =
Graphics.FromIm age(image))
{

if (saveFileDialog 1.ShowDialog() == DialogResult.OK )
{

if ((myStream = saveFileDialog1 .OpenFile()) !=
null)
{

path = saveFileDialog1 .FileName;

//image.Save(path );//works not at all- why?
Who cares!

image.Save(mySt ream,
System.Drawing. Imaging.ImageFo rmat.Png);
//works fine! (and that's all that matters)
myStream.Close( ); //is this redundant since
wrapped in a 'using'?
}
}
}

}
Aug 29 '08 #1
2 2817
"raylopez99 " <ra********@yah oo.comwrote in message
news:1c******** *************** ***********@k7g 2000hsd.googleg roups.com...
Beware newbies: I spent a day before I figured this out: copying a
bitmap (image) file to file is not quite like copying a text file--you
have to do some tricks (see below), like using a "Graphics" object to
wrap around the image (!). It's not so simple as shown in most
examples (where they have a simple image file and hard copy it into a
harddrive along the lines of : image.Save(@"C: \\temp\\myimage .pgn");
That will work, but it's a rare bird that needs this.

The below is adapted from Chris Sells book and from another thread
here. The only thing I have not figured out is whether you need to
use .Close on the stream that the image is stored /backed into--I
think it can be safely deleted, since it's redundant to the using
bracket, but I'll await expert commentary from here.

...

I'm not sure what you're trying to say.

Saving Bitmaps has nothing to do with the Graphics class.

Bitmap bits are often written to directly, which doesn't involve a Graphics.

Creating a Graphics from a Bitmap lets you use the Graphics methods to draw
on the Bitmap.

Cheers,
Mark

--
Mark Salsbery
Microsoft MVP - Visual C++


Aug 30 '08 #2
On Aug 29, 5:19*pm, "Mark Salsbery [MVP]"
<MarkSalsbery[MVP]@newsgroup.nosp amwrote:
"raylopez99 " <raylope...@yah oo.comwrote in message

news:1c******** *************** ***********@k7g 2000hsd.googleg roups.com...
Beware newbies: *I spent a day before I figured this out: *copying a
bitmap (image) file to file is not quite like copying a text file--you
have to do some tricks (see below), like using a "Graphics" object to
wrap around the image (!). *It's not so simple as shown in most
examples (where they have a simple image file and hard copy it into a
harddrive along the lines of : image.Save(@"C: \\temp\\myimage .pgn");
That will work, but it's a rare bird that needs this.
The below is adapted from Chris Sells book and from another thread
here. *The only thing I have not figured out is whether you need to
use .Close on the stream that the image is stored /backed into--I
think it can be safely deleted, since it's redundant to the using
bracket, but I'll await expert commentary from here.
...

I'm not sure what you're trying to say.

Saving Bitmaps has nothing to do with the Graphics class.

Bitmap bits are often written to directly, which doesn't involve a Graphics.

Creating a Graphics from a Bitmap lets you use the Graphics methods to draw
on the Bitmap.

Cheers,
Mark

--
Mark Salsbery
Microsoft MVP - Visual C++
I'm sure you're right, but, since this particular application was for
a screenshot (which uses the Graphics class), I could not get it to
work until I wrapped the Bitmap in the graphics class as shown. The
bitmap was stored in a picturebox, which I thought was
'static' (doesn't change on Paint) but I could not store it. Perhaps
the problem was I was downcasting from Image to Bitmap (not clear if I
was, but see the link in the original thread), which apparently is not
good. In any event, following the template I posted, and upcasting
from Bitmap to Image, it worked and the screenshot is fine, and saves
properly.

All's well that ends well, which is all you can ever hope for in these
complex managed languages (unless you want to dive under the hood and
get into assembly-like language and decorator classes, vtables and
what not to see what went wrong).

RL
Aug 30 '08 #3

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

Similar topics

3
13531
by: Me Mine | last post by:
i, I have trying to code a small console app that will allow a user to select a window and then create a screen capture of the window. I haven't been able to figure out how to do the screen capture part. The code is as follows, the commented out lines at the end are things I have tried, but don't work. Whenever I try to create a compatibe Bitmap or DC it comes back with an error as win32ui: CreateCompatibleDC failed
5
2382
by: TerryWilson | last post by:
I am developing a web based service tool using asp.net that we will distribute with our product. This service tool will be used for remotely configuring the product, problem determination, etc. One thing that I would like to be able to do is display a screen capture of our product running. I have the code to get the screen capture of the desktop and I also know how to send the image back to the client to be displayed. The problem is that...
4
6382
by: gregory_may | last post by:
http://www.geocities.com/krishnapg/screencap.html This article gives a pretty good background on various ways to capture the screen. I have some code to do it the GDI way below, but I cant figure out how to get the Mouse in there. I am frustrated and looking at Direct X. I am may have to try the Windows Media API , but know nothing about it. Anyone have a way to get the mouse in my screen capture, any examples would be great (GDI,...
2
2415
by: py | last post by:
I need to take a screen shot of the computer screen. I am trying to use PIL and I saw there is ImageGrab...however it only works on Windows. Is there a platform-independent ability to work around this? thanks
2
6624
by: dumbledad | last post by:
Hi All, I'm using ASP.Net web services to provide the logic required for a Flash based prototype. The Flash and the ASP .Net run together on the client machine. One of the functions I'd like to offer is public void StoreLogEntry(string tag, string description, bool screenshotRequired)
1
4507
by: JP2006 | last post by:
I'm trying to write a control that will take a screen capture of a particular website when a user submits a form in a web application; one of the form fields is for a URL - the control needs to get an image of that web site so that it can be displayed later as a thumbnail image. I have code for taking a normal screen capture using GDI+ which works fine. What I am now trying to do is to modify it so the screen capture is of a remote...
1
4628
by: jcmag | last post by:
I would like to take a "screen capture" of just a Form. I've done the following: internal void CaptureScreen() { Graphics g1 = CreateGraphics(); Image MyImage = new Bitmap(ClientRectangle.Width, ClientRectangle.Height, g1); Graphics g2 = Graphics.FromImage(MyImage); IntPtr dc1 = g1.GetHdc();
5
9019
Atran
by: Atran | last post by:
Hello Everyone. In this article: You will know to capture the screen in 2 ways: 1)- Capture full screen. 2)- Capture region. Let's Begin: First make a new Windows Application project. And make sure your program uses these namespaces: using System;
4
2392
by: bmchheda1 | last post by:
Hello, I am creating the custom web crawler in c#. I want to incorporate screen capturing of the WebPage in the application. Where can I find the example of screen capture of WebPage in c#. Also my exact requirement is that I want to record the full webpage or portion of the webpage for a time specified and then save it as Video file as AVI. Thanks,
1
9909
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9788
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8794
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7342
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6623
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5241
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.