WPF Bitmap轉換ImageSource 內存瀑漲問題解決

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;

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章