BASS.NET API for the Un4seen BASS Audio LibraryWaveFormGetBytePositionFromX Method BASS.NET API for the Un4seen BASS Audio Library
Translates a X coordinate of a drawn WaveForm graphic to the related byte position within the rendered stream.

Namespace: Un4seen.Bass.Misc
Assembly: Bass.Net (in Bass.Net.dll) Version:

public long GetBytePositionFromX(
	int x,
	int graphicsWidth,
	int frameStart,
	int frameEnd


Type: SystemInt32
The X coordinate from which to get the byte position (between 0 and graphicsWidth-1).
Type: SystemInt32
The total width of the drawn WaveForm graphic (e.g. use pictureBox.Width).
Type: SystemInt32
The rendered frame which was used to start creating the image (specify 0 or -1 if the start was from the beginning).
Type: SystemInt32
The rendered frame which was used to end creating the image (specify -1 for the end of the WaveForm).

Return Value

Type: Int64
The converted byte position of the stream which relates to the given X coordinate, else -1.

This method might be used to translate a graphics coordinate to a byte position within a stream. The return value might for example be used by BASS_ChannelSetPosition(Int32, Int64, BASSMode) to set a new playback position.

Note: The byte position relates to the resolution you used with RenderStart(Int32, Boolean)! So if you for example used the BASS_SAMPLE_FLOAT with RenderStart(Int32, Boolean) the byte position relates to 32-bit sample data. However, if your playing stream uses only a 16-bit resolution (e.g. you used BASS_DEFAULT with BASS_StreamCreateFile(String, Int64, Int64, BASSFlag)), the returned byte position will not match! So make sure when you are calling BASS_ChannelSetPosition(Int32, Int64, BASSMode) with this return value, that your stream resolution is the same as the resolution used with RenderStart(Int32, Boolean). Otherwise you must convert the returned byte position (e.g. from 32-bit to 16-bit: pos = returnvalue/2). Or for ease of use you might use the SyncPlayback(Int32) method to ensure, that the return value of this method will already be converted accordingly for you!


Using a pictureBox and the MouseDown event handler:
' assuming the WaveForm is fully displayed in the pictureBox...
Private Sub DrawWave()
  If Not (WF Is Nothing) Then
    Me.pictureBox1.BackgroundImage = WF.CreateBitmap(Me.pictureBox1.Width, 
                                                     -1, -1, False)
    Me.pictureBox1.BackgroundImage = Nothing
  End If
End Sub

Private Sub pictureBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs)
  If WF Is Nothing Then
  End If
  Dim pos As Long = WF.GetBytePositionFromX(e.X, Me.pictureBox1.Width, - 1, - 1)
  Console.WriteLine("Position = {0}, Seconds = {1}", pos, Bass.BASS_ChannelBytes2Seconds(_stream, pos))
  Bass.BASS_ChannelSetPosition(_stream, pos)
End Sub
// assuming the WaveForm is fully displayed in the pictureBox...
private void DrawWave()
  if (WF != null)
    this.pictureBox1.BackgroundImage = WF.CreateBitmap(this.pictureBox1.Width, 
                                                       -1, -1, false);
    this.pictureBox1.BackgroundImage = null;

private void pictureBox1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  if (WF == null)
  long pos = WF.GetBytePositionFromX(e.X, this.pictureBox1.Width, -1, -1);
  Console.WriteLine("Position = {0}, Seconds = {1}", pos, Bass.BASS_ChannelBytes2Seconds(_stream, pos));
  Bass.BASS_ChannelSetPosition(_stream, pos);
See Also