Create QQ screenshot Software

Source: Internet
Author: User

I recently learned about GDI and found several articles on the Internet about software production methods. I learned a little about it. Here I will share it with you.

It mainly calls functions in winapi to complete the main functions. there are two key functions: createdc, which is used to create a DC (device environment) for the display screen as the source DC, and then create an image, through Graphics of this image. the gethdc () method is used to obtain another DC, which is used as the target DC. The two DC are mainly used by the second function. The second function is bitblt, this function scans pixels on the source DC to the Target DC. Here, the pixels on the display screen are scanned to the image we created.

After scanning, the image is the current full screen image. You can save or copy the image to be used. This enables the full screen function.

The following is the implementation of local. first, the full screen image is painted on the winform form. This can be achieved by using the form's paint event. When the form changes, the paint event will be triggered. partial is to draw a rectangle on the winform that has already painted a full screen image, and then call the graphics method drawimage to save the rectangular area to the image you created. In this way, the local image is obtained, you can save or copy images to be used.

The function is basically implemented. it should be noted that the rectangle drawn in the form is a reversible rectangle that calls controlpaint. drawreversibleframe () method. This method must be executed twice to draw a rectangle.

Main Window code:

    public partial class Form1 : Form
{
//*******************Global Varibles******************
private Bitmap MyImage = null;
private bool StartedCrop = false;

Point StartPoint = new Point(0, 0);
Rectangle SelectRect = new Rectangle();
int DeltaX = 0;
int DeltaY = 0;
//****************************************************

[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
public static extern int BitBlt(
IntPtr hDestDC,
int x,
int y,
int nWidth,
int nHeight,
IntPtr hSrcDC,
int xSrc,
int ySrc,
System.Int32 dwRop
);
[DllImport("gdi32.dll", EntryPoint = "CreateDC")]
public static extern IntPtr CreateDC(
string lpDriverName,
string lpDeviceName,
string lpOutput,
IntPtr lpInitData
);

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
this.Visible = false;
Thread.Sleep(100);
IntPtr dc1 = CreateDC("DISPLAY", null, null, (IntPtr)null);
Graphics g1 = Graphics.FromHdc(dc1);
MyImage = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, g1);
Graphics g2 = Graphics.FromImage(MyImage);
//Visible = false;
dc1 = g1.GetHdc();
IntPtr dc2 = g2.GetHdc();
BitBlt(dc2, 0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height,
dc1, 0, 0, 13369376);

g1.ReleaseHdc(dc1);
g2.ReleaseHdc(dc2);
this.Visible = true;
this.SetBounds(0, 0, Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
this.Cursor = Cursors.Cross;
}

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (StartedCrop == false)
{
DeltaX = 0;
DeltaY = 0;
}
StartedCrop = true;
StartPoint = new Point(e.X,e.Y);
SelectRect.Width = 0;
SelectRect.Height = 0;
SelectRect.X = e.X;
SelectRect.Y = e.Y;
Invalidate();
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Form thisform = (Form)sender;
if (StartedCrop)
{
DeltaX = e.X - StartPoint.X;
if (DeltaX < 0)
DeltaX = 0;
DeltaY = e.Y - StartPoint.Y;
if (DeltaY < 0)
DeltaY = 0;
ControlPaint.DrawReversibleFrame(thisform.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed);
SelectRect.Width = e.X - SelectRect.X;
SelectRect.Height = e.Y - SelectRect.Y;
ControlPaint.DrawReversibleFrame(thisform.RectangleToScreen(SelectRect), Color.Black, FrameStyle.Dashed);
}
}

private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (DeltaX == 0 || DeltaY == 0)
{
return;
}

StartedCrop = false;
SelectRect.X = e.X - StartPoint.X;
SelectRect.Y = e.Y - StartPoint.Y;
this.Cursor = Cursors.Cross;


Bitmap theImage = new Bitmap(DeltaX, DeltaY);
Graphics g = Graphics.FromImage(theImage);
Rectangle destRect = new Rectangle(0, 0, DeltaX, DeltaY);
g.DrawImage(MyImage, destRect, StartPoint.X, StartPoint.Y, theImage.Width, theImage.Height, GraphicsUnit.Pixel);
MyImage = (Bitmap)theImage.Clone();
this.SetBounds(0, 0, MyImage.Width, MyImage.Height);
this.Visible = false;

frmOptions optionsForm = new frmOptions();
optionsForm.imageToSaveOrCopy = MyImage;
optionsForm.ShowDialog();
}

private void Form1_Paint(object sender, PaintEventArgs e)
{
if (MyImage != null)
e.Graphics.DrawImage(MyImage, ClientRectangle, 0, 0, MyImage.Width, MyImage.Height, GraphicsUnit.Pixel);
}

private void Form1_Resize(object sender, EventArgs e)
{
Invalidate();
}

private void Form1_DoubleClick(object sender, EventArgs e)
{
Application.Exit();
}
}

The option window code (frmoptions) has two buttons to save or copy the captured image:

    public partial class frmOptions : Form
{
public Image imageToSaveOrCopy;

public frmOptions()
{
InitializeComponent();
}

private void frmOptions_Load(object sender, EventArgs e)
{

}

private void btnSave_Click(object sender, EventArgs e)
{
if (imageToSaveOrCopy == null) {
MessageBox.Show("You have not choose the image! Please restart this program!");
Application.Exit();
}

SaveFileDialog sfd = new SaveFileDialog();
sfd.Filter = "*.jpg|*.jpg";
sfd.AddExtension = true;
sfd.FileName = "1.jpg";
if (sfd.ShowDialog() == DialogResult.OK)
{
imageToSaveOrCopy.Save(sfd.FileName);
}
Application.Exit();
}

private void btnCopy_Click(object sender, EventArgs e)
{
if (imageToSaveOrCopy == null)
{
MessageBox.Show("You have not choose the image! Please restart this program!");
Application.Exit();
}
try
{
Clipboard.SetImage(imageToSaveOrCopy);
}
catch (Exception ex)
{
MessageBox.Show("Copy to clipboard error:\r\n"+ex.Message);
}
Application.Exit();
}
}

In some cases, you need to draw a picture on the screen. to map the screen position (point) to the position of our main form one by one, you need to maximize the main form to the full screen. To improve the effect, you can
The borderstyle attribute of the form is set to none. This completes the work.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.