none
Scanner programming in C# RRS feed

  • Question

  • I am creating a project in C#, using ado.net, i want to scan pictures from a scanner and view it in a picturebox then save it to a sqlserver database and select a picture from the database and view it in the picturebox.

    thanks

    Wednesday, October 24, 2012 12:45 PM

Answers

  • It's a fairly simple thing to do once you have an image file. Getting that image file from your scanner may be a little trickier. From the information you gave, I'd be inclined to setup a hotfolder so that when the scanner created the file on disc, a background process could launch an app to allow you save/view images from the database. 

    This code here is a basic outline of how you would persist an image to database:

    public static void PerisitImage(string path, IDbConnection connection)
        {
            using (var command = connection.CreateCommand ())
            {
                Image img = Image.FromFile (path);
                MemoryStream tmpStream = new MemoryStream();
                img.Save (tmpStream, ImageFormat.Png); // change to other format
                tmpStream.Seek (0, SeekOrigin.Begin);
                byte[] imgBytes = new byte[MAX_IMG_SIZE];
                tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);
    
                command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
                IDataParameter par = command.CreateParameter();
                par.ParameterName = "payload";
                par.DbType = DbType.Binary;
                par.Value = imgBytes;
                command.Parameters.Add(par);
                command.ExecuteNonQuery ();
            }
        }
    However, it assumes basic knowledge of ADO.NET.

    Thursday, October 25, 2012 12:10 AM
  • If you want a complete example of doing all these things, your best bet is to either request it here:

    http://code.msdn.microsoft.com/site/requests

    Or break it down and use ye olde search tool...

    A search for "wpf scanner" brings up all the sample projects you need:

    http://www.codeproject.com/Articles/171666/Twain-for-WPF-Applications-Look-Ma-No-Handles

    http://channel9.msdn.com/coding4fun/blog/Scanner-access-with-WIA-via-WPF

    I'm sure none of the projects you find cover everything in one example, but I'm also sure you are smart enough to pull them all together.

    Regards,
    Pete


    #PEJL


    Thursday, October 25, 2012 5:44 PM
    Moderator
  • Tuesday, October 30, 2012 7:10 AM
  • The first answer in this tackOverflow thread shows how you could create a Windows service (which runs in the background) monitoring a specific folder. You would need to ensure that scanner files are only saved to a specific directory you're monitoring. 

    With the service running, when a new file is created in the directory (someone has saved a scanner image there), the folder monitor will see that, identity the new file(s) and then could pop up a Windows Forms or WPF application that would allow you to save that file into a database, or view other database files that were previously saved.

    Again, this is just one in a myriad of different ways you could set this up. In my opinion (since I've done it in the past) this method seems to be the easiest and most flexible.

    http://stackoverflow.com/questions/3612311/monitoring-a-folder-for-new-file-creation-without-using-filesystemwatcher-in-ne

    Tuesday, October 30, 2012 5:28 PM

All replies

  • Hi,

    What is your question? Your post only contains requirements for the application to be developed but that's it. I think most of us here won't develop your entire application for you based on your specs.

    Give it a go for yourself! Come back here if you run into problems...

    Kind regards.


    Please mark the best replies as answers - Twitter: @rickvdbosch - Blog: http://bloggingabout.net/blogs/rick

    Wednesday, October 24, 2012 1:05 PM
  • I dont know how to view the image from the scanner in picturebox? and how to save it to a sqlserver database?

    thanks

    Wednesday, October 24, 2012 11:18 PM
  • Hi,

    do you want to aquire the image from a scanner, or do you have the image from the scanner already saved to disc and now want to display it in a picturebox?

    Regards,

      Thorsten

    Wednesday, October 24, 2012 11:29 PM
  • It's a fairly simple thing to do once you have an image file. Getting that image file from your scanner may be a little trickier. From the information you gave, I'd be inclined to setup a hotfolder so that when the scanner created the file on disc, a background process could launch an app to allow you save/view images from the database. 

    This code here is a basic outline of how you would persist an image to database:

    public static void PerisitImage(string path, IDbConnection connection)
        {
            using (var command = connection.CreateCommand ())
            {
                Image img = Image.FromFile (path);
                MemoryStream tmpStream = new MemoryStream();
                img.Save (tmpStream, ImageFormat.Png); // change to other format
                tmpStream.Seek (0, SeekOrigin.Begin);
                byte[] imgBytes = new byte[MAX_IMG_SIZE];
                tmpStream.Read (imgBytes, 0, MAX_IMG_SIZE);
    
                command.CommandText = "INSERT INTO images(payload) VALUES (:payload)";
                IDataParameter par = command.CreateParameter();
                par.ParameterName = "payload";
                par.DbType = DbType.Binary;
                par.Value = imgBytes;
                command.Parameters.Add(par);
                command.ExecuteNonQuery ();
            }
        }
    However, it assumes basic knowledge of ADO.NET.

    Thursday, October 25, 2012 12:10 AM
  • Hi,

    I want to take the image from the scanner to picturebox. no files are used because i want the project to work in any PC ( users PC ).

    If i use a file on the disc a problem about the path of that file would be appeared when the program

    work on another PC.

    thanks.

    Thursday, October 25, 2012 5:32 PM
  • If you want a complete example of doing all these things, your best bet is to either request it here:

    http://code.msdn.microsoft.com/site/requests

    Or break it down and use ye olde search tool...

    A search for "wpf scanner" brings up all the sample projects you need:

    http://www.codeproject.com/Articles/171666/Twain-for-WPF-Applications-Look-Ma-No-Handles

    http://channel9.msdn.com/coding4fun/blog/Scanner-access-with-WIA-via-WPF

    I'm sure none of the projects you find cover everything in one example, but I'm also sure you are smart enough to pull them all together.

    Regards,
    Pete


    #PEJL


    Thursday, October 25, 2012 5:44 PM
    Moderator
  • Hi Nate Greenwood,

    thank you very much,

    How to get an imagefile from the scanner considering that the projet will be used by another PCs.

    thanks

    Saturday, October 27, 2012 9:03 AM
  • Tuesday, October 30, 2012 7:10 AM
  • The first answer in this tackOverflow thread shows how you could create a Windows service (which runs in the background) monitoring a specific folder. You would need to ensure that scanner files are only saved to a specific directory you're monitoring. 

    With the service running, when a new file is created in the directory (someone has saved a scanner image there), the folder monitor will see that, identity the new file(s) and then could pop up a Windows Forms or WPF application that would allow you to save that file into a database, or view other database files that were previously saved.

    Again, this is just one in a myriad of different ways you could set this up. In my opinion (since I've done it in the past) this method seems to be the easiest and most flexible.

    http://stackoverflow.com/questions/3612311/monitoring-a-folder-for-new-file-creation-without-using-filesystemwatcher-in-ne

    Tuesday, October 30, 2012 5:28 PM