S
Simon Verona
I'm using wia in a vb.net project to capture images from a scanner.. This
works fine.
However, I want to control the way that images are scanned - ie B+W,
Greyscale and resolution etc, but can't work out how to. My code for
scanning is below (sorry it's a bit messy - it's been chopped and changed
around a lot)... There seems to be very little documentation on WIA, can
anybody help me with this ???
Code is :
Private Sub Getscanner()
' Setup Scanning and get scanner
txtStatus.Text = "Connecting to Scanner/Camera"
Try
wiadev = wia.ShowSelectDevice()
Catch ex As Exception
txtStatus.Text = "Could not connect to scanner/camera"
Exit Sub
End Try
Dim str As WIA.Property
For Each str In wiadev.Properties
Try
Debug.WriteLine("Property : " & str.Name & " = " & CStr(str.Value))
Catch
End Try
Next
txtStatus.Text = "Connected to Scanner/camera"
End Sub
Private Sub CaptureImages()
Dim FileName As String
Dim count As Int32
Dim tmp As Object
Dim image As Image
Dim gr As System.Drawing.Graphics
Dim prop As System.Drawing.Imaging.PropertyItem
Dim scale As Decimal
Dim tmpfilename As String
Dim Cb As System.Drawing.Image.GetThumbnailImageAbort = New
System.Drawing.Image.GetThumbnailImageAbort(AddressOf AbortImage)
Dim ImageCount As Int32
Getscanner()
txtStatus.Text = "Getting Images.."
'Try
' wiaPics = wiaRoot.Children. 'GetItemsFromUI(WIALib.WiaFlag.UseCommonUI,
WIALib.WiaIntent.ImageTypeColor)
'Catch ex As Exception
' MsgBox(ex.Message)
' Exit Sub
' End Try
Dim WiaPics As WIA.ItemClass
Dim WiaItem1 As WIA.ItemsClass
Dim wiaitem As WIA.ItemClass
Dim WiaImage As WIA.ImageFileClass
WiaItem1 = wiadev.Items
ImageCount = WiaItem1.Count
Dim offset As Int32 = 0
' Try
For count = 1 To WiaItem1.Count
wiaitem = WiaItem1.Item(count)
'wiaItem = System.Runtime.InteropServices.Marshal.CreateWrapperOfType(tmp,
GetType(WIALib.ItemClass))
Do
FileName = DirName & "\img" & Format(count + offset, "0000") & ".jpg"
If System.IO.File.Exists(FileName) Then offset = offset + 1
Loop Until System.IO.File.Exists(FileName) = False
txtStatus.Text = "Getting Image - " & count & " of " & ImageCount
Application.DoEvents()
tmpfilename = System.IO.Path.GetTempFileName
System.IO.File.Delete(tmpfilename)
Try
tmp = wiaitem.Transfer()
tmp.SaveFile(tmpfilename)
' tmp.Transfer(tmpfilename, False)
'System.Runtime.InteropServices.Marshal.ReleaseComObject(wiaItem)
image = System.Drawing.Bitmap.FromFile(tmpfilename)
Try
System.IO.File.Delete(DirName & "\tmppic" & count & ".jpg")
Catch ex As Exception
'txtstatus.Text = "Error deleting file : " & Err.Description
' Application.DoEvents()
End Try
' If image.Width > 500 Then
'scale = 500 / image.Width
'image = image.GetThumbnailImage(500, Int(image.Height * scale), Cb,
IntPtr.Zero)
'End If
PictureBox1.Image = image
image.Save(FileName)
image = Nothing
RaiseEvent ImageLoaded()
Catch ex As Exception
'MsgBox(ex.Message)
End Try
' Catch
' 'txtstatus.Text = "Failed..."
' End Try
Next
Me.Close()
End Sub
----
Many thanks in advance
Simon
works fine.
However, I want to control the way that images are scanned - ie B+W,
Greyscale and resolution etc, but can't work out how to. My code for
scanning is below (sorry it's a bit messy - it's been chopped and changed
around a lot)... There seems to be very little documentation on WIA, can
anybody help me with this ???
Code is :
Private Sub Getscanner()
' Setup Scanning and get scanner
txtStatus.Text = "Connecting to Scanner/Camera"
Try
wiadev = wia.ShowSelectDevice()
Catch ex As Exception
txtStatus.Text = "Could not connect to scanner/camera"
Exit Sub
End Try
Dim str As WIA.Property
For Each str In wiadev.Properties
Try
Debug.WriteLine("Property : " & str.Name & " = " & CStr(str.Value))
Catch
End Try
Next
txtStatus.Text = "Connected to Scanner/camera"
End Sub
Private Sub CaptureImages()
Dim FileName As String
Dim count As Int32
Dim tmp As Object
Dim image As Image
Dim gr As System.Drawing.Graphics
Dim prop As System.Drawing.Imaging.PropertyItem
Dim scale As Decimal
Dim tmpfilename As String
Dim Cb As System.Drawing.Image.GetThumbnailImageAbort = New
System.Drawing.Image.GetThumbnailImageAbort(AddressOf AbortImage)
Dim ImageCount As Int32
Getscanner()
txtStatus.Text = "Getting Images.."
'Try
' wiaPics = wiaRoot.Children. 'GetItemsFromUI(WIALib.WiaFlag.UseCommonUI,
WIALib.WiaIntent.ImageTypeColor)
'Catch ex As Exception
' MsgBox(ex.Message)
' Exit Sub
' End Try
Dim WiaPics As WIA.ItemClass
Dim WiaItem1 As WIA.ItemsClass
Dim wiaitem As WIA.ItemClass
Dim WiaImage As WIA.ImageFileClass
WiaItem1 = wiadev.Items
ImageCount = WiaItem1.Count
Dim offset As Int32 = 0
' Try
For count = 1 To WiaItem1.Count
wiaitem = WiaItem1.Item(count)
'wiaItem = System.Runtime.InteropServices.Marshal.CreateWrapperOfType(tmp,
GetType(WIALib.ItemClass))
Do
FileName = DirName & "\img" & Format(count + offset, "0000") & ".jpg"
If System.IO.File.Exists(FileName) Then offset = offset + 1
Loop Until System.IO.File.Exists(FileName) = False
txtStatus.Text = "Getting Image - " & count & " of " & ImageCount
Application.DoEvents()
tmpfilename = System.IO.Path.GetTempFileName
System.IO.File.Delete(tmpfilename)
Try
tmp = wiaitem.Transfer()
tmp.SaveFile(tmpfilename)
' tmp.Transfer(tmpfilename, False)
'System.Runtime.InteropServices.Marshal.ReleaseComObject(wiaItem)
image = System.Drawing.Bitmap.FromFile(tmpfilename)
Try
System.IO.File.Delete(DirName & "\tmppic" & count & ".jpg")
Catch ex As Exception
'txtstatus.Text = "Error deleting file : " & Err.Description
' Application.DoEvents()
End Try
' If image.Width > 500 Then
'scale = 500 / image.Width
'image = image.GetThumbnailImage(500, Int(image.Height * scale), Cb,
IntPtr.Zero)
'End If
PictureBox1.Image = image
image.Save(FileName)
image = Nothing
RaiseEvent ImageLoaded()
Catch ex As Exception
'MsgBox(ex.Message)
End Try
' Catch
' 'txtstatus.Text = "Failed..."
' End Try
Next
Me.Close()
End Sub
----
Many thanks in advance
Simon