kbakst
asked on
Store/view a scanned document?
What might be the best way to store and view a scanned document with Visual Basic? I have a SQL 7.0 server, so I suppose I could store it in a binary field in a table - or would it be better to just have a pointer to a file on disc? How to actually get it in there? How about viewing the document on screen?
Multiple questions deserves extra points - setting this one to 400.
Kelly
Multiple questions deserves extra points - setting this one to 400.
Kelly
I think i saw an example like this at http://lightning.prohosting.com/~shell123 if i remember right it was in the sniplets section or could have been in the full applications section
spam
ameba, spam? What are you talking about?
Cheers!
Cheers!
ASKER
For ameba - so I should store the actual pictures on disc? Why is this better than storing it in the DB? What is the advantage of a usercontrol over a picturebox? Are there good 3rd party controls for viewing pictures that I should consider?
ASKER
sdland1 posted spam as a comment.
Oh I get it... sdland1 profile: "Senior VB developer For LandSoft Ent" and the weblink is to his site... Self promotion, but maybe not spam... I took a look at the VB code library there and there do seem to be alot of items there.
But, I didn't see anything specific about this question...
Back to this question, alot of experts here agree that storing images in databases is not a good idea. Storing the pointers in a database and actually loading the image from a file on disk is supposed to be faster...
Cheers!
But, I didn't see anything specific about this question...
Back to this question, alot of experts here agree that storing images in databases is not a good idea. Storing the pointers in a database and actually loading the image from a file on disk is supposed to be faster...
Cheers!
>LandSoft
Mentioning your own product or site is *OK* if it directly solves the problem. If it says - go through/search sections and you will MAYBE find something, then it is spam.
I think it is OK to have some promotion for top experts who contributed a lot to this community. But this is not the case.
Mentioning your own product or site is *OK* if it directly solves the problem. If it says - go through/search sections and you will MAYBE find something, then it is spam.
I think it is OK to have some promotion for top experts who contributed a lot to this community. But this is not the case.
kbakst
>so I should store the actual pictures on disc?
Yes. Store only filename in your DB
>Why is this better than storing it in the DB?
Faster access. File System is the best database for storing files. Easier backup, easier admin work, easier storing on multiple servers...
>What is the advantage of a usercontrol over a picturebox?
Hidden code complexity. But picturebox will also work:
http://www.vb-helper.com/HowTo/scrwin.zip
>Are there good 3rd party controls for viewing pictures that I should consider?
I think it is better to create your own, so you will have its source.
>so I should store the actual pictures on disc?
Yes. Store only filename in your DB
>Why is this better than storing it in the DB?
Faster access. File System is the best database for storing files. Easier backup, easier admin work, easier storing on multiple servers...
>What is the advantage of a usercontrol over a picturebox?
Hidden code complexity. But picturebox will also work:
http://www.vb-helper.com/HowTo/scrwin.zip
>Are there good 3rd party controls for viewing pictures that I should consider?
I think it is better to create your own, so you will have its source.
You can use the code below to Read and Write Images in DB :
-- Add the Remote Data Objects Reference to your project (this code uses RDO, But you can change the code to use ADO also.)
-- Run the script in your DB to create the test table :
CREATE TABLE dbo.ImgTable (
Img_ID int NOT NULL ,
Img_Description varchar (40) NULL ,
Img_Bitmap image NULL
)
-- Picture1 is your scanned image.
Dim en As rdoEnvironment
Dim Cn As rdoConnection
Dim Rs As rdoResultset
Dim sql As String
Dim Fl As Long, Chunks As Integer
Dim Fragment As Integer, Chunk() As Byte, I As Integer
Const ChunkSize As Integer = 16384
Private Sub Form_Load()
Set en = rdoEnvironments(0)
Set Cn = en.OpenConnection(dsName:= "", _
Prompt:=rdDriverNoPrompt, _
Connect:="uid=sa;pwd=;driv er={SQL Server};" _
& "server=YOURSERVERNAME;dat abase=YOUR DB;")
End Sub
Private Sub ReadFromDB_Click()
sql = "Select * from ImgTable where Img_ID = " & InputBox("Enter the Image ID :", "Load Image from DB")
Set Rs = Cn.OpenResultset(sql, rdOpenStatic, rdConcurReadOnly, rdExecDirect)
If Rs.EOF Then
MsgBox "Can't find Image by that ID"
Exit Sub
End If
Open "pictemp" For Binary Access Write As 1
Fl = Rs!Img_Bitmap.ColumnSize
Chunks = Fl \ ChunkSize
Fragment = Fl Mod ChunkSize
ReDim Chunk(Fragment)
Chunk() = Rs!Img_Bitmap.GetChunk(Fra gment)
Put 1, , Chunk()
For I = 1 To Chunks
ReDim Buffer(ChunkSize)
Chunk() = Rs!Photo.GetChunk(ChunkSiz e)
Put 1, , Chunk()
Next
Close 1
Picture1.Picture = LoadPicture("pictemp")
Kill "pictemp"
End Sub
Private Sub SaveToDB_Click()
Dim Img_ID As Long
Img_ID = InputBox("Enter the Image ID :", "Save Image to DB")
sql = "Select * from ImgTable where Img_ID = " & Img_ID
Set Rs = Cn.OpenResultset(sql, rdOpenDynamic, rdConcurValues, rdExecDirect)
If Rs.EOF Then
Rs.AddNew
Rs!Img_ID = Img_ID
Description = InputBox("Describe the picture", "Save Image to DB")
Rs!Img_Description = Description
Else
Rs.Edit
End If
SavePicture Picture1.Picture, "pictemp"
Open "pictemp" For Binary Access Read As 1
Fl = LOF(1) ' Length of data in file
If Fl = 0 Then Close 1: Exit Sub
Chunks = Fl \ ChunkSize
Fragment = Fl Mod ChunkSize
Rs!Img_Bitmap.AppendChunk Null
ReDim Chunk(Fragment)
Get 1, , Chunk()
Rs!Img_Bitmap.AppendChunk Chunk()
ReDim Chunk(ChunkSize)
For I = 1 To Chunks
Get 1, , Chunk()
Rs!Img_Bitmap.AppendChunk Chunk()
Next
Close 1
Rs.Update
Rs.Close
Set Rs = Nothing
Kill "pictemp"
End Sub
-- Add the Remote Data Objects Reference to your project (this code uses RDO, But you can change the code to use ADO also.)
-- Run the script in your DB to create the test table :
CREATE TABLE dbo.ImgTable (
Img_ID int NOT NULL ,
Img_Description varchar (40) NULL ,
Img_Bitmap image NULL
)
-- Picture1 is your scanned image.
Dim en As rdoEnvironment
Dim Cn As rdoConnection
Dim Rs As rdoResultset
Dim sql As String
Dim Fl As Long, Chunks As Integer
Dim Fragment As Integer, Chunk() As Byte, I As Integer
Const ChunkSize As Integer = 16384
Private Sub Form_Load()
Set en = rdoEnvironments(0)
Set Cn = en.OpenConnection(dsName:=
Prompt:=rdDriverNoPrompt, _
Connect:="uid=sa;pwd=;driv
& "server=YOURSERVERNAME;dat
End Sub
Private Sub ReadFromDB_Click()
sql = "Select * from ImgTable where Img_ID = " & InputBox("Enter the Image ID :", "Load Image from DB")
Set Rs = Cn.OpenResultset(sql, rdOpenStatic, rdConcurReadOnly, rdExecDirect)
If Rs.EOF Then
MsgBox "Can't find Image by that ID"
Exit Sub
End If
Open "pictemp" For Binary Access Write As 1
Fl = Rs!Img_Bitmap.ColumnSize
Chunks = Fl \ ChunkSize
Fragment = Fl Mod ChunkSize
ReDim Chunk(Fragment)
Chunk() = Rs!Img_Bitmap.GetChunk(Fra
Put 1, , Chunk()
For I = 1 To Chunks
ReDim Buffer(ChunkSize)
Chunk() = Rs!Photo.GetChunk(ChunkSiz
Put 1, , Chunk()
Next
Close 1
Picture1.Picture = LoadPicture("pictemp")
Kill "pictemp"
End Sub
Private Sub SaveToDB_Click()
Dim Img_ID As Long
Img_ID = InputBox("Enter the Image ID :", "Save Image to DB")
sql = "Select * from ImgTable where Img_ID = " & Img_ID
Set Rs = Cn.OpenResultset(sql, rdOpenDynamic, rdConcurValues, rdExecDirect)
If Rs.EOF Then
Rs.AddNew
Rs!Img_ID = Img_ID
Description = InputBox("Describe the picture", "Save Image to DB")
Rs!Img_Description = Description
Else
Rs.Edit
End If
SavePicture Picture1.Picture, "pictemp"
Open "pictemp" For Binary Access Read As 1
Fl = LOF(1) ' Length of data in file
If Fl = 0 Then Close 1: Exit Sub
Chunks = Fl \ ChunkSize
Fragment = Fl Mod ChunkSize
Rs!Img_Bitmap.AppendChunk Null
ReDim Chunk(Fragment)
Get 1, , Chunk()
Rs!Img_Bitmap.AppendChunk Chunk()
ReDim Chunk(ChunkSize)
For I = 1 To Chunks
Get 1, , Chunk()
Rs!Img_Bitmap.AppendChunk Chunk()
Next
Close 1
Rs.Update
Rs.Close
Set Rs = Nothing
Kill "pictemp"
End Sub
ASKER
I guess the question I'd have to ask for this answer is: why do YOU think it's better to store in the database? If you can't come up with a reason, I'd have to give the credit to Ameba - even thought his name should be spelled Amoeba. :)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This works for me. 800 points from me to Ameba in 2 days. Worth every bit.
Kelly
Kelly
Thanks :)
?!
Listview on the left side, usercontrol (or picturebox with scrollbars) on the right side.
See MS Exchange 'Fax Viewer' for ideas.
Listview is with thumbnails (e.g. size 54 x 71 pixels).
thumbnails are saved to DB as binary type, real bitmaps are on disk.
When form loads you can load 100 or more thumbnails to listview without any problem. When user clicks on thumbnail, i.e. icon in your listview, you load real bitmap from disk and show it.
It is also good idea to have categories for your documents and organize folders. You'll need second DB table for this.
(IOW, don't keep 20000 pictures in one single folder :)