以下是我要做的事情:
我没有太多运气试过以下内容:
这将保存图像,但它会捕获顶部的任何窗口以及任务栏和屏幕外内容为白色:
var controlGraphic = mapControl.CreateGraphics();
var img = new Bitmap(mapControl.Width, mapControl.Height, controlGraphic);
var imgGraphic = Graphics.FromImage(img);
var controlGraphicDc = controlGraphic.GetHdc();
var imgGraphicDc = imgGraphic.GetHdc();
BitBlt(imgGraphicDc, 0, 0, mapControl.ClientRectangle.Width, mapControl.ClientRectangle.Height, controlGraphicDc, 0, 0, SRCCOPY);
controlGraphic.ReleaseHdc(controlGraphicDc);
imgGraphic.ReleaseHdc(imgGraphicDc);
img.Save(fileName, ImageFormat.Png);
我尝试的代码类似于此链接中的上一篇文章,但它似乎没有更好的工作: Capture the screen shot using .NET
所以我不认为它与地图控件本身有任何关系,我认为你可以使用任何控件(即WebBrowser)并将其调整为巨大并获得相同的效果。
我想知道是否有人做过类似的事情,或者可能有办法将控件嵌套在一些可滚动控件中并以某种方式捕获其内容,或者可能是不同的上下文并进行捕获?无论如何,我或许可以使用WPF。
感谢。
WebBrowser控件显示相同结果的示例(带有WebBrowser控件的新winforms和按钮):
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WebBrowserCaptureTest
{
public partial class Form1 : Form
{
[DllImport("gdi32.dll")]
private static extern bool BitBlt(
IntPtr hdcDest, // handle to destination DC
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
int dwRop // raster operation code
);
private const int SRCCOPY = 0xCC0020;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.SetBounds(0, 0, 4096, 4096);
webBrowser1.Navigate(@"http://www.google.com");
}
private void CaptureControl(System.Windows.Forms.Control control, string fileName)
{
var controlGraphic = control.CreateGraphics();
var img = new Bitmap(control.Width, control.Height, controlGraphic);
var imgGraphic = Graphics.FromImage(img);
var controlGraphicDc = controlGraphic.GetHdc();
var imgGraphicDc = imgGraphic.GetHdc();
BitBlt(imgGraphicDc, 0, 0, control.ClientRectangle.Width, control.ClientRectangle.Height, controlGraphicDc, 0, 0, SRCCOPY);
controlGraphic.ReleaseHdc(controlGraphicDc);
imgGraphic.ReleaseHdc(imgGraphicDc);
img.Save(fileName, ImageFormat.Png);
}
static void DrawControlToBitmap(Control ctrl, string fileName)
{
System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(ctrl.Width, ctrl.Height);
ctrl.DrawToBitmap(bmp, ctrl.ClientRectangle);
bmp.Save(fileName);
}
private void button1_Click(object sender, EventArgs e)
{
CaptureControl(this.webBrowser1, "Capture.png");
DrawControlToBitmap(webBrowser1, "Capture_B.png");
}
}
}
注意事项: