解决C#全屏幕截图的实现方法
程序员文章站
2023-12-20 22:15:04
今天一位同事想写一个全屏幕截图的代码。当然要实现的第一步是能够获取整个屏幕的位图,记得win32 api的createdc, bitblt等函数可以使用。于是上网查了下,果...
今天一位同事想写一个全屏幕截图的代码。当然要实现的第一步是能够获取整个屏幕的位图,记得win32 api的createdc, bitblt等函数可以使用。于是上网查了下,果然屏幕截图用这些函数。但winform已经可以把api都忘记了,所以得寻找一个无win32 api的实现方式。综合了网上的实现,以及自己的一些设计,实现思路如下:
1. 开始截图时,创建一个与屏幕大小一样的位图,然后用graphics.copyfromscreen()把屏幕位图拷贝到该位图上。这是很关键的一步,这样所有的操作就都可以在该位图上进行了,而无实际屏幕无关了。
code
int width = screen.primaryscreen.bounds.width;
int height = screen.primaryscreen.bounds.height;
bitmap bmp = new bitmap(width, height);
using (graphics g = graphics.fromimage(bmp)) {
g.copyfromscreen(0, 0, 0, 0, new size(width, height));
}
2. 接下来为了方便在这之上进行截图,有一个很重要的设计实现方式:用全屏幕窗体代替现有真实屏幕,这样就可以把截图过程的所有操作都在那个窗体上实现(该窗体设置成无边框,高宽等于屏幕大小即可),另外为了显示掩蔽效果(只能正常显示选择的部分屏幕内容,而其实部分用一个如半透明层覆盖),就添加一层半透明位置位图。具体代码如下:
code
public partial class fullscreenform : form {
private rectangle rectselected = rectangle.empty;
private bool isclipping = false;
private bitmap screen;
private bitmap coverlayer = null;
private color covercolor;
private brush rectbrush = null;
private bitmap resultbmp = null;
public fullscreenform(bitmap screen) {
initializecomponent();
int width = screen.primaryscreen.bounds.width;
int height = screen.primaryscreen.bounds.height;
coverlayer = new bitmap(width, height);
covercolor = color.fromargb(50, 200, 0, 0);
rectbrush = new solidbrush(covercolor);
using (graphics g = graphics.fromimage(coverlayer)) {
g.clear(covercolor);
}
this.bounds = new rectangle(0, 0, width, height);
this.screen = screen;
this.doublebuffered = true;
}
protected override void onmousedown(mouseeventargs e) {
if (e.button == mousebuttons.left) {
isclipping = true;
rectselected.location = e.location;
}
else if (e.button == mousebuttons.right) {
this.dialogresult = dialogresult.ok;
}
}
protected override void onmousemove(mouseeventargs e) {
if (e.button == mousebuttons.left && isclipping) {
rectselected.width = e.x - rectselected.x;
rectselected.height = e.y - rectselected.y;
this.invalidate();
}
}
protected override void onmouseup(mouseeventargs e) {
if (e.button == mousebuttons.left && isclipping) {
rectselected.width = e.x - rectselected.x;
rectselected.height = e.y - rectselected.y;
this.invalidate();
resultbmp = new bitmap(rectselected.width, rectselected.height);
using (graphics g = graphics.fromimage(resultbmp)) {
g.drawimage(screen,new rectangle(0, 0, rectselected.width, rectselected.height), rectselected, graphicsunit.pixel);
}
this.dialogresult = dialogresult.ok;
}
}
protected override void onpaint(painteventargs e) {
graphics g = e.graphics;
g.drawimage(screen, 0, 0);
g.drawimage(coverlayer, 0, 0);
paintrectangle();
}
protected override void onpaintbackground(painteventargs e) {
}
protected override void onkeydown(keyeventargs e) {
if (e.keycode == keys.escape) {
this.dialogresult = dialogresult.cancel;
}
}
private void paintrectangle() {
using (graphics g = graphics.fromimage(coverlayer)) {
g.clear(covercolor);
graphicspath path = new graphicspath();
path.addrectangle(this.bounds);
path.addrectangle(rectselected);
g.fillpath(rectbrush, path);
g.drawrectangle(pens.blue, rectselected);
}
}
public bitmap resultbitmap {
get { return resultbmp; }
}
}
上面的代码都很容易看明白,这里有一个技巧就是graphicspath,它自动会形成一个中空的区域。上面的实现很容易扩展:多区域截图,多裁判截图等都很容易实现。
1. 开始截图时,创建一个与屏幕大小一样的位图,然后用graphics.copyfromscreen()把屏幕位图拷贝到该位图上。这是很关键的一步,这样所有的操作就都可以在该位图上进行了,而无实际屏幕无关了。
复制代码 代码如下:
code
int width = screen.primaryscreen.bounds.width;
int height = screen.primaryscreen.bounds.height;
bitmap bmp = new bitmap(width, height);
using (graphics g = graphics.fromimage(bmp)) {
g.copyfromscreen(0, 0, 0, 0, new size(width, height));
}
2. 接下来为了方便在这之上进行截图,有一个很重要的设计实现方式:用全屏幕窗体代替现有真实屏幕,这样就可以把截图过程的所有操作都在那个窗体上实现(该窗体设置成无边框,高宽等于屏幕大小即可),另外为了显示掩蔽效果(只能正常显示选择的部分屏幕内容,而其实部分用一个如半透明层覆盖),就添加一层半透明位置位图。具体代码如下:
复制代码 代码如下:
code
public partial class fullscreenform : form {
private rectangle rectselected = rectangle.empty;
private bool isclipping = false;
private bitmap screen;
private bitmap coverlayer = null;
private color covercolor;
private brush rectbrush = null;
private bitmap resultbmp = null;
public fullscreenform(bitmap screen) {
initializecomponent();
int width = screen.primaryscreen.bounds.width;
int height = screen.primaryscreen.bounds.height;
coverlayer = new bitmap(width, height);
covercolor = color.fromargb(50, 200, 0, 0);
rectbrush = new solidbrush(covercolor);
using (graphics g = graphics.fromimage(coverlayer)) {
g.clear(covercolor);
}
this.bounds = new rectangle(0, 0, width, height);
this.screen = screen;
this.doublebuffered = true;
}
protected override void onmousedown(mouseeventargs e) {
if (e.button == mousebuttons.left) {
isclipping = true;
rectselected.location = e.location;
}
else if (e.button == mousebuttons.right) {
this.dialogresult = dialogresult.ok;
}
}
protected override void onmousemove(mouseeventargs e) {
if (e.button == mousebuttons.left && isclipping) {
rectselected.width = e.x - rectselected.x;
rectselected.height = e.y - rectselected.y;
this.invalidate();
}
}
protected override void onmouseup(mouseeventargs e) {
if (e.button == mousebuttons.left && isclipping) {
rectselected.width = e.x - rectselected.x;
rectselected.height = e.y - rectselected.y;
this.invalidate();
resultbmp = new bitmap(rectselected.width, rectselected.height);
using (graphics g = graphics.fromimage(resultbmp)) {
g.drawimage(screen,new rectangle(0, 0, rectselected.width, rectselected.height), rectselected, graphicsunit.pixel);
}
this.dialogresult = dialogresult.ok;
}
}
protected override void onpaint(painteventargs e) {
graphics g = e.graphics;
g.drawimage(screen, 0, 0);
g.drawimage(coverlayer, 0, 0);
paintrectangle();
}
protected override void onpaintbackground(painteventargs e) {
}
protected override void onkeydown(keyeventargs e) {
if (e.keycode == keys.escape) {
this.dialogresult = dialogresult.cancel;
}
}
private void paintrectangle() {
using (graphics g = graphics.fromimage(coverlayer)) {
g.clear(covercolor);
graphicspath path = new graphicspath();
path.addrectangle(this.bounds);
path.addrectangle(rectselected);
g.fillpath(rectbrush, path);
g.drawrectangle(pens.blue, rectselected);
}
}
public bitmap resultbitmap {
get { return resultbmp; }
}
}
上面的代码都很容易看明白,这里有一个技巧就是graphicspath,它自动会形成一个中空的区域。上面的实现很容易扩展:多区域截图,多裁判截图等都很容易实现。