Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / VB.NET / July 2007

Tip: Looking for answers? Try searching our database.

Picurebox SQL issue...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
huntco@gmail.com - 05 Jul 2007 17:15 GMT
Need to grab an image off the disk, preview in a picturebox, then save
in SQL database for later viewing, etc.

Got the "grab/preview" part working fine, and *something* is being
stored in the SQL database, but when I go to retrieve the image and
place it in the picture box, the picture is blank.  SQL datatype is
image, and the tableadapter has an update query that accepts the image
field and primary key.  Image field is sent as a byte array.  Length
of the image in SQL is 2 bytes more than the file on disk.

So, in trying to debug....

I've found that if I do a picturebox.image.save("c:\saved.tif",TIFF)
it saves it just fine, and the resultant image file on disk is
readible.  But, if I do:

picturebox.image.save(memoryStream,TIFF)
memoryStream.read(byteArray,0,memoryStream.length)
system.io.file.writeAllBytes("c:\bytes.tif",byteArray)

the resultant file on disk is not a readible TIFF image.

...which makes me wonder how I'm supposed to convert BLOB data for
storage and retrieval in SQL.  I've read through many articles which
suggest that I should:

dim byteArr(imageContent.length)
imageContent.read(byteArr,0,imageContent.length)
tableAdapter.updateQuery(byteArr,primaryKey)

where imageContent is system.io.fileStream and is initialized to
system.io.file.openRead(filePathOnDisk).

And it *appears* to work - well, it stores *something* in the SQL
database, but whatever it's storing is illegible.  And when I try the
same series of steps but save to disk, it's also not recognizable as a
TIFF file.

Which takes me back to the question - how do I get BLOB data (TIFF or
other image/picture data) into SQL so that I can extract it back from
SQL and view it in a picturebox (VB.Net 2.x)?
Mark S. Milley, MCSD (BinarySwitch) - 05 Jul 2007 20:43 GMT
Hi Tracy -

Have you tried adding an additional column to the database to store
the original byte count? You can use that when you're converting the
data back.

In my experience, though, the best answer is to avoid database storage
of binary data alltogether. I've always run into strange quirks like
these.

I don't know anything about the problem domain/architecture you're
currently working with, but I've found that you're typically better
off saving the file outside of the database and accessing it via
either a web share, a shared folder, a webservice, or an RPC call.
Typically, on the server, I change the file names to match the primary
key of the row that it's referring to (typically a guid), and store
the original file name in the database for renaming on-the-fly later
(if desired). This avoids any filename collisions.

It will take a little more programming on your end to make sure that
the file system coincides with the database, but it will avoid these
kinds of bugs...

Good Luck,

-Mark
huntco@gmail.com - 05 Jul 2007 22:11 GMT
Kept banging my head against the wall, and here's what I came up with.

       Dim byteArr2(Me.imageContent.Length) As Byte
       ' convert stream to byte - the hard way, because the .read
and .write operations don't seem to work
       Dim aByte As Byte
       Dim x1 As Integer
       For x1 = 0 To Me.imageContent.Length - 1
           Me.imageContent.Seek(x1, IO.SeekOrigin.Begin)
           aByte = Me.imageContent.ReadByte
           byteArr2(x1) = aByte
       Next
       tableAdapter.updateImage(byteArr2, primaryKey)

That will get the data into SQL in a format that can be retrieved and
displayed.  Here's how to get it out:

       Dim aTable As myDataTable
       Dim aTableRow As myDataTableRow
       aTable=tableAdapter.getByPKEY(primaryKey)
       For Each aTableRow In aTable
           Dim aStream As New
System.IO.MemoryStream(aTableRow.imageColumn) ' <-- this was the trick
           Me.PictureBox1.Image = Image.FromStream(aStream)
       Next

I originally had:

dim aStream as system.io.stream
aStream=new system.io.memoryStream
aStream.write(aTableRow.imageColumn,0,aTableRow.imageColumn.length)

but that gave me "invalid parameter" or something on the
image.fromStream(aStream) line.

Anyone have any idea why it works one way and not the other?  And, is
that a bug, or a feature?  :)

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.