关闭

[C#]全屏拷贝代码

1077人阅读 评论(2) 收藏 举报

 

要先声明下:这段代码是我转贴过来的。本来作为资料,但是居然还是可以搜索到。CSDN的blog看来还是有bug啊。另外,我将文章类型改为“转贴”,但是似乎文章的头上依然顶着个“原”字。让人也无可奈何。

[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
        
public static extern bool BitBlt(
         IntPtr hdcDest, 
//目标设备的句柄 
         int nXDest, // 目标对象的左上角的X坐标 
         int nYDest, // 目标对象的左上角的X坐标 
         int nWidth, // 目标对象的矩形的宽度 
         int nHeight, // 目标对象的矩形的长度 
         IntPtr hdcSrc, // 源设备的句柄 
         int nXSrc, // 源对象的左上角的X坐标 
         int nYSrc, // 源对象的左上角的X坐标 
         System.Int32 dwRop // 光栅的操作值 
         );

        
public  Bitmap windowFullScreen()//全屏截图程序
        {
            
//建立屏幕Graphics
            Graphics grpScreen = Graphics.FromHwnd(IntPtr.Zero);
            
//根据屏幕大小建立位图
            Bitmap bitmap = new Bitmap(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height, grpScreen);
            
//建立位图相关Graphics
            Graphics grpBitmap = Graphics.FromImage(bitmap);
            
//建立屏幕上下文
            IntPtr hdcScreen = grpScreen.GetHdc();
            
//建立位图上下文
            IntPtr hdcBitmap = grpBitmap.GetHdc();
            
//将屏幕捕获保存在图位中
            BitBlt(hdcBitmap, 00, bitmap.Width, bitmap.Height, hdcScreen, 000x00CC0020);
            
//关闭位图句柄
            grpBitmap.ReleaseHdc(hdcBitmap);
            
//关闭屏幕句柄
            grpScreen.ReleaseHdc(hdcScreen);
            
//释放位图对像
            grpBitmap.Dispose();
            
//释放屏幕对像
            grpScreen.Dispose();

            
//返回捕获位图
            return bitmap;
        }


        
public void save(Image image)//保存文件程序
        {
            
            
bool isSave = true;
            SaveFileDialog saveImageDialog 
= new SaveFileDialog();
            saveImageDialog.Title 
= "Capture screen image save dialog";
            saveImageDialog.Filter 
= @"jpeg|*.jpg|bmp|*.bmp|gif|*.gif";

            
if (saveImageDialog.ShowDialog() == DialogResult.OK)
            
{
                
string fileName = saveImageDialog.FileName.ToString();

                
if (fileName != "" && fileName != null)
                
{
                    
string fileExtName = fileName.Substring(fileName.LastIndexOf("."+ 1).ToString();

                    System.Drawing.Imaging.ImageFormat imgformat 
= null;

                    
if (fileExtName != "")
                    
{
                        
switch (fileExtName)
                        
{
                            
case "jpg":
                                imgformat 
= System.Drawing.Imaging.ImageFormat.Jpeg;
                                
break;
                            
case "bmp":
                                imgformat 
= System.Drawing.Imaging.ImageFormat.Bmp;
                                
break;
                            
case "gif":
                                imgformat 
= System.Drawing.Imaging.ImageFormat.Gif;
                                
break;
                            
default:
                                MessageBox.Show(
"只能存取为: jpg,bmp,gif 格式");
                                isSave 
= false;
                                
break;
                        }


                    }


                    
//默认保存为JPG格式
                    if (imgformat == null)
                    
{
                        imgformat 
= System.Drawing.Imaging.ImageFormat.Jpeg;
                    }


                    
if (isSave)
                    
{
                        
try
                        
{
                            image.Save(fileName, imgformat);
                            MessageBox.Show(
"图片已经成功保存!~~");
                        }

                        
catch
                        
{
                            MessageBox.Show(
"保存失败,你还没有截取过图片或已经清空图片!");
                        }

                    }


                }


            }

//            return image;
        }
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10139次
    • 积分:168
    • 等级:
    • 排名:千里之外
    • 原创:8篇
    • 转载:1篇
    • 译文:0篇
    • 评论:2条
    文章分类
    文章存档