using System; using System.Windows; using Microsoft.Win32; using System.Drawing; using System.Runtime.InteropServices; namespace ImageProcessUtilsTest { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { private ImageUtils _aaa = new ImageUtils(); public MainWindow() { InitializeComponent(); } private void OnTestResizeOnlyClick(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg"; ofd.Multiselect = false; if (ofd.ShowDialog() == true) { Bitmap image = new Bitmap(ofd.FileName); var rawImg = _aaa.BitmapToRawImage(image); var resizedRawImg = _aaa.ResizeBilinearWithCpp(rawImg, _aaa.NetImgW, _aaa.NetImgH); var resizedBitmap = _aaa.RawImageToBitmap(resizedRawImg); resizedBitmap.Save("aaa.jpg", System.Drawing.Imaging.ImageFormat.Jpeg); MessageBox.Show("完成"); } } private void OnTestResizeCompareClick(object sender, RoutedEventArgs e) { OpenFileDialog ofd = new OpenFileDialog(); ofd.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg"; ofd.Multiselect = false; if (ofd.ShowDialog() == true) { Bitmap image = new Bitmap(ofd.FileName); var rawImg = _aaa.BitmapToRawImage(image); _aaa.DoResize(rawImg, new MyRect(10, 10, image.Width - 20, image.Height - 20), new MyRect(0, 0, _aaa.NetImgW, _aaa.NetImgH)); if (_aaa.NetImgC == 3) { _aaa.ExtractRGB24AsColor(0); } else { _aaa.ExtractRGB24AsGray(0); } string path = "resize_orig.txt"; System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate); fs.Write(_aaa.ResizedImageDataBuffer, 0, _aaa.ResizedImageDataBuffer.Length); fs.Close(); string pathExtract = "extract_orig.txt"; System.IO.FileStream fsExtract = new System.IO.FileStream(pathExtract, System.IO.FileMode.OpenOrCreate); byte[] dataBuffer = new byte[_aaa.DataBuffer.Length * 4]; GCHandle hObjectDst = GCHandle.Alloc(dataBuffer, GCHandleType.Pinned); IntPtr pDataDst = hObjectDst.AddrOfPinnedObject(); Marshal.Copy(_aaa.DataBuffer, 0, pDataDst, _aaa.DataBuffer.Length); fsExtract.Write(dataBuffer, 0, dataBuffer.Length); fsExtract.Close(); _aaa.DoResizeCpp(rawImg, new MyRect(10, 10, image.Width - 20, image.Height - 20), new MyRect(0, 0, _aaa.NetImgW, _aaa.NetImgH)); if (_aaa.NetImgC == 3) { _aaa.ExtractRGB24AsColorCpp(0); } else { _aaa.ExtractRGB24AsGrayCpp(0); } string pathCpp = "resize_cpp.txt"; System.IO.FileStream fsCpp = new System.IO.FileStream(pathCpp, System.IO.FileMode.OpenOrCreate); fsCpp.Write(_aaa.ResizedImageDataBuffer, 0, _aaa.ResizedImageDataBuffer.Length); fsCpp.Close(); string pathExtractCpp = "extract_cpp.txt"; System.IO.FileStream fsExtractCpp = new System.IO.FileStream(pathExtractCpp, System.IO.FileMode.OpenOrCreate); byte[] dataBufferCpp = new byte[_aaa.DataBuffer.Length * 4]; GCHandle hObjectDstCpp = GCHandle.Alloc(dataBufferCpp, GCHandleType.Pinned); IntPtr pDataDstCpp = hObjectDstCpp.AddrOfPinnedObject(); Marshal.Copy(_aaa.DataBuffer, 0, pDataDstCpp, _aaa.DataBuffer.Length); fsExtractCpp.Write(dataBufferCpp, 0, dataBufferCpp.Length); fsExtractCpp.Close(); MessageBox.Show("完成"); } } } }