当前位置: 首页 > 编程语言 > C#

C#保存打印屏幕分享

时间:2023-04-10 18:21:45 C#

C#保存打印屏幕Bitmapbitmap=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);图形图形=图形。FromImage(位图作为图像);graphics.CopyFromScreen(0,0,0,0,bitmap.Size);bitmap.Save(@"C:Tempprintscreen.jpg",ImageFormat.Jpeg);这是我的打印屏幕按钮代码。问题是我按下按钮几次,它只是写在旧图像文件(printscreen.jpg)上,它不会创建另一个新图像文件,如printscreen1.jpg。尝试这个。它每次都会生成一个唯一的文件。位图bitmap=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);Graphicsgraphics=Graphics.FromImage(位图作为图像);graphics.CopyFromScreen(0,0,0,0,bitmap.Size);bitmap.Save(@"C:Tempprintscreen"+Guid.NewGuid()+".jpg",ImageFormat.Jpeg);或位图bitmap=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);Graphicsgraphics=Graphics.FromImage(位图作为图像);graphics.CopyFromScreen(0,0,0,0,bitmap.Size);vardate=DateTime.Now.ToString("MMddyyHmmss");bitmap.Save(@"C:Tempprintscreen"+date+".jpg",ImageFormat.Jpeg);它不会创建另一个新的图像文件,您不要求它创建代码bitmap.Save(@"C:Tempprintscreen.jpg",ImageFormat.Jpeg);总是写入同一个文件。新图像文件,例如printscreen1.jpg如果您正在创建一个新文件,您需要动态生成文件名。类似于stringfileName=System.IO.Path.GetTempPath()+DateTime.Now.ToString()+".jpg";位图.Save(文件名,ImageFormat.Jpeg);你可以统计以printscreen开头的目录中已经存在的文件数。intcount=Directory.EnumerateFiles(@"C:Temp").Where(x=>x.StartsWith("printscreen")).Count();bitmap.Save(String.Format(@"C:Tempprintscreen{0}.jpg",count),ImageFormat.Jpeg);如果您有很多文件,这可能会更慢,因为它必须遍历它们,但它更加自动化。另一种选择是创建一个文件来存储当前计数器编号。File.Write("Counter.txt",counter.ToString());当你想在程序重启后启动计数器时,你可以这样做。整数计数器=0;if(File.Exists("Counter.txt"))counter=Int32.Parse(File.ReadAllText("Counter.txt"));bitmap.Save(String.Format(@"C:Tempprintscreen{0}.jpg",++counter),ImageFormat.Jpeg);当然,如果您不打算重新启动应用程序并继续执行序列,则可以避免将计数器存储在文本文件中,只需将计数器从零开始递增即可。创建一个字段说计数器并在每个按钮上单击递增它并将其用于文件名-bitmap.Save(@"C:Tempprintscreen"+(counter++)+".jpg",ImageFormat.Jpeg);您也可以尝试这种方式:intnumber;string[]path=Directory.GetFiles(@"C:Temp",@"PrintScreen*");if(path.Length==0){bitmap.Save(@"C:Tempprintscreen.jpg",ImageFormat.Jpeg);返回;}数组排序(路径);Array.Reverse(路径);int.TryParse(Regex.Match(path[0],@"(d+)(?=.jpg)").ToString(),outnumber);bitmap.Save(@"C:Tempprintscreen"+(number+1).ToString()+".jpg",ImageFormat.Jpeg);我认为通过按降序对它们进行排序而不是枚举它们来选择最后一个文件会更有效率。此外,不要跟踪单独的计数器,而是选择最后一次计数并递增它。{//应用程序.DoEvents();位图printscreen=newBitmap(Screen.PrimaryScreen.Bounds.Width,Screen.PrimaryScreen.Bounds.Height);图形graphics=Graphics.FromImage(printscreenasImage);graphics.CopyFromScreen(0,0,0,0,printscreen.Size);字符串路径="";如果(forLog){path=LoggerPath+StudentNumber.ToString()+"\"+SessionID.ToString()+"\";}else{path=PrintScreenPath+StudentNumber.ToString()+"\"+SessionID.ToString()+"\";}if(!Directory.Exists(path))Directory.CreateDirectory(path);字符串文件名=DateTime.Now.Ticks.ToString();SaveJPGWithCompressionSetting(printscreen,path+fileName+".jpeg",17L);printscreen.Dispose();图形处理();返回文件名;}并且这里是SaveJPGWithCompressionSettingmetHodprivatevoidSaveJPGWithCompressionSetting(Imageimage,stringszFileName,longlCompression){try{EncoderParameterseps=newEncoderParameters(1);eps.Param[0]=newEncoderParameter(System.Drawing.Imaging.Encoder.Quality,lCompression);ImageCodecInfoici=GetEncoderInfo("图像/jpeg");image.Save(szFileName,ici,eps);以上就是C#学习教程:在C#中保存打印屏幕分享的内容,如果对你有用,需要了解更多C#学习教程,希望大家多多关注。本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处: