符合中小企业对网站设计、功能常规化式的企业展示型网站建设
本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...
商城网站建设因基本功能的需求不同费用上面也有很大的差别...
手机微信网站开发、微信官网、微信商城网站...
主要是利用java的几个先有的函数,如Robot这个类的一个方法createScreenCapture一个获得一个任意大小的屏幕图像(在这里是全屏图像),而所谓的截图就是在这个图像上画出一个矩形,再利用上面的方法获得这部分的图像,程序中的cf.setAlwaysOnTop(true)是必需的;看起来是在屏幕上截图,其实态枝和只是在一个在一个内镶有桌面背景的JFrame中截图。不知道还有没有其他的好方法~
成都做网站、成都网站建设的关注点不是能为您做些什么网站,而是怎么做网站,有没有做好网站,给创新互联公司一个展示的机会来证明自己,这并不会花费您太多时间,或许会给您带来新的灵感和惊喜。面向用户友好,注重用户体验,一切以用户为中心。
附上代码:
import java.awt.*;
import java.awt.image.*;
import java.awt.event.*;
import java.io.File;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Camera {
/**
* @param args
*/
public static void main(String[] args) {
CameraJFrame cf=new CameraJFrame();
cf.setAlwaysOnTop(true);
cf.setUndecorated(true);
cf.setVisible(true);
}
}
class CameraJFrame extends JFrame
{
/**
*
*/搭段
private static final long serialVersionUID = 1L;
Dimension di=Toolkit.getDefaultToolkit().getScreenSize();
public CameraJFrame()
{
setSize(di);
getContentPane().add(new CameraJPanel());
}
class CameraJPanel extends JPanel implements MouseListener,MouseMotionListener
{
/**
* flag主要是用来判别状态。
* 文件的格式名帆盯是unname+数字编号,格式是png
*/
private static final long serialVersionUID = 1L;
BufferedImage bi,get;
int startx,starty,endx,endy;
int flag=1;
String filename="unname";
String fileformat="png";
int count=1;
public CameraJPanel()
{
try
{
Robot ro=new Robot();
bi=ro.createScreenCapture(new Rectangle(0,0,di.width,di.height));
}
catch(Exception e)
{
e.printStackTrace();
}
addMouseListener(this);
addMouseMotionListener(this);
}
public void paintComponent(Graphics g)
{
g.drawImage(bi,0,0,di.width,di.height,this);
g.setColor(Color.red);
g.drawRect(startx, starty, endx-startx, endy-starty);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
if(e.getButton()==MouseEvent.BUTTON3)
{
System.exit(0);
}
else if(e.getClickCount()==2)
{
try
{
Robot ro=new Robot();
get=ro.createScreenCapture(new Rectangle(startx,starty,endx-startx,endy-starty));
String name=filename+String.valueOf(count++)+"."+fileformat;
File f=new File(name);
ImageIO.write(get, fileformat, f);
}
catch(Exception ex)
{
ex.printStackTrace();
}
flag=1; //置flag为1,以便重新开始截图。
startx=starty=endx=endy=0;
repaint();
}
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
if(flag==1)
{
startx=e.getX();
starty=e.getY();
}
}
public void mouseReleased(MouseEvent e) {
flag=0;
}
public void mouseDragged(MouseEvent e) {
flag=1;
endx=e.getX();
endy=e.getY();
repaint();
}
public void mouseMoved(MouseEvent e) {}
}
}
import java.awt.AWTException;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.filechooser.FileSystemView;
/**
* java截屏
* 运行誉稿后将当前屏幕截取,并最大化显示。
* 拖拽鼠标,选择自己需要的部分。
* 按Esc键保存图片到桌面,并退出程序。
* 点击右上角(没有可见的按钮),退出程序,不保存图片。扮毁
*
* @author JinCeon
*/
public class SnapshotTest {
public static void main(String[] args) {
// 全屏运行
RectD rd = new RectD();
GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice();
gd.setFullScreenWindow(rd);
}
}
class RectD extends JFrame {
private static final long serialVersionUID = 1L;
int orgx, orgy, endx, endy;
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
BufferedImage image;
BufferedImage tempImage;
BufferedImage saveImage;
Graphics g;
@Override
public void paint(Graphics g) {
RescaleOp ro = new RescaleOp(0.8f, 0, null);
tempImage = ro.filter(image, null);
g.drawImage(tempImage, 0, 0, this);
}
public RectD() {
snapshot();
setVisible(true);
// setSize(d);//庆缺孝最大化窗口
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
orgx = e.getX();
orgy = e.getY();
}
});
this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
endx = e.getX();
endy = e.getY();
g = getGraphics();
g.drawImage(tempImage, 0, 0, RectD.this);
int x = Math.min(orgx, endx);
int y = Math.min(orgy, endy);
int width = Math.abs(endx - orgx)+1;
int height = Math.abs(endy - orgy)+1;
// 加上1,防止width或height为0
g.setColor(Color.BLUE);
g.drawRect(x-1, y-1, width+1, height+1);
//减1,加1都是为了防止图片将矩形框覆盖掉
saveImage = image.getSubimage(x, y, width, height);
g.drawImage(saveImage, x, y, RectD.this);
}
});
this.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
// 按Esc键退出
if (e.getKeyCode() == 27) {
saveToFile();
System.exit(0);
}
}
});
}
public void saveToFile() {
SimpleDateFormat sdf = new SimpleDateFormat("yyyymmddHHmmss");
String name = sdf.format(new Date());
File path = FileSystemView.getFileSystemView().getHomeDirectory();
String format = "jpg";
File f = new File(path + File.separator + name + "." + format);
try {
ImageIO.write(saveImage, format, f);
} catch (IOException e) {
e.printStackTrace();
}
}
public void snapshot() {
try {
Robot robot = new Robot();
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
image = robot.createScreenCapture(new Rectangle(0, 0, d.width,
d.height));
} catch (AWTException e) {
e.printStackTrace();
}
}
}
哎~要是你给我个分就好了!我穗散前几天刚刚做的!你拿去吧!
/*
作者:泡沫
地址:
功能:用于掘世截取图片,方判族肢便快捷!
mail:yuhuidog#163点抗 (注意:其中#为@)
*/
import java.awt.AWTException;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Panel;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class AWTpicture extends Frame implements MouseListener,MouseMotionListener,ActionListener{
private int firstX,firstY,frameWidth,frameHeight;
private int firstWith,firstHeight,firstPointx,firstPointy;
private BufferedImage bi,sbi,original;
private Robot robot;
private Rectangle rectangle;
private Rectangle rectangleCursor,rectangleCursorUp,rectangleCursorDown,rectangleCursorLeft,rectangleCursorRight;
private Rectangle rectangleCursorRU,rectangleCursorRD,rectangleCursorLU,rectangleCursorLD;
private Image bis;
private Dimension dimension;
private Button button,button2,clearButton;
private Point[] point=new Point[3];
private int width,height;
private int nPoints=5;
private Panel panel;
private boolean drawHasFinish=false,change=false;
private int changeFirstPointX,changeFirstPointY,changeWidth,changeHeight;
private boolean changeUP=false,changeDOWN=false,changeLEFT=false,changeRIGHT=false,changeRU=false,changeRD=false,changeLU=false,changeLD=false;
private boolean clearPicture=false,redraw=false;
private FileDialog fileDialog;
private AWTpicture(){
//取得屏幕大小
dimension=Toolkit.getDefaultToolkit().getScreenSize();
frameWidth=dimension.width;
frameHeight=dimension.height;
fileDialog=new FileDialog(this,"泡沫截图",FileDialog.SAVE);
rectangle=new Rectangle(frameWidth,frameHeight);
panel=new Panel();
button=new Button("退出");
button.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
button.setBackground(Color.green);
button2=new Button("截取");
button2.setBackground(Color.darkGray);
button2.addActionListener(new MyTakePicture(this));
button2.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
button.addActionListener(this);
clearButton=new Button("重绘");
clearButton.setBackground(Color.green);
clearButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
clearButton.addActionListener(new MyClearPicture(this));
panel.setLayout(new BorderLayout());
panel.add(clearButton, BorderLayout.SOUTH);
panel.add(button, BorderLayout.NORTH);
panel.add(button2, BorderLayout.CENTER);
try {
robot=new Robot();
} catch (AWTException e) {
e.printStackTrace();
}
//截取全屏
bi=robot.createScreenCapture(rectangle);
original=bi;
this.setSize(frameWidth,frameHeight);
this.setUndecorated(true);
this.addMouseListener(this);
this.addMouseMotionListener(this);
this.add(panel,BorderLayout.EAST);
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
this.setVisible(true);
this.repaint();
}
public static void main(String[] args){
new AWTpicture();
}
public void paint(Graphics g) {
this.drawR(g);
}
//缓存图片
public void update(Graphics g){
if(bis==null){
bis=this.createImage(frameWidth, frameHeight);
}
Graphics ga=bis.getGraphics();
Color c=ga.getColor();
ga.setColor(Color.black);
ga.fillRect(0, 0, frameWidth, frameHeight);
ga.setColor(c);
paint(ga);
g.drawImage(bis, 0, 0, frameWidth, frameHeight, null);
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseReleased(MouseEvent e) {
if(!drawHasFinish){
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[2].x;
firstPointy=point[2].y;
}
changeFirstPointX=firstPointx;
changeFirstPointY=firstPointy;
if(point[1]!=null point[2]!=null ){
rectangleCursorUp=new Rectangle(firstPointx+20,firstPointy-10,width-40,20);
rectangleCursorDown=new Rectangle(firstPointx+20,firstPointy+height-10,width-40,20);
rectangleCursorLeft=new Rectangle(firstPointx-10,firstPointy+10,20,height-20);
rectangleCursorRight=new Rectangle(firstPointx+width-10,firstPointy+10,20,height-20);
rectangleCursorLU=new Rectangle(firstPointx-10,firstPointy-10,30,20);
rectangleCursorLD=new Rectangle(firstPointx-10,firstPointy+height-10,30,20);
rectangleCursorRU=new Rectangle(firstPointx+width-10,firstPointy-10,20,20);
rectangleCursorRD=new Rectangle(firstPointx+width-10,firstPointy+height-10,20,20);
drawHasFinish=true;
}
}
//确定每边能改变大小的矩形
if(drawHasFinish){
rectangleCursorUp=new Rectangle(changeFirstPointX+20,changeFirstPointY-10,changeWidth-40,20);
rectangleCursorDown=new Rectangle(changeFirstPointX+20,changeFirstPointY+changeHeight-10,changeWidth-40,20);
rectangleCursorLeft=new Rectangle(changeFirstPointX-10,changeFirstPointY+10,20,changeHeight-20);
rectangleCursorRight=new Rectangle(changeFirstPointX+changeWidth-10,changeFirstPointY+10,20,changeHeight-20);
rectangleCursorLU=new Rectangle(changeFirstPointX-2,changeFirstPointY-2,10,10);
rectangleCursorLD=new Rectangle(changeFirstPointX-2,changeFirstPointY+changeHeight-2,10,10);
rectangleCursorRU=new Rectangle(changeFirstPointX+changeWidth-2,changeFirstPointY-2,10,10);
rectangleCursorRD=new Rectangle(changeFirstPointX+changeWidth-2,changeFirstPointY+changeHeight-2,10,10);
}
}
public void mouseDragged(MouseEvent e) {
point[2]=e.getPoint();
//if(!drawHasFinish){
this.repaint();
// }
//托动鼠标移动大小
if(change){
if(changeUP){
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointY=e.getPoint().y;
}
if(changeDOWN){
changeHeight=e.getPoint().y-changeFirstPointY;
}
if(changeLEFT){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeFirstPointX=e.getPoint().x;
}
if(changeRIGHT){
changeWidth=e.getPoint().x-changeFirstPointX;
}
if(changeLU){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointX=e.getPoint().x;
changeFirstPointY=e.getPoint().y;
}
if(changeLD){
changeWidth=changeWidth+changeFirstPointX-e.getPoint().x;
changeHeight=e.getPoint().y-changeFirstPointY;
changeFirstPointX=e.getPoint().x;
}
if(changeRU){
changeWidth=e.getPoint().x-changeFirstPointX;
changeHeight=changeHeight+changeFirstPointY-e.getPoint().y;
changeFirstPointY=e.getPoint().y;
}
if(changeRD){
changeWidth=e.getPoint().x-changeFirstPointX;
changeHeight=e.getPoint().y-changeFirstPointY;
}
this.repaint();
}
}
public void mouseMoved(MouseEvent e) {
point[1]=e.getPoint();
//改变鼠标的形状
if(rectangleCursorUp!=null rectangleCursorUp.contains(point[1])){
this.setCursor(new Cursor(Cursor.N_RESIZE_CURSOR));
change=true;
changeUP=true;
}else if(rectangleCursorDown!=null rectangleCursorDown.contains(point[1])){
this.setCursor(new Cursor(Cursor.S_RESIZE_CURSOR));
change=true;
changeDOWN=true;
}else if(rectangleCursorLeft!=null rectangleCursorLeft.contains(point[1])){
this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
change=true;
changeLEFT=true;
}else if(rectangleCursorRight!=null rectangleCursorRight.contains(point[1]) ){
this.setCursor(new Cursor(Cursor.W_RESIZE_CURSOR));
change=true;
changeRIGHT=true;
}else if(rectangleCursorLU !=null rectangleCursorLU.contains(point[1])){
this.setCursor(new Cursor(Cursor.NW_RESIZE_CURSOR));
change=true;
changeLU=true;
}else if(rectangleCursorLD !=null rectangleCursorLD.contains(point[1])){
this.setCursor(new Cursor(Cursor.SW_RESIZE_CURSOR));
change=true;
changeLD=true;
}else if(rectangleCursorRU!=null rectangleCursorRU.contains(point[1])){
this.setCursor(new Cursor(Cursor.NE_RESIZE_CURSOR));
change=true;
changeRU=true;
}else if(rectangleCursorRD!=null rectangleCursorRD.contains(point[1])){
this.setCursor(new Cursor(Cursor.SE_RESIZE_CURSOR));
change=true;
changeRD=true;
}else{
this.setCursor(new Cursor(Cursor.CROSSHAIR_CURSOR));
changeUP=false;changeDOWN=false;changeRIGHT=false;changeLEFT=false;changeRU=false;
changeRD=false;changeLU=false;changeLD=false;
}
redraw=false;
}
@Override
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
class MyTakePicture implements ActionListener{
AWTpicture aWTpicture;
MyTakePicture(AWTpicture aWTpicture){
this.aWTpicture=aWTpicture;
}
//保存图片
public void actionPerformed(ActionEvent e) {
fileDialog.setVisible(true);
if(changeWidth0){
sbi=bi.getSubimage(changeFirstPointX,changeFirstPointY,changeWidth,changeHeight);
File file=new File(fileDialog.getDirectory());
file.mkdir();
try {
ImageIO.write(sbi, "jpeg",new File(file,fileDialog.getFile()+".jpg") );
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
class MyClearPicture implements ActionListener{
AWTpicture aWTpicture;
MyClearPicture(AWTpicture aWTpicture){
this.aWTpicture=aWTpicture;
}
public void actionPerformed(ActionEvent e) {
drawHasFinish=false;
change=false;
redraw=true;
rectangleCursorUp=null;
rectangleCursorDown=null;
rectangleCursorLeft=null;
rectangleCursorRight=null;
rectangleCursorRU=null;
rectangleCursorRD=null;
rectangleCursorLU=null;
rectangleCursorLD=null;
changeWidth=0;
changeHeight=0;
aWTpicture.repaint();
}
}
public void drawR(Graphics g){
g.drawImage(bi, 0,0,frameWidth,frameHeight, null);
if(point[1]!=null point[2]!=null !drawHasFinish !redraw){
int[] xPoints={point[1].x,point[2].x,point[2].x,point[1].x,point[1].x};
int[] yPoints={point[1].y,point[1].y,point[2].y,point[2].y,point[1].y};
width=(point[2].x-point[1].x)0?(point[2].x-point[1].x):(point[1].x-point[2].x);
height=(point[2].y-point[1].y)0?(point[2].y-point[1].y):(point[1].y-point[2].y);
changeWidth=width;
changeHeight=height;
Color c=g.getColor();
g.setColor(Color.red);
g.drawString(width+"*"+height, point[1].x, point[1].y-5);
//画点
/*int i;
if()*/
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[2].x;
firstPointy=point[1].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[1].x;
firstPointy=point[2].y;
}
if(point[1].xpoint[2].x point[1].ypoint[2].y){
firstPointx=point[2].x;
firstPointy=point[2].y;
}
g.fillRect(firstPointx-2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+(width)/2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+width-2,firstPointy-2 , 5,5);
g.fillRect(firstPointx+width-2,firstPointy+ height/2-2, 5,5);
g.fillRect(firstPointx+width-2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx+(width)/2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx-2,firstPointy+height-2, 5,5);
g.fillRect(firstPointx-2,firstPointy+ height/2-2, 5,5);
//画矩形
//g.drawString("fafda", point[1].x-100, point[1].y-5);
g.drawPolyline(xPoints, yPoints, nPoints);
}
if(change){
g.setColor(Color.red);
g.drawString(changeWidth+"*"+changeHeight, changeFirstPointX, changeFirstPointY-5);
g.fillRect(changeFirstPointX-2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+(changeWidth)/2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY-2 , 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY+ changeHeight/2-2, 5,5);
g.fillRect(changeFirstPointX+changeWidth-2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX+(changeWidth)/2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX-2,changeFirstPointY+changeHeight-2, 5,5);
g.fillRect(changeFirstPointX-2,changeFirstPointY+ changeHeight/2-2, 5,5);
g.drawRect(changeFirstPointX, changeFirstPointY, changeWidth, changeHeight);
}
}
}
QQ截图的核心其实就是调用WINDOWS API函数,主要涉及两个核心组件, user32.dll和gdi32.dll。
如下是,C#代码调用上述两个核心组件的完整示例:
namespace WindowsFormsApplication1
{
/// summary
/// 屏幕捕获类
/// /summary
public class ScreenCapture
{
/// summary
/// 创建一个包含整个桌面的截图Image对象(捕获到的桌面是当前WINDOWS操作系统活动桌面)
/// /summary
/// returns/returns
public Image CaptureScreen()
{
return CaptureWindow(User32.GetDesktopWindow());
}
/// summary
/// 创建一个包含特定窗口的截图Image对象
改茄 /// /summary
/// param name="handle"启动本程序的句柄窗口(在Windows上这是由Handle属性获得)/param
/// returns/returns
public Image CaptureWindow(IntPtr handle)
{
// 获取目标窗口的HDC
IntPtr hdcSrc = User32.GetWindowDC(handle);
// 获取它的大小
User32.RECT windowRect = new User32.RECT();
User32.GetWindowRect(handle, ref windowRect);
int width = windowRect.right - windowRect.left;
int height = windowRect.bottom - windowRect.top;
// 创建设备上下文对象
IntPtr hdcDest = GDI32.CreateCompatibleDC(hdcSrc);
橘歼迟 IntPtr hBitmap = GDI32.CreateCompatibleBitmap(hdcSrc, width, height);
IntPtr hOld = GDI32.SelectObject(hdcDest, hBitmap);
GDI32.BitBlt(hdcDest, 0, 0, width, height, hdcSrc, 0, 0, GDI32.SRCCOPY);
GDI32.SelectObject(hdcDest, hOld);
GDI32.DeleteDC(hdcDest);
User32.ReleaseDC(handle, hdcSrc);
圆李 Image img = Image.FromHbitmap(hBitmap);
GDI32.DeleteObject(hBitmap);
return img;
}
/// summary
/// 获取特定窗口,并保存它
/// /summary
/// param name="handle"/param
/// param name="filename"/param
/// param name="format"/param
public void CaptureWindowToFile(IntPtr handle, string filename, ImageFormat format)
{
Image img = CaptureWindow(handle);
img.Save(filename, format);
}
/// summary
/// 捕获整个windows活动窗口并保存它
/// /summary
/// param name="filename"/param
/// param name="format"/param
public void CaptureScreenToFile(string filename, ImageFormat format)
{
Image img = CaptureScreen();
img.Save(filename, format);
}
/// summary
/// GDI32 相关的API函数
/// /summary
private class GDI32
{
public const int SRCCOPY = 0x00CC0020;
[DllImport("gdi32.dll")]
public static extern bool BitBlt(IntPtr hObject, int nXDest, int nYDest,
int nWidth, int nHeight, IntPtr hObjectSource,
int nXSrc, int nYSrc, int dwRop);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleBitmap(IntPtr hDC, int nWidth,
int nHeight);
[DllImport("gdi32.dll")]
public static extern IntPtr CreateCompatibleDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteDC(IntPtr hDC);
[DllImport("gdi32.dll")]
public static extern bool DeleteObject(IntPtr hObject);
[DllImport("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hDC, IntPtr hObject);
}
/// summary
/// User32 API相关函数
/// /summary
private class User32
{
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
[DllImport("user32.dll")]
public static extern IntPtr GetWindowDC(IntPtr hWnd);
[DllImport("user32.dll")]
public static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDC);
[DllImport("user32.dll")]
public static extern IntPtr GetWindowRect(IntPtr hWnd, ref RECT rect);
}
}
}
// 调用示例:
private void button1_Click(object sender, EventArgs e)
{
ScreenCapture sc = new ScreenCapture();
// 捕获整个屏幕并保存到一个文件里
Image img = sc.CaptureScreen();
// 将捕获的图片显示在图片控件里
this.pictureBox1.Image = img;
// 捕获当前运行窗体并保存在C盘,文件名和后缀为temp.png
sc.CaptureWindowToFile(this.Handle, "C:\\temp.png", ImageFormat.Gif);
}