c# - CopyFromScreen 是获取屏幕截图的正确方法吗?

标签 c# .net winforms screenshot

我正在创建一个在特定时间间隔截取桌面屏幕截图的应用。

代码:

String nme = "";
Bitmap printscreen = new Bitmap(Screen.PrimaryScreen.Bounds.Width, 
                                Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(printscreen as Image);
graphics.CopyFromScreen(0, 0, 0, 0, printscreen.Size);
nme = DateTime.Now.ToString();
printscreen.Save(@"F:\Temp\printScre.jpg", ImageFormat.Jpeg);
  1. CopyFromScreen 是获取桌面屏幕截图的正确方法吗?
  2. 如果我想将此屏幕截图发送到远程服务器,那么最好的方法是什么?
  3. 我应该在发送之前做一些压缩吗?如果是的话,最有效的方法是什么?

最佳答案

  1. 是的,这是正确的方式
  2. 有几种可能性。我建议通过电子邮件或 FTP 发送它(因为它在 C# 中很简单)。
  3. 不,我个人认为您不需要额外的压缩。您的屏幕截图已保存为 JPEG,因此它们已被压缩。

用于发送带附件的电子邮件的代码段:

MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient(YOUR SMTP SERVER ADDRESS);
mail.From = new MailAddress(SENDER ADDRESS);
mail.To.Add(RECEIVER ADDRESS);
mail.Subject = "Test Mail - 1";
mail.Body = "mail with attachment";
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment("YOURFILE");
mail.Attachments.Add(attachment);
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(YOUR_SMTP_USER_NAME, YOUR_SMTP_PASSWORD);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail); 

(基于 this 博客)

FTP 的代码片段:

    FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://www.contoso.com/test.htm");
    request.Method = WebRequestMethods.Ftp.UploadFile;
    request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
    StreamReader sourceStream = new StreamReader("testfile.txt");
    byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());
    sourceStream.Close();
    request.ContentLength = fileContents.Length;
    Stream requestStream = request.GetRequestStream();
    requestStream.Write(fileContents, 0, fileContents.Length);
    requestStream.Close();
    FtpWebResponse response = (FtpWebResponse)request.GetResponse();    
    Console.WriteLine("Upload File Complete, status {0}", response.StatusDescription);    
    response.Close();

(基于 MSDN )

关于c# - CopyFromScreen 是获取屏幕截图的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19353619/

相关文章:

c# - 由于异步回调,在(不存在的)文本框中附加文本时出错

c# - 如果枚举用作方法参数,是否始终需要在 C# 中进行强制转换?

c# - CRM 2011,解决方案和发布者选项不可用

c# - 显示隐藏表格

.net - 任务管理器不同意 Process Explorer?

c# - Windows形成不同的类,试图改变textbox.text

.net - .net 的桌面多线程 UI 示例应用程序

c# - 同一个盒子上的进程间通信 - 2 个应用程序或进程之间的通信

c# - Vector3.Set 和 Rigidbody2D.velocity.Set 和功能无法正常工作

c# - 如何最好地处理包含 0 到 3 个参数/过滤器但使用单个 LinQ to Entities 表达式的搜索查询