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);
        /// 
        /// 显示当前节拍及与平均节拍的差异
        /// 
        /// 
        /// 
        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;

        }

        /// 
        /// 转换Bitmap到BitmapSource(经本人测试此方法为效率最高,内存最低)
        /// 
        /// 
        /// 
        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;
        }

 

你可能感兴趣的:(WPF)