EditToolsHelper.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Media.Imaging;
  9. using System.Windows;
  10. using PDF_Office.Model.EditTools.Background;
  11. using PDF_Office.Model.EditTools.Watermark;
  12. namespace PDF_Office.Helper
  13. {
  14. public static class EditToolsHelper
  15. {
  16. public static byte[] ConvertColor(string color)
  17. {
  18. byte[] rgb_array = new byte[] { 0, 0, 0 };
  19. rgb_array[0] = (byte)(color[1] * 15 + color[2]);
  20. rgb_array[1] = (byte)(color[3] * 15 + color[4]);
  21. rgb_array[2] = (byte)(color[5] * 15 + color[6]);
  22. return rgb_array;
  23. }
  24. public static void ChooseFile(string filePath,ref BackgroundInfo backgroundInfo)
  25. {
  26. using (FileStream fileData = File.OpenRead(filePath))
  27. {
  28. string fileExt = Path.GetExtension(filePath).ToLower();
  29. BitmapFrame frame = null;
  30. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  31. if (decoder != null && decoder.Frames.Count > 0)
  32. {
  33. frame = decoder.Frames[0];
  34. }
  35. if (frame != null)
  36. {
  37. backgroundInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  38. backgroundInfo.ImageWidth = frame.PixelWidth;
  39. backgroundInfo.ImageHeight = frame.PixelHeight;
  40. frame.CopyPixels(backgroundInfo.ImageArray, frame.PixelWidth * 4, 0);
  41. #if DEBUG
  42. Trace.WriteLine("width :" + backgroundInfo.ImageWidth);
  43. Trace.WriteLine("height :" + backgroundInfo.ImageHeight);
  44. #endif
  45. }
  46. }
  47. }
  48. public static void ChooseFile(string filePath, ref WatermarkInfo watermarkInfo)
  49. {
  50. using (FileStream fileData = File.OpenRead(filePath))
  51. {
  52. string fileExt = Path.GetExtension(filePath).ToLower();
  53. BitmapFrame frame = null;
  54. BitmapDecoder decoder = BitmapDecoder.Create(fileData, BitmapCreateOptions.None, BitmapCacheOption.Default);
  55. if (decoder != null && decoder.Frames.Count > 0)
  56. {
  57. frame = decoder.Frames[0];
  58. }
  59. if (frame != null)
  60. {
  61. watermarkInfo.ImageArray = new byte[frame.PixelWidth * frame.PixelHeight * 4];
  62. watermarkInfo.ImageWidth = frame.PixelWidth;
  63. watermarkInfo.ImageHeight = frame.PixelHeight;
  64. frame.CopyPixels(watermarkInfo.ImageArray, frame.PixelWidth * 4, 0);
  65. #if DEBUG
  66. Trace.WriteLine("width :" + watermarkInfo.ImageWidth);
  67. Trace.WriteLine("height :" + watermarkInfo.ImageHeight);
  68. #endif
  69. }
  70. }
  71. }
  72. }
  73. }