标签:System timer bitmap new 瀑涨 WPF Bitmap Drawing
DispatcherTimer timer_CurrentBeat = new DispatcherTimer();//条码自动读取时钟 private void Window_Loaded(object sender, RoutedEventArgs e) { timer_CurrentBeat.Interval = new TimeSpan(0, 0, 1); //500毫秒执行一次 timer_CurrentBeat.Interval = new TimeSpan(500); timer_CurrentBeat.Tick += timer_CurrentBeat_Tick; timer_CurrentBeat.Start(); } delegate void SetIMGCallback(Bitmap text); /// <summary> /// 显示当前节拍及与平均节拍的差异 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer_CurrentBeat_Tick(object sender, EventArgs e) { string noImgPath = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + @"\Resources\\151221102251.jpg"; System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(noImgPath); this.Dispatcher.BeginInvoke(new SetIMGCallback(GetBitmapSource), bitmap);//为异步调用委托 //BitmapToBitmapImage(bitmap); } [DllImport("gdi32.dll")] static extern bool DeleteObject(IntPtr hObject); public void BandImg2(Bitmap bitmap) { try { IntPtr hBitmap = bitmap.GetHbitmap(); ImageSource wpfBitmap = System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap( hBitmap, IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions()); if (!DeleteObject(hBitmap)) { throw new System.ComponentModel.Win32Exception(); } this.picImage.Source = wpfBitmap; } catch (Exception ex) { MessageBox.Show(ex.Message); } } public void BitmapToBitmapImage(Bitmap bitmap) { Bitmap bitmapSource = new Bitmap(bitmap.Width, bitmap.Height); int i, j; for (i = 0; i < bitmap.Width; i++) { for (j = 0; j < bitmap.Height; j++) { System.Drawing.Color pixelColor = bitmap.GetPixel(i, j); System.Drawing.Color newColor = System.Drawing.Color.FromArgb(pixelColor.R, pixelColor.G, pixelColor.B); bitmapSource.SetPixel(i, j, newColor); } } MemoryStream ms = new MemoryStream(); bitmapSource.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); BitmapImage bitmapImage = new BitmapImage(); bitmapImage.BeginInit(); bitmapImage.StreamSource = new MemoryStream(ms.ToArray()); bitmapImage.EndInit(); bitmapImage.Freeze(); this.picImage.Source = bitmapImage; } /// <summary> /// 转换Bitmap到BitmapSource(经本人测试此方法为效率最高,内存最低) /// </summary> /// <param name="bmp"></param> /// <returns></returns> public void GetBitmapSource(System.Drawing.Bitmap bmp) { BitmapFrame bf = null; using (System.IO.MemoryStream ms = new System.IO.MemoryStream()) { bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png); bf = BitmapFrame.Create(ms, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); } this.picImage.Source = bf;
标签:System,timer,bitmap,new,瀑涨,WPF,Bitmap,Drawing 来源: https://www.cnblogs.com/robertyao/p/16381477.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。