|
@@ -1,5 +1,6 @@
|
1
|
1
|
using System;
|
2
|
2
|
using System.Drawing;
|
|
3
|
+using System.Drawing.Drawing2D;
|
3
|
4
|
using System.Drawing.Imaging;
|
4
|
5
|
using System.IO;
|
5
|
6
|
using System.Text;
|
|
@@ -98,7 +99,7 @@ namespace Common.system
|
98
|
99
|
bitmap.EndInit();
|
99
|
100
|
bitmap.Freeze();
|
100
|
101
|
}
|
101
|
|
- return bitmap;
|
|
102
|
+ return bitmap;
|
102
|
103
|
}
|
103
|
104
|
catch (Exception)
|
104
|
105
|
{
|
|
@@ -395,27 +396,55 @@ namespace Common.system
|
395
|
396
|
/// </summary>
|
396
|
397
|
/// <param name="ui">需要截图的UI控件</param>
|
397
|
398
|
/// <param name="filePathName">图片保存地址 命名 1.png</param>
|
398
|
|
- /// <param name="width">保存宽</param>
|
399
|
|
- /// <param name="height">保存高</param>
|
400
|
|
- public static void SaveUIToImage(FrameworkElement ui, string filePathName, int width, int height)
|
|
399
|
+ /// <param name="width">窗口宽</param>
|
|
400
|
+ /// <param name="height">窗口高</param>
|
|
401
|
+ /// <param name="ImgWidth">图片高</param>
|
|
402
|
+ /// <param name="ImgHeight">图片高</param>
|
|
403
|
+ public static void SaveUIToImage(FrameworkElement ui, string filePathName, int width, int height, int ImgWidth, int ImgHeight)
|
401
|
404
|
{
|
402
|
405
|
try
|
403
|
406
|
{
|
404
|
|
- System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create);
|
|
407
|
+ //System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create);
|
405
|
408
|
//在位图中呈现UI元素
|
406
|
|
- RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY,
|
407
|
|
- PixelFormats.Pbgra32);
|
|
409
|
+ RenderTargetBitmap bmp = new RenderTargetBitmap(width, height, PrimaryScreen.DpiX, PrimaryScreen.DpiY, PixelFormats.Pbgra32);
|
408
|
410
|
bmp.Render(ui);
|
409
|
411
|
BitmapEncoder encoder = new PngBitmapEncoder();
|
410
|
412
|
encoder.Frames.Add(BitmapFrame.Create(bmp));
|
411
|
|
- encoder.Save(fs);
|
412
|
|
- fs.Close();
|
413
|
|
- new Thread(new ThreadStart(new Action(() =>
|
|
413
|
+ //encoder.Save(fs);
|
|
414
|
+ //fs.Close();
|
|
415
|
+ Bitmap Img = new Bitmap(ImgWidth, ImgHeight);
|
|
416
|
+ try
|
414
|
417
|
{
|
415
|
|
- Bitmap bitmap = CutImageWhitePart(filePathName);
|
416
|
|
- FileToolsCommon.DeleteFile(filePathName);
|
417
|
|
- bitmap.Save(filePathName);
|
418
|
|
- }))).Start();
|
|
418
|
+ MemoryStream memoryStream = new MemoryStream();
|
|
419
|
+ encoder.Save(memoryStream);
|
|
420
|
+ //System.Drawing.Image img = System.Drawing.Image.FromStream(memoryStream);
|
|
421
|
+ Bitmap bit = new Bitmap(memoryStream);
|
|
422
|
+ try
|
|
423
|
+ {
|
|
424
|
+ Graphics g = Graphics.FromImage(Img);
|
|
425
|
+ //g.InterpolationMode = InterpolationMode.HighQualityBicubic;
|
|
426
|
+ g.DrawImage(bit, new Rectangle(0, 0, ImgWidth, ImgHeight), new Rectangle(0, 0, (int)bit.Width, (int)bit.Height), GraphicsUnit.Pixel);
|
|
427
|
+ g.Dispose();
|
|
428
|
+ }
|
|
429
|
+ catch
|
|
430
|
+ {
|
|
431
|
+ Img = bit;
|
|
432
|
+ }
|
|
433
|
+ new Thread(new ThreadStart(new Action(() =>
|
|
434
|
+ {
|
|
435
|
+ //Img.Save(filePathName);
|
|
436
|
+ Bitmap bitmap = CutImageWhitePart(Img);
|
|
437
|
+ FileToolsCommon.DeleteFile(filePathName);
|
|
438
|
+ bitmap.Save(filePathName);
|
|
439
|
+ bitmap.Dispose();
|
|
440
|
+ Img.Dispose();
|
|
441
|
+ bit.Dispose();
|
|
442
|
+ }))).Start();
|
|
443
|
+ }
|
|
444
|
+ catch (Exception ex)
|
|
445
|
+ {
|
|
446
|
+
|
|
447
|
+ }
|
419
|
448
|
}
|
420
|
449
|
catch (Exception ex)
|
421
|
450
|
{
|
|
@@ -430,9 +459,9 @@ namespace Common.system
|
430
|
459
|
/// 裁剪图片(去掉白边)
|
431
|
460
|
/// </summary>
|
432
|
461
|
/// <param name="FilePath"></param>
|
433
|
|
- public static Bitmap CutImageWhitePart(string FilePath)
|
|
462
|
+ public static Bitmap CutImageWhitePart(Bitmap bmp)
|
434
|
463
|
{
|
435
|
|
- Bitmap bmp = new Bitmap(FilePath);
|
|
464
|
+ //Bitmap bmp = new Bitmap(FilePath);
|
436
|
465
|
//上左右下
|
437
|
466
|
int top = 0, left = 0, right = bmp.Width, bottom = bmp.Height;
|
438
|
467
|
|