using System; using System.Windows; using System.Drawing; using Microsoft.Win32; using System.Windows.Media.Imaging; using System.Drawing.Imaging; using System.Runtime.InteropServices; using RasonicSignalDetectionCSLib; namespace RasonicSignalDetectionDemo { /// /// MainWindow.xaml 的交互逻辑 /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); _imgorig = null; ImgOrig.Source = _imgorig; } RasonicSignalDetection _process = new RasonicSignalDetection(); private BitmapImage _imgorig; private string _origImgFolder; private void DetectImage(object sender, RoutedEventArgs e) { try { OpenFileDialog openFileD = new OpenFileDialog(); openFileD.Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg"; openFileD.Multiselect = false; openFileD.Title = "选择图像"; if (openFileD.ShowDialog() ?? false) { Bitmap img = new Bitmap(Bitmap.FromFile(openFileD.FileName)); RawImage rawImage = BitmapToRawImage(img); RasonicSignalDetectResult result = _process.DetectRasonicSignal(rawImage); if (result == RasonicSignalDetectResult.NoSignal) { StatusText1.Text = "无信号"; } else if(result == RasonicSignalDetectResult.Signal) { StatusText1.Text = "有信号"; } else { StatusText1.Text = "出现错误"; } _imgorig = BitmapToBitmapImage(img); ImgOrig.Source = _imgorig; ; System.Drawing.Rectangle rect = System.Drawing.Rectangle.Empty; img.Dispose(); } } catch (Exception excep) { MessageBox.Show("发生错误:" + excep.Message); } } /// /// 转为BitmapImage /// /// /// public static BitmapImage BitmapToBitmapImage( Bitmap img) { BitmapImage bmpimg = new BitmapImage(); using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { img.Save(ms, ImageFormat.Png); bmpimg.BeginInit(); bmpimg.StreamSource = ms; bmpimg.CacheOption = BitmapCacheOption.OnLoad; bmpimg.EndInit(); bmpimg.Freeze(); } return bmpimg; } /// /// 将System.Drawimg.Bitmap转为RawImage /// /// /// /// public static RawImage BitmapToRawImage(Bitmap image, bool keepChannel = false) { int width = image.Width; int height = image.Height; System.Drawing.Imaging.PixelFormat pixelFormat = image.PixelFormat; if (pixelFormat != System.Drawing.Imaging.PixelFormat.Format24bppRgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppArgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppPArgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format32bppRgb && pixelFormat != System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { throw new Exception("Unexpected image pixel format:" + pixelFormat); } int origChannel = 3; if (pixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb) { origChannel = 3; } else if (pixelFormat == System.Drawing.Imaging.PixelFormat.Format8bppIndexed) { origChannel = 1; } else { origChannel = 4; } int dstChannel = 3; if (keepChannel) { dstChannel = origChannel; } var bmData = image.LockBits(new System.Drawing.Rectangle(0, 0, width, height), ImageLockMode.ReadOnly, pixelFormat); byte[] dstDataArray = new byte[width * height * dstChannel]; unsafe { int stride = bmData.Stride; int dstStride = width * dstChannel; IntPtr ptr = bmData.Scan0; IntPtr ptrH, ptrW; for (int nh = 0; nh < height; nh++) { ptrH = IntPtr.Add(ptr, nh * stride); if (origChannel == dstChannel) { Marshal.Copy(ptrH, dstDataArray, nh * dstStride, dstStride); } else if (origChannel > dstChannel) { for (int nw = 0; nw < width; nw++) { ptrW = IntPtr.Add(ptrH, nw * origChannel); Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel, dstChannel); } } else { for (int nw = 0; nw < width; nw++) { ptrW = IntPtr.Add(ptrH, nw * origChannel); for (int nc = 0; nc < dstChannel; nc++) { Marshal.Copy(ptrW, dstDataArray, nh * dstStride + nw * dstChannel + nc, 1); } } } } } image.UnlockBits(bmData); return new RawImage(dstDataArray, width, height, dstChannel); } } }