Solved

Scan & Store

Posted on 2000-04-15
3
271 Views
Last Modified: 2010-05-18
Hi all,
I want to Scan documents from my VB application and store in my SQl server database  image field.
After scanning I am showing the image in image control , now how can I store picture from image control to database.
Can anyboby help me with VB code
for unbound ADO connection.
Tarun
0
Comment
Question by:tarun_ghosh
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Expert Comment

by:scj1
ID: 2723496
I am looking for the answer to a similar problem too...help! Incidently the GetChunk / AppendChunk methods which are almost definetely the mechanism for implementing this problem are only demonstrated using database to database operations in the MSDN. The problem here, is once the picture is in memory or even on disk - how do we go about inserting it into an SQL database via ADO....I assume the image type has something to do with it but I have yet to find a working example....

Thanks

StuJ
0
 
LVL 1

Accepted Solution

by:
scj1 earned 50 total points
ID: 2723860
I've found the answer to your problem....(and mine). Okay some of this code has been used from an answer back in 1999 which came from an example that originally used Oracle. This example connects to whatever open recordset you pass in, with the picture field name.

Method 1: SetPicture puts a picture into the database (this assumes a bitmap file - you can save your scanned image first if you want or modify the code to read the bytes from a IPictureDisp object)

Method 2: GetPicture retrieves the picture from the current record from a passed in recordset. It does use a temporary file to write out the picture object. It returns an IPictureDisp object.

The code:

Public Function setPicture(ByVal strPicturePath As String, strPicField As String, ByVal rs As ADODB.Recordset) As Boolean
  ' note: this requires the record to already exist - it will insert the
  ' picture at the current position in the recordset
  ' Returns true if success - false otherwise
  Const BlockSize = 100000
 
  Dim ByteData() As Byte   'Byte array for Blob data.
  Dim SourceFile As Integer
 
  ' Save Picture image to the table column.
  SourceFile = FreeFile
  Open strPicturePath For Binary Access Read As SourceFile
  FileLength = LOF(SourceFile)  ' Get the length of the file.
 
  If FileLength = 0 Then
    Close SourceFile
    setPicture = False
    Exit Function
  Else
    Numblocks = FileLength / BlockSize
    LeftOver = FileLength Mod BlockSize
    ReDim ByteData(LeftOver)
    Get SourceFile, , ByteData()
    rs(strPicField).AppendChunk ByteData()
    ReDim ByteData(BlockSize)
    For i = 1 To Numblocks
      Get SourceFile, , ByteData()
      rs(strPicField).AppendChunk ByteData()
    Next i
    rs.Update   'Commit the new data.
    Close SourceFile
    setPicture = True
  End If
End Function

Public Function getPicture(strPicField As String, ByVal rs As ADODB.Recordset) As IPictureDisp
  Const BlockSize = 100000
  Dim ByteData() As Byte   'Byte array for picture file.
  Dim DestFileNum As Integer
  Dim DiskFile As String

  ' Remove any existing destination file.
  DiskFile = App.Path & "\temp.bmp"
  If Len(Dir$(DiskFile)) > 0 Then
     Kill DiskFile
  End If
 
  DestFileNum = FreeFile
  FileLength = rs(strPicField).ActualSize
  Open DiskFile For Binary As DestFileNum
 
  Numblocks = FileLength / BlockSize
  LeftOver = FileLength Mod BlockSize
   
  rs.Move 0, adBookmarkCurrent
  ByteData() = rs(strPicField).GetChunk(LeftOver)
  Put DestFileNum, , ByteData()
 
  For i = 1 To Numblocks - 1
      ByteData() = rs(strPicField).GetChunk(BlockSize)
      Put DestFileNum, , ByteData()
  Next i
 
  Close DestFileNum

  Set getPicture = LoadPicture(App.Path & "\temp.bmp")
End Function
0
 

Author Comment

by:tarun_ghosh
ID: 2726454
Thanks saji,
It is working perfectly. Only constraint
is that I can't get pictures which are
already stored in different method.
Like imported from Access table.
Tarun
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
What are programming codes and how do they work? 7 119
jboss 7.1 start up error 1 85
Getting Variable not defined error in Python 1 85
Ruby or Python 7 134
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question