[VB 2008] Screenshot Taker?
I've searched everywhere for a code that allows you to take a screenshot of a selected area of the screen in VB 2008 for a project I'm working on.
I don't want it to take a screen shot of the full screen, I want the user to be able to select the coordinates themselves, just like the Snipping Tool on Windows 7.
If anyone could find me a code like that I'd be very grateful :)
Re: [VB 2008] Screenshot Taker?
Try google :)
Use the code for a full screen screenshot, just change the pos of where its taking the screenshot.
Easiest way make a form with a button, then use the forms positions cord for where to copy pixels from the screen.
Re: [VB 2008] Screenshot Taker?
Quote:
Originally Posted by
viroware
Try google :)
If you read the first 3 words of the fucking thread, I did put: "I've searched everywhere".
Re: [VB 2008] Screenshot Taker?
Im sorry, I didnt notice you dont know how to use google, Type it in the box and click the magic button.
It isnt that hard to find, I found loads of tutorials when researching vb, and i did make a region tool for my screenshot program.
Re: [VB 2008] Screenshot Taker?
edit: I didn't see the part about the selected area but I will look at it and update my post. Sorry about that.
Hello, I had to do this once for a C# app. I only had it in C# so I switched it to VB.NET for you.
Enjoy!
First you must import this:
PHP Code:
Imports System.Drawing.Imaging
Put this into the button_click sub or anywhere you want it to be. So it will execute the GetScreenShot sub
PHP Code:
GetScreenShot()
Copy and paste this, so you create the GetScreenShot sub.
PHP Code:
Sub GetScreenShot()
Dim result As Bitmap
Dim pt1 As Integer
Dim pt2 As Integer
result = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
Using (Graphics.FromImage(result))
pt1 = Screen.PrimaryScreen.Bounds.X
pt2 = Screen.PrimaryScreen.Bounds.X
Graphics.FromImage(result).CopyFromScreen(pt1, pt2, 0, 0, result.Size, CopyPixelOperation.SourceCopy)
End Using
'It will save it into your BIN directory
PictureBox1.Image = result
result.Save("test.bmp", ImageFormat.Bmp)
End Sub
For the selected area part it get more tricky. The way I would see this, is to first of all, take a full screenshot of the primary screen.
Then you print this as this as the background-image of a form and remove the border of this form so it look like the primary screen background itself.
After that, on the mousedown you would set the start coordinate of your mouse and on the Mousemove event you could update a function that would draw a rectangle using DrawRectangle with the new current mouse position, so everytime iy get into this function the rectangle redraw itself. At the end, on the mouse up you print what is inside this rectangle and save it somewhere.
Sorry for my poor english. I just typed this really quickly.
I gotta go sleep so, i'll come back with this tomorow
Re: [VB 2008] Screenshot Taker?
Cheers pal, I'll check it out now to see if it works :)
Re: [VB 2008] Screenshot Taker?
this is what i use...
Code:
Private Function screenshot()
'Setting screen shot functions
' Create a new Bitmap object with the screen bounds
Dim ss As Bitmap = New Bitmap(My.Computer.Screen.Bounds.Width, My.Computer.Screen.Bounds.Height, Imaging.PixelFormat.Format32bppArgb)
' Create a Graphics object that will process the screen shot
Dim gfx As Graphics = Graphics.FromImage(ss)
' Copy the screen contents
gfx.CopyFromScreen(My.Computer.Screen.Bounds.X, My.Computer.Screen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size, CopyPixelOperation.SourceCopy)
' Save the resulting graphics
ss.Save(My.Computer.FileSystem.CurrentDirectory & "\screenshots\" & "image.jpg", Imaging.ImageFormat.Jpeg)
End Function
Re: [VB 2008] Screenshot Taker?
Thanks, but I wanted it where the user can specify the coordinates themselves and then it will crop the image and save it automatically.
Re: [VB 2008] Screenshot Taker?
wow didnt know u didnt know how to read code.
Code:
My.Computer.Screen.Bounds.X, My.Computer.Screen.Bounds.Y, 0, 0
and then
Code:
ss.Save(My.Computer.FileSystem.CurrentDirectory & "\screenshots\" & "image.jpg", Imaging.ImageFormat.Jpeg)
look thats saves it too.
Re: [VB 2008] Screenshot Taker?
Quote:
Originally Posted by
-DefaulT
wow didnt know u didnt know how to read code.
Code:
My.Computer.Screen.Bounds.X, My.Computer.Screen.Bounds.Y, 0, 0
and then
Code:
ss.Save(My.Computer.FileSystem.CurrentDirectory & "\screenshots\" & "image.jpg", Imaging.ImageFormat.Jpeg)
look thats saves it too.
I dont think you've read his first post correctly. He wants to take a screenshot of a specified selected area. But as I said in my other post, you will have to draw a rectangle on runtime and update this rectangle on the MouseMove event. The hard thing is to draw a proper rectangle depeding if you are selecting from Left to Right + bottom OR Right to Left + bottom OR Left to Right + Top OR Right to Left + Top.
Here is the algorithm I made in C# for the MouseMove event:
Code:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X > startPoint.X && e.Y > startPoint.Y)
{
myRectangle = new Rectangle(startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
}
else
{
if (e.X < startPoint.X && e.Y > startPoint.Y)
{
myRectangle = new Rectangle(e.X, startPoint.Y, startPoint.X - e.X, e.Y - startPoint.Y);
}
else
{
if (e.X < startPoint.X && e.Y < startPoint.Y)
{
myRectangle = new Rectangle(e.X, e.Y, startPoint.X - e.X, startPoint.Y - e.Y);
}
else
{
myRectangle = new Rectangle(startPoint.X, e.Y, e.X - startPoint.X, startPoint.Y - e.Y);
}
}
}
}
this.Invalidate();
}
The startPoint variable is set when the MouseDown event is activated. The rectangle is based on this startPoint and it shape will update depending on the new X/Y cursor coordinate, in the MouseMove event.
@m0nsta
I will try finish this by tomorow after my exam.
Hope this will help ;)
Re: [VB 2008] Screenshot Taker?
Quote:
Originally Posted by
Hilroy
I dont think you've read his first post correctly. He wants to take a screenshot of a specified selected area. But as I said in my other post, you will have to draw a rectangle on runtime and update this rectangle on the MouseMove event. The hard thing is to draw a proper rectangle depeding if you are selecting from Left to Right + bottom
OR Right to Left + bottom
OR Left to Right + Top
OR Right to Left + Top.
Here is the algorithm I made in C# for the MouseMove event:
Code:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (e.X > startPoint.X && e.Y > startPoint.Y)
{
myRectangle = new Rectangle(startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
}
else
{
if (e.X < startPoint.X && e.Y > startPoint.Y)
{
myRectangle = new Rectangle(e.X, startPoint.Y, startPoint.X - e.X, e.Y - startPoint.Y);
}
else
{
if (e.X < startPoint.X && e.Y < startPoint.Y)
{
myRectangle = new Rectangle(e.X, e.Y, startPoint.X - e.X, startPoint.Y - e.Y);
}
else
{
myRectangle = new Rectangle(startPoint.X, e.Y, e.X - startPoint.X, startPoint.Y - e.Y);
}
}
}
}
this.Invalidate();
}
The startPoint variable is set when the MouseDown event is activated. The rectangle is based on this startPoint and it shape will update depending on the new X/Y cursor coordinate, in the MouseMove event.
@m0nsta
I will try finish this by tomorow after my exam.
Hope this will help ;)
Thanks! You are a great help :)
Credits will be given to you in the program I am making :)
Re: [VB 2008] Screenshot Taker?
Ok so here is my code in C#.
Global variables
Code:
Rectangle myRectangle;
Point startPoint;
Point recTopPoint = new Point();
cScreenShot screenshot = new cScreenShot();
Form constructor
Code:
public Form1()
{
InitializeComponent();
//set a new cursor
this.Cursor = System.Windows.Forms.Cursors.Cross;
//prevent flicker
this.DoubleBuffered = true;
}
The loading part is a bit sketchy but working.
On the form Load event we will make the form transparent and then start a timer. The timer will have a 2 sec interval (2000ms). After the 2000 ms interval, it will take a screenshot of our user screen. I added this timer because executing the screenshot function on the form load event was to quick. I made this fix kinda quickly so, feel free to edit this part to your need.
form load event
Code:
private void Form1_Load(object sender, EventArgs e)
{
//Hide the form
this.Opacity = 0.0;
//Set the form so it will look like being the real background screen
this.FormBorderStyle = FormBorderStyle.None;
this.WindowState = FormWindowState.Maximized;
//start the timer
timer1.Enabled = true;
}
And then the timer tick event
Code:
private void timer1_Tick(object sender, EventArgs e)
{
//kill the timer
timer1.Enabled = false;
//New screen capture. The function will return the full screen bitmap
Bitmap rBitmap = screenshot.GetFullScreenCapture();
//set the form background image
this.BackgroundImage = rBitmap;
//Reset the opacity to 1 (100% visible)
this.Opacity = 1;
}
The mouse down part is where we will create the rectangle and set the real start point of this rectangle. The start point is constant and will never change.
Code:
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
//if the left mouse button is down
if (e.Button == MouseButtons.Left)
{
//Set our new start point
startPoint = new Point(e.X, e.Y);
//create a new rectangle
myRectangle = new Rectangle(e.X, e.Y, 0, 0);
//cause our rectangle to redraw itself
this.Invalidate();
}
}
For the mouse move event we will simply redraw our rectangle depending of our mouse coord, just like I explained in my last post, but this time we will keep in mind the upper left X and Y coordinate of our rectangle for the screenshot area part!
Code:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
//if the left mouse button is down
if (e.Button == MouseButtons.Left)
{
//check if we are drawing our rectangle from right bottom
if (e.X > startPoint.X && e.Y > startPoint.Y)
{
//Create a new rectangle with our new coordinate and size.
myRectangle = new Rectangle(startPoint.X, startPoint.Y, e.X - startPoint.X, e.Y - startPoint.Y);
//Set the upper-X and upper-Y coordinate as a new point (top left of our rectangle)
recTopPoint.X = startPoint.X;
recTopPoint.Y = startPoint.Y;
}
else
{
//check if we are drawing our rectangle from left bottom
if (e.X < startPoint.X && e.Y > startPoint.Y)
{
myRectangle = new Rectangle(e.X, startPoint.Y, startPoint.X - e.X, e.Y - startPoint.Y);
recTopPoint.X = e.X;
recTopPoint.Y = startPoint.Y;
}
else
{
//check if we are drawing our rectangle from top left
if (e.X < startPoint.X && e.Y < startPoint.Y)
{
myRectangle = new Rectangle(e.X, e.Y, startPoint.X - e.X, startPoint.Y - e.Y);
recTopPoint.X = e.X;
recTopPoint.Y = e.Y;
}
else
{
//check if we are drawing our rectangle from top right
myRectangle = new Rectangle(startPoint.X, e.Y, e.X - startPoint.X, startPoint.Y - e.Y);
recTopPoint.X = startPoint.X;
recTopPoint.Y = e.Y;
}
}
}
}
//cause our rectangle to redraw itself
this.Invalidate();
}
Paint event will simply "paint" our rectangle:
Code:
private void Form1_Paint(object sender, PaintEventArgs e)
{
//use pen(color of pen, size of border)
using (Pen pen = new Pen(Color.Red,1))
{
//draw our rectangle
e.Graphics.DrawRectangle(pen, myRectangle);
}
}
The MouseUp event is where we will get what is inside our rectangle.
Code:
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
//We check if our rectangle IS a rectangle and not a line.
if (myRectangle.Height > 0 && myRectangle.Width > 0)
{
/*Execute our function that will take a snapshot of what is inside the rectangle with
the specified area*/
//SnapshotRectangle(top left corner, our rectangle itself)
screenshot.SnapshotRectangle(recTopPoint, myRectangle);
//Just close the program when the snapshot is done
this.Close();
}
}
I made a class for the screenshot and rectangle snapshot functions but you can edit this. So here is the whole class block of code
Code:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace screenshot_taker_area
{
/// <summary>
/// EVERYTHING ABOUT TAKING A PICTUREEE
/// </summary>
class cScreenShot
{
/// <summary>
/// //Get the rectangle Snapshot (top left of our rectangle, rectangle itself)
/// </summary>
/// <param name="TopLeft"></param>
/// <param name="myRectangle"></param>
public void SnapshotRectangle(Point TopLeft, Rectangle myRectangle)
{
//Create a new bitmap with the specified size (size of our rectangle) !
using (Bitmap ss = new Bitmap(myRectangle.Width-1, myRectangle.Height-1, System.Drawing.Imaging.PixelFormat.Format32bppArgb))
{
using (Graphics gfx = Graphics.FromImage(ss))
{
//Get the snapshot form screen from our rectangle area (:
gfx.CopyFromScreen(TopLeft.X+1, TopLeft.Y+1, 0, 0, ss.Size, CopyPixelOperation.SourceCopy);
}
//save it
ss.Save("rectangle.png", System.Drawing.Imaging.ImageFormat.Png);
}
}
/// <summary>
/// Get a screenshot of our user screen
/// </summary>
/// <returns></returns>
public Bitmap GetFullScreenCapture()
{
Bitmap resultBtm;
int pt1;
int pt2;
//Create a new bitmap with the user screen bounds
resultBtm = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);
using (Graphics.FromImage(resultBtm))
{
//Get the screenshot
pt1 = Screen.PrimaryScreen.Bounds.X;
pt2 = Screen.PrimaryScreen.Bounds.Y;
Graphics.FromImage(resultBtm).CopyFromScreen(pt1, pt2, 0, 0, resultBtm.Size, CopyPixelOperation.SourceCopy);
}
//return our bitmap
return resultBtm;
}
}
}