Friday, January 19, 2007

Snap It! - How to take screen shot using .Net

Introduction

We all would have used snap shots (screen shots) to illustrate bugs or behavior or something that others could understand by seeing them. For taking snap shots we just hit the "Print Screen" key and copy it to Ms Paint and save it. Ever wondered how to programmatically take a snap shot? Then this article is for you.

Image-1 This is how the application looks

Scenario


Why do we need to take snapshots programmatically? Consider a scenario where you want to report some kind of behavior errors in an application. Human supervision is not possible when the occurrence of error is random. So we just code a program to take snap shots are regular intervals and store it as a image file. So that later you can use it.

GDI classes we need to know

Lets see what we need to know before we do coding. We need to have a little knowledge about GDI and graphics classes. The Graphics class provides methods for drawing objects to the display device. A Graphics object is associated with a specific device context. This class is used as a canvas to draw images. In order to display anything in a window, you have to obtain a reference to the window's Graphics object.


Next comes the Bitmap class. A Bitmap object is an object used to work with images defined by pixel data. A bitmap consists of the pixel data for a graphics image and its attributes.

Getting Started


To use the GDI classes we need to include System.Drawing.Imaging namespace in the "using" section of the code. Go to the source view and add a string variable and a property to the class. This string variable is used in the property to store and retrieve the output file name.

// variable to store output file name
private string strFilename;
// Property for storing and retrieving output file name
public string filename
{

get{return strFilename;}
set{strFilename=value;}
}


In the design view, add a button and a savefiledialog to the form. Double click the button and go to the click event in the code view. Use the savefiledialog control to retrieve the output file name from the user. And store the file name in the variable strFilename using the property filename.

// Check user clicked OK button
// Get the filename and put in the textbox and property
if(saveFileDialog1.ShowDialog()!=DialogResult.Cancel)
{

filename=saveFileDialog1.FileName;
}


Before taking the snap shot we hide our application because we don't want our application in the snap shot image. Sometimes even after hiding the application, the snap shot pictures our application, this is because the application takes few extra milliseconds to hide, so we make our application to wait for some milliseconds.

this.Hide();
// Slow the thread to hide the application before taking the snap
Thread.Sleep(200);

How to Snap It?

Now we create a Bitmap object to set the size of the screen. We use the width and height of the application screen to capture the snap shot. So the user can place our application screen on any area of the screen, move/resize to get the snap shot. To make the user more comfortable in selecting the snap shot area we make the application screen semi-transparent by setting the opacity of the form to 50%

Resizing the application
Image-2 Resizing and Moving the screen


// Setting the size of the screen for the bitmap Bitmap bmp= new Bitmap(this.Width,this.Height,PixelFormat.Format32bppArgb);


A Graphics object is required to get the image bounds and create the actual picture. So we initialize a Graphics object and get the image bounds from the Bitmap that we have already created. CopyFromScreen method is used to get the snap shot from the screen. Based on the user selection of the file type in the savefiledialog box, we use different image formats to save the file using Bitmap.Save function.

// Saving file in the selected image format
bmp.Save(filename,ImageFormat.Jpeg);

After saving the picture we show the application back by using the following statement

// Showing the application again this.Show();

Viewing the snap shot


Now the screen is captured and the picture is saved. We need to view the snap shot. There is an option available to see how your snap shot has come up, just right after saving the file. We use Process class of System.Diagnostic namespace to view the image file.

// Viewing the image
Process pr= new Process();
pr.StartInfo.FileName=filename;
pr.Start();

Viewing the imageViewing the image
Image -3 Viewing the saved image file

When the Start method is executed the image file is opened in the respective application configured in the system.
Thats all folks.

Monday, January 15, 2007

File Splitter in .Net



Introduction

I love to do utility programs in .Net. And this is one of them. As I was playing with JSplit (a free file splitter program), I wondered if I could do it in .Net. The framework supports file operations like reading files in bytes and creating them. It is indeed easy to do file operations in .Net Basics on File operations
The namespace System.IO provides classes for file operations. StreamReader is a class which provides methods to read a file in different modes. It allows the user to read file by number of characters, by line, by block and to the end of the file. There is another class FileStream which allows the user to read file by number of bytes. This is what we are going to use.




Getting Started

Lets begin by creating a windows application with a textbox, a numericupdown control, a label and three buttons. Add a openfileDialog control and add some code to extract the filename from the dialog and to put it in the textbox when the browse button is clicked.
if(openFileDialog1.ShowDialog()==DialogResult.OK) { txtFileName.Text=openFileDialog1.FileName;
Now change the Maximum value of the numericupdown control to 1. Change the backcolor of the label to black and forecolor to green (to give a matrix screen effect)

Set the text property of the buttons to "Calculate" and "Split".

Include the namespaces System.IO and System.Text in the Using section.

Calculate Method

In Calculate method we are going to calculate the number of parts the file will be splitted based on the file size and the split size specified by the user.

Double click on the calculate button to go to the Click event. Now initialize the class FileStream by passing the filename from the textbox and specifying file mode and file access parameters.

FileStream FR=new FileStream(txtFileName.Text,FileMode.Open, FileAccess.Read);

The number of parts is caculated by dividing the file size (in bytes) and user selected split size. This is made to display in the label.

int no_of_parts = Int32.Parse(FR.Length.ToString())/intByteSize;
int intmod=Int32.Parse(FR.Length.ToString())%intByteSize;

//Include the remaining bytes
if(intmod > 0)
no_of_parts=no_of_parts+1;


Split Method

In this method we are going to split the file in to user desired size and number of parts. We again use the FileStream class to read the file. But here we are going to read the file by number of bytes required for each part. So after reading a block of bytes required for a part we write it to a file by using FileStream but in the create file mode and write file access.

//Read the file by number of bytes and create part files for(int i=0;i { if(intmod>0 && i==no_of_parts-1) { by=new byte[intmod]; FR.Read(by,0,intmod); } else { by=new byte[intByteSize]; FR.Read(by,0,intByteSize); }

FileStream's WriteByte function is used to write the bytes to a file. Like this number of files are generated for the number of parts calculated. Each file is written with a dynamic name by appending some incrementing number to the file name.

foreach(byte b in by)
{
FW.WriteByte(b);
}


Batch File

The significance of a file splitter is the ability to reunite the file without the actual splitter application. For this, we create a batch file in the split function to reunite the files and create the actual file. This batch file contains statements to combine the part files and generate the actual file.

StringBuilder SB= new StringBuilder(); SB.Append("Copy /b " + tempfilenames + " \"" + filename + "\""); SW.Write(SB.ToString());


We use StringBuilder class to generate the statement for the batch file. The statement includes the command for uniting the files followed by the filenames. This command should be dynamically added to include the filenames. Now we write this statement to a file and save. The content of the batch file should look like the following statement.

Copy /b "test.pdf0" +"test.pdf1" +"test.pdf2" "test.pdf"



Conclusion
While using file operations we should ensure that the file is closed after the read/write process. Thats all folks!
For further reading and code download