473,225 Members | 1,368 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,225 software developers and data experts.

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
InitializeComponent();), 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
myBitmapScreenShot;

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

private void ScreenCapture_button (object sender, EventArgs e)
{
Rectangle screenRect = Screen.PrimaryScreen.WorkingArea;
myBitmapScreenShot = new Bitmap (screenRect.Width,
screenRect.Height);
using (Graphics targetGraphics = Graphics.FromImage
(myBitmapScreenShot))
{
targetGraphics.CopyFromScreen(0,0,0,0,new
Size(myBitmapScreenShot.Width,myBitmapScreenShot.H eight));

}

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

private void MySAVEButton_Click(object sender, EventArgs e)
{

String path;
Stream myStream;
saveFileDialog1.InitialDirectory = "C:\
\imageScreenSave.png";
saveFileDialog1.Filter = "picture files (*.png)|*.png";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;

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

using (Image image = new Bitmap(myBitmapScreenShot))

//wrap Graphics object around image to draw into

using (Graphics imageGraphics =
Graphics.FromImage(image))
{

if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{

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

path = saveFileDialog1.FileName;

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

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

}
Aug 29 '08 #1
2 2746
"raylopez99" <ra********@yahoo.comwrote in message
news:1c**********************************@k7g2000h sd.googlegroups.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.nospamwrote:
"raylopez99" <raylope...@yahoo.comwrote in message

news:1c**********************************@k7g2000h sd.googlegroups.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
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...
5
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. ...
4
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...
2
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...
2
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...
1
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...
1
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...
5
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...
4
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#. ...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.