Author Topic: C# WPF WaveForm CreateBitmap return always null  (Read 428 times)

Marcus Reinhart

  • Posts: 13
Hello,

maybe i make some mistake. But i can't find them. I try code bellow but WF.CreateBitmap returns always null.
What is wrong?

Code: [Select]
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Un4seen.Bass;
using Un4seen.Bass.Misc;

namespace WaveFormTest
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        WaveForm WF = null;

        public MainWindow()
        {
            InitializeComponent();

            BassNet.Registration("someMail@email.de", "XXXXXXXXXXXXX");

            Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Process.GetCurrentProcess().MainWindowHandle);
           
string file = @"D:\Sources\Privat\WaveFormTest\WaveFormTest\bin\Debug\Test.mp3";

            WF = new WaveForm(file, new WAVEFORMPROC(WaveFormCallback), null);
            WF.FrameResolution = 0.002f;
            WF.CallbackFrequency = 500;
            WF.DrawWaveForm = WaveForm.WAVEFORMDRAWTYPE.Stereo;
            WF.RenderStart(true, BASSFlag.BASS_DEFAULT);
        }

        private void WaveFormCallback(int framesDone, int framsTotal, TimeSpan elapsedTime, bool finished)
        {
            if (finished)
            {
                Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate ()
                {
                    Bitmap wfBitmap = WF.CreateBitmap((int)imgWF.Width, (int)imgWF.Height, -1, -1, true);
                    ImageSourceConverter c = new ImageSourceConverter();

// imgWF = WPF <Image/>
                    imgWF.Source = (ImageSource)c.ConvertFrom(wfBitmap);
                });
            }
        }
    }
}

radio42

  • Posts: 4637
Re: C# WPF WaveForm CreateBitmap return always null
« Reply #1 on: 27 Feb '18 - 13:10 »
Can you please check, if WF.RenderStart actually returned TRUE?!
If not, it seems, that a decoding stream cannot be created for the given filename.
Also check, if within your WaveFormCallback, what value 'framsTotal' is greater than 0?!

Marcus Reinhart

  • Posts: 13
Re: C# WPF WaveForm CreateBitmap return always null
« Reply #2 on: 27 Feb '18 - 19:51 »
Hi,

thanks for reply. Your hints have help me. But there was one more failure. I've forget to set the image height in the WPF Image Component.
And my conversion not worked. The code bellow do the work now.


Code: [Select]
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;
using Un4seen.Bass;
using Un4seen.Bass.Misc;

namespace WaveFormTest
{
    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        WaveForm WF = null;

        public MainWindow()
        {
            InitializeComponent();

            BassNet.Registration("someMail@email.de", "XXXXXXXXXXXXX");

            Bass.BASS_Init(-1, 44100, BASSInit.BASS_DEVICE_DEFAULT, Process.GetCurrentProcess().MainWindowHandle);
           
    string file = @"D:\Sources\Privat\WaveFormTest\WaveFormTest\bin\Debug\Test.mp3";

            WF = new WaveForm(file, new WAVEFORMPROC(WaveFormCallback), null);
            WF.FrameResolution = 0.002f;
            WF.CallbackFrequency = 500;
            WF.DrawWaveForm = WaveForm.WAVEFORMDRAWTYPE.Stereo;
            WF.RenderStart(true, BASSFlag.BASS_DEFAULT);
        }

        private void WaveFormCallback(int framesDone, int framsTotal, TimeSpan elapsedTime, bool finished)
        {
            if (finished)
            {
                if (framsTotal > 0)
                {
                    Dispatcher.Invoke(DispatcherPriority.Normal, (Action)delegate ()
                    {
                        MemoryStream ms = new MemoryStream();
                        WF.CreateBitmap((int)imgWF.Width, (int)imgWF.Height, -1, -1, true).Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                        BitmapImage image = new BitmapImage();
                        image.BeginInit();
                        ms.Seek(0, SeekOrigin.Begin);
                        image.StreamSource = ms;
                        image.EndInit();

                        // imgWF = <Image /> WPF Component
                        imgWF.Source = image;

                    });
                }
            }
        }
    }
}