class ScreenShot
{
public static void CaptureImage(Point SourcePoint, Point DestinationPoint, Rectangle SelectionRectangle, string FilePath)
{
using (Bitmap bitmap = new Bitmap(SelectionRectangle.Width, SelectionRectangle.Height))
{
using (Graphics g = Graphics.FromImage(bitmap))
{
g.CopyFromScreen(SourcePoint, DestinationPoint, SelectionRectangle.Size);
}
bitmap.Save(FilePath, ImageFormat.Bmp);
}
}
}
所需添加引用如下:
using System;
using System.Drawing;
using System.Drawing.Imaging;
調用方法:
private void button1_Click(object sender, EventArgs e)
{
saveFileDialog1.DefaultExt = "bmp";
saveFileDialog1.Filter = "bmp files (*.bmp)|*.bmp";
saveFileDialog1.Title = "導出地圖為...";
saveFileDialog1.ShowDialog();
if (saveFileDialog1.FileName.Length > 0)
{
ScreenPath = saveFileDialog1.FileName;
}
else
{
return;
}
this.Refresh();
//3個參數:獲得控件所在屏幕坐標,目標坐標點為(0,0),獲得控件大小。
ScreenShot.CaptureImage(axMapControl1.PointToScreen(Point.Empty), Point.Empty, new Rectangle(axMapControl1.Location, axMapControl1.Size), ScreenPath);
}