|
@@ -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
|
{
|