C # Save the form as an image (Save the profile)

Source: Internet
Author: User

The source code is as follows:

# Region Save the profile private void button_save_Click (object sender, EventArgs e) {SaveFileDialog saveImageDialog = new SaveFileDialog (); saveImageDialog. title = "Save profile chart"; saveImageDialog. defaultExt = ". png "; saveImageDialog. fileName = Linefeaturelayer. name + "profile chart"; saveImageDialog. initialDirectory = "C: \"; DialogResult dr = saveImageDialog. showDialog (); if (dr = DialogResult. OK) {CaptureImage (saveImage Dialog. FileName); MessageBox. Show ("saved successfully! ") ;}} Private void CaptureImage (string path) {try {// obtain the current Screen size Rectangle rect = new Rectangle (); rect = Screen. getWorkingArea (this); // create an image Graphics g1 = this with the current screen as the template. createGraphics (); // create a screen size-based Bitmap Image MyImage = new Bitmap (rect. width, rect. height, g1); Graphics g2 = Graphics. fromImage (MyImage); // obtain the DC IntPtr dc1 = g1.GetHdc (); // obtain the DC IntPtr dc2 = g2.GetHdc () of Bitmap; // call this API function to implement the screen Capture BitBlt (dc2, 0, 0, rect. width, rect. height, dc1, 0, 0, 13369376); // release the DC g1.ReleaseHdc (dc1) on the screen; // release the DC g2.ReleaseHdc (dc2) of Bitmap ); // save MyImage in JPG format. save (path, ImageFormat. png);} catch (System. exception ex) {MessageBox. show (ex. message + "An error occurred while saving the image! ") ;}} // Declare an API function [System. runtime. interopServices. dllImportAttribute ("gdi32.dll")] private static extern bool BitBlt (IntPtr hdcDest, // int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, // source DC handle int nXSrc, int nYSrc, System. int32 dwRop // The processing value of the grating); # endregion
:



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.