|
@@ -403,6 +403,8 @@ namespace Common.system
|
403
|
403
|
/// <param name="ImgHeight">图片高</param>
|
404
|
404
|
public static void SaveUIToImage(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
|
405
|
405
|
{
|
|
406
|
+ //SaveUI(ui, filePathName, width, height, ImgWidth, ImgHeight);
|
|
407
|
+ //return;
|
406
|
408
|
try
|
407
|
409
|
{
|
408
|
410
|
//在位图中呈现UI元素
|
|
@@ -481,8 +483,209 @@ namespace Common.system
|
481
|
483
|
//Console.WriteLine(ex.Message);
|
482
|
484
|
}
|
483
|
485
|
}
|
|
486
|
+ public static void SaveUI(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
|
|
487
|
+ {
|
|
488
|
+ try
|
|
489
|
+ {
|
|
490
|
+ //在位图中呈现UI元素
|
|
491
|
+ RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Pbgra32);
|
|
492
|
+ bmp.Render(ui);
|
|
493
|
+
|
|
494
|
+ BitmapEncoder encoder = new PngBitmapEncoder();
|
|
495
|
+ encoder.Frames.Add(BitmapFrame.Create(bmp));
|
|
496
|
+
|
|
497
|
+ SaveModel saveModel = new SaveModel();
|
|
498
|
+ saveModel.ImgWidth = ImgWidth;
|
|
499
|
+ saveModel.ImgHeight = ImgHeight;
|
|
500
|
+ saveModel.filePathName = filePathName;
|
|
501
|
+ saveModel.bmp = bmp;
|
|
502
|
+ //定义切割矩形
|
|
503
|
+ var cut = new Int32Rect(0, 0, width, height);
|
|
504
|
+
|
|
505
|
+ //计算Stride
|
|
506
|
+
|
|
507
|
+ var stride = bmp.Format.BitsPerPixel * cut.Width / 8;
|
484
|
508
|
|
485
|
|
- private static void SaveImage(object obj)
|
|
509
|
+ //声明字节数组
|
|
510
|
+
|
|
511
|
+ byte[] data = new byte[cut.Height * stride];
|
|
512
|
+
|
|
513
|
+ //调用CopyPixels
|
|
514
|
+
|
|
515
|
+ bmp.CopyPixels(cut, data, stride, 0);
|
|
516
|
+
|
|
517
|
+ //saveModel.encoder =new PngBitmapEncoder();
|
|
518
|
+ //foreach (BitmapFrame eitem in encoder.Frames)
|
|
519
|
+ //{
|
|
520
|
+ // saveModel.encoder = new PngBitmapEncoder();
|
|
521
|
+ // saveModel.encoder.Frames.Add( eitem );
|
|
522
|
+ //}
|
|
523
|
+ Thread myThread = new Thread(SaveImage);
|
|
524
|
+ myThread.Start(saveModel);
|
|
525
|
+ }
|
|
526
|
+ catch (Exception ex)
|
|
527
|
+ {
|
|
528
|
+
|
|
529
|
+ }
|
|
530
|
+
|
|
531
|
+ }
|
|
532
|
+ public static void SaveImage(object model)
|
|
533
|
+ {
|
|
534
|
+ try
|
|
535
|
+ {
|
|
536
|
+ SaveModel saveModel = (SaveModel)model;
|
|
537
|
+ int ImgWidth = saveModel.ImgWidth;
|
|
538
|
+ int ImgHeight = saveModel.ImgHeight;
|
|
539
|
+ //BitmapEncoder encoder = saveModel.encoder;
|
|
540
|
+ RenderTargetBitmap bmp = saveModel.bmp;
|
|
541
|
+ BitmapEncoder encoder = new PngBitmapEncoder();
|
|
542
|
+ encoder.Frames.Add(BitmapFrame.Create(bmp));
|
|
543
|
+ string filePathName = saveModel.filePathName;
|
|
544
|
+ if (ImgWidth > 0)
|
|
545
|
+ {
|
|
546
|
+ Bitmap Img = new Bitmap(ImgWidth, ImgHeight);
|
|
547
|
+ try
|
|
548
|
+ {
|
|
549
|
+ MemoryStream memoryStream = new MemoryStream();
|
|
550
|
+ encoder.Save(memoryStream);
|
|
551
|
+ new Thread(new ThreadStart(new Action(() =>
|
|
552
|
+ {
|
|
553
|
+ //System.Drawing.Image img = System.Drawing.Image.FromStream(memoryStream);
|
|
554
|
+ Bitmap bit = new Bitmap(memoryStream);
|
|
555
|
+ if (ImgWidth - 2 < bit.Width)
|
|
556
|
+ {
|
|
557
|
+ try
|
|
558
|
+ {
|
|
559
|
+ Graphics g = Graphics.FromImage(Img);
|
|
560
|
+ //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
561
|
+ g.DrawImage(bit, new Rectangle(0, 0, ImgWidth, ImgHeight), new Rectangle(0, 0, bit.Width, bit.Height), GraphicsUnit.Pixel);
|
|
562
|
+ g.Dispose();
|
|
563
|
+ }
|
|
564
|
+ catch
|
|
565
|
+ {
|
|
566
|
+ Img = bit;
|
|
567
|
+ }
|
|
568
|
+ }
|
|
569
|
+ else
|
|
570
|
+ {
|
|
571
|
+ Img = bit;
|
|
572
|
+ }
|
|
573
|
+ Img.Save(filePathName);
|
|
574
|
+ //Bitmap bitmap = CutImageWhitePart(Img);
|
|
575
|
+ //FileToolsCommon.DeleteFile(filePathName);
|
|
576
|
+ //bitmap.Save(filePathName);
|
|
577
|
+ //bitmap.Dispose();
|
|
578
|
+ memoryStream.Dispose();
|
|
579
|
+ Img.Dispose();
|
|
580
|
+ bit.Dispose();
|
|
581
|
+ }))).Start();
|
|
582
|
+ }
|
|
583
|
+ catch (Exception ex)
|
|
584
|
+ {
|
|
585
|
+
|
|
586
|
+ }
|
|
587
|
+ }
|
|
588
|
+ else
|
|
589
|
+ {
|
|
590
|
+ //SaveImageModel sim = new SaveImageModel();
|
|
591
|
+ //sim.encoder = new PngBitmapEncoder();
|
|
592
|
+ //sim.encoder = new PngBitmapEncoder();
|
|
593
|
+ //BitmapFrame test= encoder.Frames[0];
|
|
594
|
+ //sim.encoder.Frames.Add(BitmapFrame.Create(test));
|
|
595
|
+ ////sim.encoder.Frames[0].CopyTo(encoder.Frames[0]);
|
|
596
|
+ ////sim.encoder.CopyTo(encoder);
|
|
597
|
+ ////encoder.CopyTo(sim.encoder);
|
|
598
|
+ //sim.FilePath = filePathName;
|
|
599
|
+ ////sim.bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Pbgra32);
|
|
600
|
+ ////bmp.CopyTo(sim.bmp);
|
|
601
|
+ //Thread t1 = new Thread(SaveImage);
|
|
602
|
+ //t1.Start(sim);
|
|
603
|
+
|
|
604
|
+ System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create);
|
|
605
|
+ encoder.Save(fs);
|
|
606
|
+ fs.Close();
|
|
607
|
+ }
|
|
608
|
+ }
|
|
609
|
+ catch (Exception ex)
|
|
610
|
+ {
|
|
611
|
+
|
|
612
|
+ }
|
|
613
|
+ }
|
|
614
|
+ /// <summary>
|
|
615
|
+ /// 截图转换成bitmap
|
|
616
|
+ /// </summary>
|
|
617
|
+ /// <param name="element"></param>
|
|
618
|
+ /// <param name="width">默认控件宽度</param>
|
|
619
|
+ /// <param name="height">默认控件高度</param>
|
|
620
|
+ /// <param name="x">默认0</param>
|
|
621
|
+ /// <param name="y">默认0</param>
|
|
622
|
+ /// <returns></returns>
|
|
623
|
+ public static Bitmap ToBitmap(FrameworkElement element, string filePathName, int width = 0, int height = 0, int x = 0, int y = 0)
|
|
624
|
+ {
|
|
625
|
+ if (width == 0) width = (int)element.ActualWidth;
|
|
626
|
+ if (height == 0) height = (int)element.ActualHeight;
|
|
627
|
+
|
|
628
|
+ var rtb = new RenderTargetBitmap(width, height, x, y, System.Windows.Media.PixelFormats.Default);
|
|
629
|
+ rtb.Render(element);
|
|
630
|
+ var bit = BitmapSourceToBitmap(rtb);
|
|
631
|
+
|
|
632
|
+ //测试代码
|
|
633
|
+ DirectoryInfo d = new DirectoryInfo(System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Cache"));
|
|
634
|
+ if (!d.Exists) d.Create();
|
|
635
|
+ bit.Save(System.IO.Path.Combine(d.FullName, "控件截图.png"));
|
|
636
|
+
|
|
637
|
+ return bit;
|
|
638
|
+ }
|
|
639
|
+
|
|
640
|
+ /// <summary>
|
|
641
|
+ /// BitmapSource转Bitmap
|
|
642
|
+ /// </summary>
|
|
643
|
+ /// <param name="source"></param>
|
|
644
|
+ /// <returns></returns>
|
|
645
|
+ public static Bitmap BitmapSourceToBitmap(BitmapSource source)
|
|
646
|
+ {
|
|
647
|
+ return BitmapSourceToBitmap(source, source.PixelWidth, source.PixelHeight);
|
|
648
|
+ }
|
|
649
|
+
|
|
650
|
+ /// <summary>
|
|
651
|
+ /// Convert BitmapSource to Bitmap
|
|
652
|
+ /// </summary>
|
|
653
|
+ /// <param name="source"></param>
|
|
654
|
+ /// <returns></returns>
|
|
655
|
+ public static Bitmap BitmapSourceToBitmap(BitmapSource source, int width, int height)
|
|
656
|
+ {
|
|
657
|
+ Bitmap bmp = null;
|
|
658
|
+ try
|
|
659
|
+ {
|
|
660
|
+ System.Drawing.Imaging.PixelFormat format = System.Drawing.Imaging.PixelFormat.Format24bppRgb;
|
|
661
|
+ /*set the translate type according to the in param(source)*/
|
|
662
|
+ switch (source.Format.ToString())
|
|
663
|
+ {
|
|
664
|
+ case "Rgb24":
|
|
665
|
+ case "Bgr24": format = System.Drawing.Imaging.PixelFormat.Format24bppRgb; break;
|
|
666
|
+ case "Bgra32": format = System.Drawing.Imaging.PixelFormat.Format32bppPArgb; break;
|
|
667
|
+ case "Bgr32": format = System.Drawing.Imaging.PixelFormat.Format32bppRgb; break;
|
|
668
|
+ case "Pbgra32": format = System.Drawing.Imaging.PixelFormat.Format32bppArgb; break;
|
|
669
|
+ }
|
|
670
|
+ bmp = new Bitmap(width, height, format);
|
|
671
|
+ BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(System.Drawing.Point.Empty, bmp.Size),
|
|
672
|
+ ImageLockMode.WriteOnly,
|
|
673
|
+ format);
|
|
674
|
+ source.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride);
|
|
675
|
+ bmp.UnlockBits(data);
|
|
676
|
+ }
|
|
677
|
+ catch
|
|
678
|
+ {
|
|
679
|
+ if (bmp != null)
|
|
680
|
+ {
|
|
681
|
+ bmp.Dispose();
|
|
682
|
+ bmp = null;
|
|
683
|
+ }
|
|
684
|
+ }
|
|
685
|
+
|
|
686
|
+ return bmp;
|
|
687
|
+ }
|
|
688
|
+ private static void SaveImage1(object obj)
|
486
|
689
|
{
|
487
|
690
|
SaveImageModel sim = (SaveImageModel)obj;
|
488
|
691
|
//RenderTargetBitmap bmp = sim.bmp;
|
|
@@ -627,5 +830,12 @@ namespace Common.system
|
627
|
830
|
public BitmapEncoder encoder { get; set; }
|
628
|
831
|
//public RenderTargetBitmap bmp { get; set; }
|
629
|
832
|
}
|
630
|
|
-
|
|
833
|
+ public class SaveModel
|
|
834
|
+ {
|
|
835
|
+ public int ImgWidth { get; set; }
|
|
836
|
+ public int ImgHeight { get; set; }
|
|
837
|
+ public BitmapEncoder encoder { get; set; }
|
|
838
|
+ public string filePathName { get; set; }
|
|
839
|
+ public RenderTargetBitmap bmp { get; set; }
|
|
840
|
+ }
|
631
|
841
|
}
|