Преглед изворни кода

zhao:优化录制书写卡顿问题

tags/录制修改前
耀 пре 4 година
родитељ
комит
edeef8efd7
2 измењених фајлова са 135 додато и 21 уклоњено
  1. 114
    17
      Common/system/ImageHelper.cs
  2. 21
    4
      XHWK.WKTool/XHMicroLessonSystemWindow.xaml.cs

+ 114
- 17
Common/system/ImageHelper.cs Прегледај датотеку

@@ -392,6 +392,94 @@ namespace Common.system
392 392
         #endregion 图片压缩
393 393
 
394 394
         #region 截屏指定UI控件
395
+        /// <summary>
396
+        /// 保存图片
397
+        /// </summary>
398
+        /// <param name="ui">需要截图的UI控件</param>
399
+        /// <param name="filePathName">图片保存地址 命名 1.png</param>
400
+        /// <param name="width">图片宽</param>
401
+        /// <param name="height">图片高</param>
402
+        /// <param name="ImgWidth">转换后高</param>
403
+        /// <param name="ImgHeight">转换后高</param>
404
+        public static void SaveUI(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
405
+        {
406
+            try
407
+            {
408
+                //在位图中呈现UI元素
409
+                RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Pbgra32);
410
+                bmp.Render(ui);
411
+
412
+                //定义切割矩形
413
+                var cut = new Int32Rect(0, 0, width, height);
414
+                //计算Stride
415
+                var stride = bmp.Format.BitsPerPixel * cut.Width / 8;
416
+                //声明字节数组
417
+                byte[] data = new byte[cut.Height * stride];
418
+
419
+                //调用CopyPixels
420
+                bmp.CopyPixels(cut, data, stride, 0);
421
+                new Thread(new ThreadStart(new Action(() =>
422
+                {
423
+                    BitmapSource bitmapSource = BitmapSource.Create(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Bgr32, null, data, stride);
424
+
425
+                    BitmapEncoder encoder = new PngBitmapEncoder();
426
+                    encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
427
+                    if (ImgWidth > 0)
428
+                    {
429
+                        Bitmap Img = new Bitmap(ImgWidth, ImgHeight);
430
+                        try
431
+                        {
432
+                            //MemoryStream memoryStream = new MemoryStream(data);
433
+                            //Bitmap bit = (Bitmap)Image.FromStream(memoryStream);
434
+
435
+                            MemoryStream memoryStream = new MemoryStream();
436
+                            encoder.Save(memoryStream);
437
+                            Bitmap bit = new Bitmap(memoryStream,true);
438
+                            if (ImgWidth - 2 < bit.Width)
439
+                                {
440
+                                    try
441
+                                    {
442
+                                        Graphics g = Graphics.FromImage(Img);
443
+                                    g.DrawImage(bit, new Rectangle(0, 0, ImgWidth, ImgHeight), new Rectangle(0, 0, bit.Width, bit.Height), GraphicsUnit.Pixel);
444
+                                        g.Dispose();
445
+                                    }
446
+                                    catch
447
+                                    {
448
+                                        Img = bit;
449
+                                    }
450
+                                }
451
+                                else
452
+                                {
453
+                                    Img = bit;
454
+                            }
455
+                            Img.Save(filePathName);
456
+                            memoryStream.Dispose();
457
+                            Img.Dispose();
458
+                            bit.Dispose();
459
+                        }
460
+                        catch (Exception ex)
461
+                        {
462
+                            using (var stream = new FileStream(filePathName, FileMode.Create))
463
+                            {
464
+                                encoder.Save(stream);
465
+                            }
466
+                        }
467
+                    }
468
+                    else
469
+                    {
470
+                        using (var stream = new FileStream(filePathName, FileMode.Create))
471
+                        {
472
+                            encoder.Save(stream);
473
+                        }
474
+                    }
475
+                }))).Start();
476
+            }
477
+            catch (Exception ex)
478
+            {
479
+
480
+            }
481
+        }
482
+
395 483
         /// <summary>
396 484
         /// 保存图片
397 485
         /// </summary>
@@ -403,8 +491,6 @@ namespace Common.system
403 491
         /// <param name="ImgHeight">图片高</param>
404 492
         public static void SaveUIToImage(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
405 493
         {
406
-            //SaveUI(ui, filePathName, width, height, ImgWidth, ImgHeight);
407
-            //return;
408 494
             try
409 495
             {
410 496
                 //在位图中呈现UI元素
@@ -483,7 +569,7 @@ namespace Common.system
483 569
                 //Console.WriteLine(ex.Message);
484 570
             }
485 571
         }
486
-        public static void SaveUI(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
572
+        public static void SaveUI1(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
487 573
         {
488 574
             try
489 575
             {
@@ -491,37 +577,48 @@ namespace Common.system
491 577
                 RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Pbgra32);
492 578
                 bmp.Render(ui);
493 579
 
494
-                BitmapEncoder encoder = new PngBitmapEncoder();
495
-                encoder.Frames.Add(BitmapFrame.Create(bmp));
580
+                //BitmapEncoder encoder = new PngBitmapEncoder();
581
+                //encoder.Frames.Add(BitmapFrame.Create(bmp));
582
+
583
+                //BitmapSource bitmapSource= bmp.Clone();
584
+
585
+
496 586
 
497
-                SaveModel saveModel = new SaveModel();
498
-                saveModel.ImgWidth = ImgWidth;
499
-                saveModel.ImgHeight = ImgHeight;
500
-                saveModel.filePathName = filePathName;
501
-                saveModel.bmp = bmp;
502 587
                 //定义切割矩形
503 588
                 var cut = new Int32Rect(0, 0, width, height);
504
-
505 589
                 //计算Stride
506
-
507 590
                 var stride = bmp.Format.BitsPerPixel * cut.Width / 8;
508
-
509 591
                 //声明字节数组
510
-
511 592
                 byte[] data = new byte[cut.Height * stride];
512 593
 
513 594
                 //调用CopyPixels
514
-
515 595
                 bmp.CopyPixels(cut, data, stride, 0);
596
+                new Thread(new ThreadStart(new Action(() =>
597
+                {
598
+                    BitmapSource bitmapSource = BitmapSource.Create(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Bgr32, null, data, stride);
516 599
 
600
+                    BitmapEncoder encoder = new PngBitmapEncoder();
601
+                    encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
602
+
603
+                    using (var stream = new FileStream(filePathName, FileMode.Create))
604
+                    {
605
+                        encoder.Save(stream);
606
+                    }
607
+                }))).Start();
608
+                //SaveModel saveModel = new SaveModel();
609
+                //saveModel.ImgWidth = ImgWidth;
610
+                //saveModel.ImgHeight = ImgHeight;
611
+                //saveModel.filePathName = filePathName;
612
+                //saveModel.bmp = bmp;
517 613
                 //saveModel.encoder =new PngBitmapEncoder();
518 614
                 //foreach (BitmapFrame eitem in encoder.Frames)
519 615
                 //{
520 616
                 //    saveModel.encoder = new PngBitmapEncoder();
521 617
                 //    saveModel.encoder.Frames.Add( eitem );
522 618
                 //}
523
-                Thread myThread = new Thread(SaveImage);
524
-                myThread.Start(saveModel);
619
+
620
+                //Thread myThread = new Thread(SaveImage);
621
+                //myThread.Start(saveModel);
525 622
             }
526 623
             catch (Exception ex)
527 624
             {

+ 21
- 4
XHWK.WKTool/XHMicroLessonSystemWindow.xaml.cs Прегледај датотеку

@@ -122,9 +122,21 @@ namespace XHWK.WKTool
122 122
             new Aspose.Pdf.License().SetLicense(new MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4xOTA4MjYwODA3NTM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+M2U0NGRlMzAtZmNkMi00MTA2LWIzNWQtNDZjNmEzNzE1ZmMyPC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjAwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT53UGJtNUt3ZTYvRFZXWFNIY1o4d2FiVEFQQXlSR0pEOGI3L00zVkV4YWZpQnd5U2h3YWtrNGI5N2c2eGtnTjhtbUFGY3J0c0cwd1ZDcnp6MytVYk9iQjRYUndTZWxsTFdXeXNDL0haTDNpN01SMC9jZUFxaVZFOU0rWndOQkR4RnlRbE9uYTFQajhQMzhzR1grQ3ZsemJLZFZPZXk1S3A2dDN5c0dqYWtaL1E9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=")));
123 123
             new Aspose.Slides.License().SetLicense(new MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4xOTA4MjYwODA3NTM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+M2U0NGRlMzAtZmNkMi00MTA2LWIzNWQtNDZjNmEzNzE1ZmMyPC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjAwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT53UGJtNUt3ZTYvRFZXWFNIY1o4d2FiVEFQQXlSR0pEOGI3L00zVkV4YWZpQnd5U2h3YWtrNGI5N2c2eGtnTjhtbUFGY3J0c0cwd1ZDcnp6MytVYk9iQjRYUndTZWxsTFdXeXNDL0haTDNpN01SMC9jZUFxaVZFOU0rWndOQkR4RnlRbE9uYTFQajhQMzhzR1grQ3ZsemJLZFZPZXk1S3A2dDN5c0dqYWtaL1E9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=")));
124 124
             InitializeComponent();
125
+
126
+            float WorkW = PrimaryScreen.ScaleWorkingAreaSize.Width;
127
+            float WorkH = PrimaryScreen.ScaleWorkingAreaSize.Height;
125 128
             double proportion = 1036.0 / 1290.0;
126
-            Width = (PrimaryScreen.ScaleWorkingAreaSize.Height) / proportion - (BorderThickness.Left + BorderThickness.Right);
127
-            Height = PrimaryScreen.ScaleWorkingAreaSize.Height - (BorderThickness.Top + BorderThickness.Bottom);
129
+            //限制大小 确保大屏幕下录制不会卡死  暂时
130
+            if (WorkW > 1920.0)
131
+            {
132
+                Width = 1920;
133
+                Height = 1036;
134
+            }
135
+            else
136
+            {
137
+                Width = (PrimaryScreen.ScaleWorkingAreaSize.Height) / proportion - (BorderThickness.Left + BorderThickness.Right);
138
+                Height = PrimaryScreen.ScaleWorkingAreaSize.Height - (BorderThickness.Top + BorderThickness.Bottom);
139
+            }
128 140
             GridContent.Width = Width - 14;
129 141
             GridContent.Height = Height;
130 142
 
@@ -3141,7 +3153,8 @@ namespace XHWK.WKTool
3141 3153
                     Dispatcher.Invoke(() =>
3142 3154
                     {
3143 3155
                         //保存图片
3144
-                        ImageHelper.SaveUIToImage(GridMain, FilePathName, gridActWidth, gridActHeight, (int)RsW, (int)RsH);
3156
+                        ImageHelper.SaveUI(GridMain, FilePathName, gridActWidth, gridActHeight, (int)RsW, (int)RsH);
3157
+                        //ImageHelper.SaveUIToImage(GridMain, FilePathName, gridActWidth, gridActHeight, (int)RsW, (int)RsH);
3145 3158
                         //ImageHelper.SaveUIToImage(GridMain, FilePathName, (int)(GridMain.ActualWidth * (PrimaryScreen.DpiX / 96f)), (int)(GridMain.ActualHeight * (PrimaryScreen.DpiY / 96f)), 0, 0);
3146 3159
                         //ImageHelper.SaveUIToImage(GridMain, FilePathName, (int)(ActualWidth * (PrimaryScreen.DpiX / 96f)), (int)(ActualHeight * (PrimaryScreen.DpiY / 96f)), (int)RsW, (int)RsH);
3147 3160
                         //ImageHelper.SaveUIToImage(GridMain, FilePathName, (int)(ActualWidth * (PrimaryScreen.DpiX / 96f)), (int)(ActualHeight * (PrimaryScreen.DpiY / 96f)), 1281, 1026);
@@ -4982,7 +4995,11 @@ namespace XHWK.WKTool
4982 4995
                 HideAngleBorder();
4983 4996
             }
4984 4997
         }
4985
-
4998
+        /// <summary>
4999
+        /// 加载完成后调整录制区域大小
5000
+        /// </summary>
5001
+        /// <param name="sender"></param>
5002
+        /// <param name="e"></param>
4986 5003
         private void Window_Loaded(object sender, RoutedEventArgs e)
4987 5004
         {
4988 5005
             //调整录制区域宽高

Loading…
Откажи
Сачувај