473,503 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Images, soundfiles, and SQL image

I have an application that allows users to view images and hear sound files.
The images and soundclips are stores in a SQL database in image fields.
Originally, I transferred the data from an Access database using the Upsize
wizard. I have been able to retrieve the data from the SQL database using
memorystreams. To do so, I have been using offests to bypass a header that is
present: for the images, I use offest 78 and for the sounds, it's 86.

All has been well and good but now I need to load images and sound clips
directly using my app, not Access. I have tried uploading the files using no
offset and/or the offsets I have previously needed to use, all to no avail.
For instance, after I store a .wav file to the database, I receive a message
"The wave header is corrupt".

Now, I see all kinds of examples to store and retrieve blob data on the
internet. None of them deal with headers.

Can anyone clear up this mystery? I am using VS 2005 Express C# edition and
SQL Express 2005.

Code examples:

public void PlaySound()
{
if (ds.Tables["Cards"] != null)
{
if (isSound)
{
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Sound FROM Sounds WHERE Sounds.CardID=" +

ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(),
sqlConn);
DataTable dt = new DataTable();
sql.Fill(dt);
sqlConn.Close();
DataRow dr = dt.Rows[0];

byte[] result = (byte[])dr["Sound"];

try
{
// Retrieve the sound file using the header offset
(86)
MemoryStream ms = new MemoryStream(result,
SNDHDROFFSET, result.Length - SNDHDROFFSET);
SoundPlayer sp = new SoundPlayer(ms);
sp.Play();
}
catch (Exception e)
{
MessageBox.Show("Sound header error! The sound clip
is not properly formatted in the SQL database. "+ e.Message);
}
}
}
}
}
public void AddSound(string soundFilename)
{
SqlCommand cmd = new SqlCommand();
FileStream stream = new FileStream(soundFilename, FileMode.Open,
FileAccess.Read);
byte[] snd = new byte[stream.Length];
stream.Read(snd, 0, (int)stream.Length);
stream.Close();

sqlConn.Open();
if (!isSound)
cmd = new SqlCommand("InsertSound", sqlConn);
else
cmd = new SqlCommand("UpdateSound", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@CardID", SqlDbType.Int).Value =
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"];
cmd.Parameters.Add("@Sound", SqlDbType.Image).Value = snd;
cmd.ExecuteNonQuery();
sqlConn.Close();
isSound = true;
}
ALTER PROCEDURE dbo.InsertSound
(
@CardID int,
@Sound image
)
AS
INSERT INTO Sounds (CardID, Sound)
VALUES (@CardID, @Sound)
RETURN

Nov 17 '05 #1
5 3034
Your assumption here that the header for a WAV sound file is either 78 or 86
bytes is incorrect. WAV headers are typically 44 bytes. In addition, you
would still need the WAV header to be intact in order to play the sound using
the WaveOut Windows API, unless you were to play it as RAW PCM Data at a
fixed sampling rate and channel(s).
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"wschlichtman" wrote:
I have an application that allows users to view images and hear sound files.
The images and soundclips are stores in a SQL database in image fields.
Originally, I transferred the data from an Access database using the Upsize
wizard. I have been able to retrieve the data from the SQL database using
memorystreams. To do so, I have been using offests to bypass a header that is
present: for the images, I use offest 78 and for the sounds, it's 86.

All has been well and good but now I need to load images and sound clips
directly using my app, not Access. I have tried uploading the files using no
offset and/or the offsets I have previously needed to use, all to no avail.
For instance, after I store a .wav file to the database, I receive a message
"The wave header is corrupt".

Now, I see all kinds of examples to store and retrieve blob data on the
internet. None of them deal with headers.

Can anyone clear up this mystery? I am using VS 2005 Express C# edition and
SQL Express 2005.

Code examples:

public void PlaySound()
{
if (ds.Tables["Cards"] != null)
{
if (isSound)
{
sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Sound FROM Sounds WHERE Sounds.CardID=" +

ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(),
sqlConn);
DataTable dt = new DataTable();
sql.Fill(dt);
sqlConn.Close();
DataRow dr = dt.Rows[0];

byte[] result = (byte[])dr["Sound"];

try
{
// Retrieve the sound file using the header offset
(86)
MemoryStream ms = new MemoryStream(result,
SNDHDROFFSET, result.Length - SNDHDROFFSET);
SoundPlayer sp = new SoundPlayer(ms);
sp.Play();
}
catch (Exception e)
{
MessageBox.Show("Sound header error! The sound clip
is not properly formatted in the SQL database. "+ e.Message);
}
}
}
}
}
public void AddSound(string soundFilename)
{
SqlCommand cmd = new SqlCommand();
FileStream stream = new FileStream(soundFilename, FileMode.Open,
FileAccess.Read);
byte[] snd = new byte[stream.Length];
stream.Read(snd, 0, (int)stream.Length);
stream.Close();

sqlConn.Open();
if (!isSound)
cmd = new SqlCommand("InsertSound", sqlConn);
else
cmd = new SqlCommand("UpdateSound", sqlConn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add("@CardID", SqlDbType.Int).Value =
ds.Tables["Cards"].Rows[curMgr.Position]["CardID"];
cmd.Parameters.Add("@Sound", SqlDbType.Image).Value = snd;
cmd.ExecuteNonQuery();
sqlConn.Close();
isSound = true;
}
ALTER PROCEDURE dbo.InsertSound
(
@CardID int,
@Sound image
)
AS
INSERT INTO Sounds (CardID, Sound)
VALUES (@CardID, @Sound)
RETURN

Nov 17 '05 #2
Actually, the header information I have (86 bytes) is correct. I am using,
and have been using, the System.Media.SoundPlayer class to play the file from
a database image field. It works perfectly. Attempting to use any other
offest other than 86 throws an exception with an invalid header.

I am not using the WaveOut API as there is no longer a need to do so since
the latest .NET class library includes replacement functionality.

"Peter Bromberg [C# MVP]" wrote:
Your assumption here that the header for a WAV sound file is either 78 or 86
bytes is incorrect. WAV headers are typically 44 bytes. In addition, you
would still need the WAV header to be intact in order to play the sound using
the WaveOut Windows API, unless you were to play it as RAW PCM Data at a
fixed sampling rate and channel(s).
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


Nov 17 '05 #3
Ok. In that case what I would do is retrieve on of these sound files from
your database image field, and save it on the filesystem with a .Wav
extension. Then attempt to play it using your application.

If it doesn't play, you have more information about where and what is going
wrong.
Cheers,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"wschlichtman" wrote:
Actually, the header information I have (86 bytes) is correct. I am using,
and have been using, the System.Media.SoundPlayer class to play the file from
a database image field. It works perfectly. Attempting to use any other
offest other than 86 throws an exception with an invalid header.

I am not using the WaveOut API as there is no longer a need to do so since
the latest .NET class library includes replacement functionality.

"Peter Bromberg [C# MVP]" wrote:
Your assumption here that the header for a WAV sound file is either 78 or 86
bytes is incorrect. WAV headers are typically 44 bytes. In addition, you
would still need the WAV header to be intact in order to play the sound using
the WaveOut Windows API, unless you were to play it as RAW PCM Data at a
fixed sampling rate and channel(s).
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com

Nov 17 '05 #4
Yep, it works if I retrieve the soundclips that I upsized from Access. Here's
the code:

sqlConn.Open();
SqlDataAdapter sql = new SqlDataAdapter(
"SELECT Sound FROM Sounds WHERE Sounds.CardID=" +

ds.Tables["Cards"].Rows[curMgr.Position]["CardID"].ToString(),
sqlConn);
DataTable dt = new DataTable();
sql.Fill(dt);
sqlConn.Close();
DataRow dr = dt.Rows[0];

byte[] result = (byte[])dr["Sound"];

try
{
// Retrieve the sound file using the header offset
(86)
using (MemoryStream ms = new MemoryStream(result,
SNDHDROFFSET, result.Length - SNDHDROFFSET))
{
// Test that sound file offest is correct
using (FileStream fs = new
FileStream("c:\\test.wav", FileMode.Create))
{
fs.Write(result, SNDHDROFFSET, result.Length
- SNDHDROFFSET);
}
SoundPlayer sp = new SoundPlayer(ms);
sp.Play();
}
}
catch (Exception e)
{
MessageBox.Show("Sound header error! The sound clip
is not properly formatted in the SQL database. "+ e.Message);
}

The constant SNDHDROFFSET is set to 86. The soundclip sounds exactly the
same using either the sp.Play() method or by using MS Media Player.

Any ideas?
"Peter Bromberg [C# MVP]" wrote:
Ok. In that case what I would do is retrieve on of these sound files from
your database image field, and save it on the filesystem with a .Wav
extension. Then attempt to play it using your application.

If it doesn't play, you have more information about where and what is going
wrong.
Cheers,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


Nov 17 '05 #5
As a followup, I believe my problem may be in the stored procedure I posted
in my original message. I am very noobish to SQL, esp. in stored procedures,
and think maybe I'm supposed to be referring to READTEXT or textptr(), but am
not sure. As it stands, I pass the byte[] buffer in one of the parameters to
the INSERT/UPDATE commands in my code. I do no other processing. Am I correct
in assuming this may be where the problem lies?

The reason I deduce this is that when I look at the hex representation of
the stored data as opposed to the original sound clip file, they are nothing
alike.

"Peter Bromberg [C# MVP]" wrote:
Ok. In that case what I would do is retrieve on of these sound files from
your database image field, and save it on the filesystem with a .Wav
extension. Then attempt to play it using your application.

If it doesn't play, you have more information about where and what is going
wrong.
Cheers,
Peter
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com


"wschlichtman" wrote:
Actually, the header information I have (86 bytes) is correct. I am using,
and have been using, the System.Media.SoundPlayer class to play the file from
a database image field. It works perfectly. Attempting to use any other
offest other than 86 throws an exception with an invalid header.

I am not using the WaveOut API as there is no longer a need to do so since
the latest .NET class library includes replacement functionality.

"Peter Bromberg [C# MVP]" wrote:
Your assumption here that the header for a WAV sound file is either 78 or 86
bytes is incorrect. WAV headers are typically 44 bytes. In addition, you
would still need the WAV header to be intact in order to play the sound using
the WaveOut Windows API, unless you were to play it as RAW PCM Data at a
fixed sampling rate and channel(s).
--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com

Nov 17 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1674
by: benjamin | last post by:
A pygame/python game resource question ###################################### I wander whether there is any possibility to compile a bunch of resources for a program, like images and soundfiles...
7
2310
by: Vinay | last post by:
Hi All: I have a small application that stores images either in the database or as files (depending on the user preference). I'm in the process of providing a web interface to this application....
8
2435
by: Chris Beall | last post by:
I'm struggling with how to handle, on a web page, images that contain text that the user must be able to read. Examples: tombstone photos, photos or scans of historic documents (handwritten or...
2
1952
by: mikeoley | last post by:
Ok I have a Javascript slideshow working. Every image is linked to a another page in the site. Problem is...The link wont refresh to the next link once the second images rollovers in the slideshow....
10
2293
by: Neo Geshel | last post by:
I am seeking to hand-roll my own blog in ASP.NET 2.0 and SQLExpress 2005. Why? Because I can. Because I will gain experience. The one thing that has me stumped at square one is inline images....
8
1985
by: kp87 | last post by:
I am a little bit stuck .... I want to play a bunch of soundfiles randomly, but i want to give each soundfile a rating (say 0-100) and have the likelihood that the file be chosen be tied to its...
61
4667
by: phil-news-nospam | last post by:
Why does SVG need a different tag than other images? IMHO, SVG should be implemented as an image type just like any other image type, allowing it to work with <img> tags, and ... here is the...
1
3219
by: Xah Lee | last post by:
The following is a program to generate thumbnail images for a website. Useful, if you want to do that. It is used to generate the thumbnails for my “Banners, Damsels, and Mores” project...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7084
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7278
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
5013
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
380
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.