123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- using ComPDFKitViewer.PdfViewer;
- using Microsoft.Win32;
- using PDF_Office.Model;
- using Prism.Commands;
- using Prism.Mvvm;
- using Prism.Regions;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- namespace PDF_Office.ViewModels.PropertyPanel.AnnotPanel
- {
- class SignatureCreateDialogViewModel : BindableBase, IDialogAware
- {
- public string Title => "";
- private CPDFViewer PDFViewer;
- public event Action<IDialogResult> RequestClose;
- public DelegateCommand CancelCommand { get; set; }
- public DelegateCommand CreateCommnad { get; set; }
- public DelegateCommand<object> CheckedCommnad { get; set; }
- public DelegateCommand OpenImageCommnad { get; set; }
- public ObservableCollection<string> FontNameList { get; set; }
- private int fontNameIndex = 0;
- public int FontNameIndex
- {
- get { return fontNameIndex; }
- set
- {
- SetProperty(ref fontNameIndex, value);
- }
- }
- private int radioButtonIndex;
- public int RadioButtonIndex
- {
- get { return radioButtonIndex; }
- set
- {
- SetProperty(ref radioButtonIndex, value);
- }
- }
- private int tabItemIndex;
- public int TabItemIndex
- {
- get { return tabItemIndex; }
- set
- {
- SetProperty(ref tabItemIndex, value);
- }
- }
- private string inputText = "";
- public string InputText
- {
- get { return inputText; }
- set
- {
- SetProperty(ref inputText, value);
- }
- }
- public string SaveToPath { get; private set; }
- public SignatureCreateDialogViewModel()
- {
- FontNameList = new ObservableCollection<string>();
- CancelCommand = new DelegateCommand(Cancel);
- CreateCommnad = new DelegateCommand(Create);
- CheckedCommnad = new DelegateCommand<object>(Checked);
- OpenImageCommnad = new DelegateCommand(OpenImage);
- InitFontNameList();
- }
- private void InitFontNameList()
- {
- System.Drawing.Text.InstalledFontCollection objFont = new System.Drawing.Text.InstalledFontCollection();
- foreach (var item in objFont.Families)
- {
- FontNameList.Add(item.Name);
- }
- }
- private void Checked(object index)
- {
- RadioButtonIndex = Convert.ToInt32(index);
- }
- public void OpenImage()
- {
- OpenFileDialog openFile = new OpenFileDialog();
- openFile.Filter = "All Image Files(*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff)|*.bmp;*.gif;*.jpeg;*.jpg;*.png;*.tiff|(*.bmp)|*.bmp|" +
- "(*.gif)|*.gif|" +
- "(*.jpeg)|*.jpeg|" +
- "(*.jpg)|*.jpg|" +
- "(*.png)|*.png|" +
- "(*.tiff)|*.tiff";
- if (openFile.ShowDialog() == false)
- {
- return;
- }
- string path = App.CachePath.CustomStampPath;
- string name = Guid.NewGuid().ToString();
- if (!string.IsNullOrEmpty(path))
- {
- BitmapImage image = new BitmapImage(new Uri(openFile.FileName));
- double scale = Math.Min((double)600 / image.PixelWidth, (double)600 / image.PixelHeight);
- scale = Math.Min(scale, 1);
- string ext = Path.GetExtension(openFile.FileName);
- if (ext.ToUpper() == ".PNG")
- {
- BitmapEncoder encoder = new PngBitmapEncoder();
- var targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
- encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
- path = System.IO.Path.Combine(path, name);
- using (FileStream stream = new FileStream(path, FileMode.Create))
- {
- encoder.Save(stream);
- }
- if (!string.IsNullOrEmpty(SaveToPath))
- {
- App.CachePath.AddToDeleteFiles(SaveToPath);
- }
- SaveToPath = path;
- }
- else
- {
- BitmapEncoder encoder = new JpegBitmapEncoder();
- TransformedBitmap targetBitmap = new TransformedBitmap(image, new ScaleTransform(scale, scale));
- encoder.Frames.Add(BitmapFrame.Create(targetBitmap));
- path = System.IO.Path.Combine(path, name);
- using (FileStream stream = new FileStream(path, FileMode.Create))
- {
- encoder.Save(stream);
- }
- if (!string.IsNullOrEmpty(SaveToPath))
- {
- App.CachePath.AddToDeleteFiles(SaveToPath);
- }
- SaveToPath = path;
- }
- }
- else
- {
- SaveToPath = "";
- }
- }
- /// <summary>
- /// 把文字转换成Bitmap
- /// </summary>
- /// <param name="text"></param>
- /// <param name="font"></param>
- /// <param name="rect">用于输出的矩形,文字在这个矩形内显示,为空时自动计算</param>
- /// <param name="fontcolor">字体颜色</param>
- /// <param name="backColor">背景颜色</param>
- /// <returns></returns>
- private Bitmap TextToBitmap(string text, Font font, Rectangle rect, System.Drawing.Color fontcolor, System.Drawing.Color backColor)
- {
- Graphics g;
- Bitmap bmp;
- StringFormat format = new StringFormat(StringFormatFlags.NoClip);
- if (rect == Rectangle.Empty)
- {
- bmp = new Bitmap(1, 1);
- g = Graphics.FromImage(bmp);
- //计算绘制文字所需的区域大小(根据宽度计算长度),重新创建矩形区域绘图
- SizeF sizef = g.MeasureString(text, font, PointF.Empty, format);
- int width = (int)(sizef.Width + 1);
- int height = (int)(sizef.Height + 1);
- rect = new Rectangle(0, 0, width, height);
- bmp.Dispose();
- bmp = new Bitmap(width, height);
- }
- else
- {
- bmp = new Bitmap(rect.Width, rect.Height);
- }
- g = Graphics.FromImage(bmp);
- //使用ClearType字体功能
- g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
- g.FillRectangle(new SolidBrush(backColor), rect);
- g.DrawString(text, font, new SolidBrush(fontcolor), rect, format);
- return bmp;
- }
- private void Cancel()
- {
- RequestClose.Invoke(new DialogResult(ButtonResult.Cancel));
- }
- private void Create()
- {
- switch (TabItemIndex)
- {
- case 0:
- Bitmap bmp = TextToBitmap(InputText, new Font(FontNameList[fontNameIndex], 48), Rectangle.Empty, System.Drawing.Color.Red, System.Drawing.Color.Transparent);
- string guid = Guid.NewGuid().ToString();
- string path = System.IO.Path.Combine(App.CachePath.SignatureStampPath, guid);
- bmp.Save(path, ImageFormat.Png);
- SaveToPath = path;
- break;
- case 1:
- break;
- case 2:
- break;
- default:
- break;
- }
- DialogParameters valuePairs = new DialogParameters();
- valuePairs.Add(ParameterNames.DataModel, this);
- RequestClose.Invoke(new DialogResult(ButtonResult.OK, valuePairs));
- }
- public bool CanCloseDialog()
- {
- return true;
- }
- public void OnDialogClosed()
- {
- return;
- }
- public void OnDialogOpened(IDialogParameters parameters)
- {
- if (parameters != null)
- {
- PDFViewer = parameters.GetValue<CPDFViewer>(ParameterNames.PDFViewer);
- if (PDFViewer != null)
- {
- }
- }
- return;
- }
- }
- }
|