Imports HR.EZTwainLibrary
Imports System.Runtime.InteropServices

Public Class frmNewScan

    Private Sub btnSource_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSource.Click
        If EZTwain.IsAvailable Then             'if TWain services are available
            EZTwain.SelectTwainsource(Me.Handle) 'select Twain source
            Exit Sub
        Else
            MsgBox("Twain not avaialble.", MsgBoxStyle.Critical)
        End If
    End Sub
   
    Private Sub btnAcquire_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAcquire.Click

        Dim Result As Integer
        'Acquires a picture to a bitmap
        With My.Computer.FileSystem
           If .FileExists("C:\MyImage.Jpeg") Then
             .DeleteFile("C:\MyImage.Jpeg")
           End If
        End With
        Result = EZTwain.AcquireToFileName(Me.Handle, "C:\MyImage.Jpeg")

        If Result = 0 Then  'if ok loads the image into the picturebox
            PictureBox1.Image = Image.FromFile("C:\MyImage.Jpeg")
            Exit Sub
        Else
            MsgBox("An error occured while scanning the picture.", MsgBoxStyle.Critical)
        End If

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim pathfolder As String
        pathfolder = "\" + Trim(Main.txtempno.Text)
        Try
            If txtFileName.Text = "" Then
                MsgBox("Please Enter FileName of an Image.")
            Else
                Dim file_name As String = ("D:\pics" & pathfolder)
                file_name = file_name.Substring(0, file_name.LastIndexOf("\")) & pathfolder & "\" & txtFileName.Text

                Dim bm As Bitmap = PictureBox1.Image
                ' Save the picture as a JPEG
                bm.Save(file_name & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)
                txtFileName.Text = ""
                MsgBox("Ok")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

hi,

I'm new in vb.net. Somebody help me about this problem. this problem occurs when i scan an images for the second time. i think the error is my filename(C:\MyImage.jpeg). how can i have a different filename thru coding or how to delete the tempory filename of an image to make scan again.

All replies are appreciated.
thanks for helping me.

Recommended Answers

All 5 Replies

Line # 28 - Avoid direct use of file/resource.

Use following code to load image from the file.

Using ms As New IO.MemoryStream(IO.File.ReadAllBytes("C:\MyImage.Jpeg"))
            PictureBox1.Image = Image.FromStream(ms)
End Using

Line # 28 - Avoid direct use of file/resource.

Use following code to load image from the file.

Using ms As New IO.MemoryStream(IO.File.ReadAllBytes("C:\MyImage.Jpeg"))
            PictureBox1.Image = Image.FromStream(ms)
End Using

hi,

i copy your code and after scanning. I save it and it said "a generic error occurred in GDI+" whats the error?? thanks

hi,

i copy your code and after scanning. I save it and it said "a generic error occurred in GDI+" whats the error?? thanks

hi again,,

i found out that the error occurs when i delete the file (Line: 20 - 24) it said the process cannot access the file because it is being used by another process

Please help me!!
thanks

>a generic error occurred in GDI+

Cause of Error: Trying to access invalid path or Trying to save image for path that doesn't exist.

>a generic error occurred in GDI+

Cause of Error: Trying to access invalid path or Trying to save image for path that doesn't exist.

yes it said that the generic error occurred:

bm.Save(file_name & ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg)

my file does not have a extenison(.jpg)
what is the code to have a file extension for saving the images.

thanks thanks thanks..
BTW how to rank your comment??
thanks for helping me!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.