MainWindow.xaml.cs 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. using System;
  2. using System.Windows;
  3. using Microsoft.Win32;
  4. using System.Drawing;
  5. using System.Runtime.InteropServices;
  6. namespace ImageProcessUtilsTest
  7. {
  8. /// <summary>
  9. /// MainWindow.xaml 的交互逻辑
  10. /// </summary>
  11. public partial class MainWindow : Window
  12. {
  13. private ImageUtils _aaa = new ImageUtils();
  14. public MainWindow()
  15. {
  16. InitializeComponent();
  17. }
  18. private void OnTestResizeOnlyClick(object sender, RoutedEventArgs e)
  19. {
  20. OpenFileDialog ofd = new OpenFileDialog();
  21. ofd.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg";
  22. ofd.Multiselect = false;
  23. if (ofd.ShowDialog() == true)
  24. {
  25. Bitmap image = new Bitmap(ofd.FileName);
  26. var rawImg = _aaa.BitmapToRawImage(image);
  27. var resizedRawImg = _aaa.ResizeBilinearWithCpp(rawImg, _aaa.NetImgW, _aaa.NetImgH);
  28. var resizedBitmap = _aaa.RawImageToBitmap(resizedRawImg);
  29. resizedBitmap.Save("aaa.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
  30. MessageBox.Show("完成");
  31. }
  32. }
  33. private void OnTestResizeCompareClick(object sender, RoutedEventArgs e)
  34. {
  35. OpenFileDialog ofd = new OpenFileDialog();
  36. ofd.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg";
  37. ofd.Multiselect = false;
  38. if (ofd.ShowDialog() == true)
  39. {
  40. Bitmap image = new Bitmap(ofd.FileName);
  41. var rawImg = _aaa.BitmapToRawImage(image);
  42. _aaa.DoResize(rawImg, new MyRect(10, 10, image.Width - 20, image.Height - 20), new MyRect(0, 0, _aaa.NetImgW, _aaa.NetImgH));
  43. if (_aaa.NetImgC == 3)
  44. {
  45. _aaa.ExtractRGB24AsColor(0);
  46. }
  47. else
  48. {
  49. _aaa.ExtractRGB24AsGray(0);
  50. }
  51. string path = "resize_orig.txt";
  52. System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate);
  53. fs.Write(_aaa.ResizedImageDataBuffer, 0, _aaa.ResizedImageDataBuffer.Length);
  54. fs.Close();
  55. string pathExtract = "extract_orig.txt";
  56. System.IO.FileStream fsExtract = new System.IO.FileStream(pathExtract, System.IO.FileMode.OpenOrCreate);
  57. byte[] dataBuffer = new byte[_aaa.DataBuffer.Length * 4];
  58. GCHandle hObjectDst = GCHandle.Alloc(dataBuffer, GCHandleType.Pinned);
  59. IntPtr pDataDst = hObjectDst.AddrOfPinnedObject();
  60. Marshal.Copy(_aaa.DataBuffer, 0, pDataDst, _aaa.DataBuffer.Length);
  61. fsExtract.Write(dataBuffer, 0, dataBuffer.Length);
  62. fsExtract.Close();
  63. _aaa.DoResizeCpp(rawImg, new MyRect(10, 10, image.Width - 20, image.Height - 20), new MyRect(0, 0, _aaa.NetImgW, _aaa.NetImgH));
  64. if (_aaa.NetImgC == 3)
  65. {
  66. _aaa.ExtractRGB24AsColorCpp(0);
  67. }
  68. else
  69. {
  70. _aaa.ExtractRGB24AsGrayCpp(0);
  71. }
  72. string pathCpp = "resize_cpp.txt";
  73. System.IO.FileStream fsCpp = new System.IO.FileStream(pathCpp, System.IO.FileMode.OpenOrCreate);
  74. fsCpp.Write(_aaa.ResizedImageDataBuffer, 0, _aaa.ResizedImageDataBuffer.Length);
  75. fsCpp.Close();
  76. string pathExtractCpp = "extract_cpp.txt";
  77. System.IO.FileStream fsExtractCpp = new System.IO.FileStream(pathExtractCpp, System.IO.FileMode.OpenOrCreate);
  78. byte[] dataBufferCpp = new byte[_aaa.DataBuffer.Length * 4];
  79. GCHandle hObjectDstCpp = GCHandle.Alloc(dataBufferCpp, GCHandleType.Pinned);
  80. IntPtr pDataDstCpp = hObjectDstCpp.AddrOfPinnedObject();
  81. Marshal.Copy(_aaa.DataBuffer, 0, pDataDstCpp, _aaa.DataBuffer.Length);
  82. fsExtractCpp.Write(dataBufferCpp, 0, dataBufferCpp.Length);
  83. fsExtractCpp.Close();
  84. MessageBox.Show("完成");
  85. }
  86. }
  87. }
  88. }