How to Upload Scanned Images to Google Drive With Dynamic .NET TWAIN

In this tutorial, let’s learn how to implement a simple application to scan documents with Dynamic .NET TWAIN and upload scanned images to Google Drive step by step.

upload image to google drive


  1. Download Dynamic .NET TWAIN SDK.
  2. Download virtual scanner if there is no real scanner connected.

Scan Documents

Launch Visual Studio and create a new project. Choose the template Windows Forms Application.

Right-click on References to add DynamicDotNetTWAIN.dll.

Drag components to layout:


Get all available sources:

            for (lngNum = 0; lngNum < dynamicDotNetTwain.SourceCount; lngNum++)

Scan and display documents:

                dynamicDotNetTwain.IfShowUI = false;
                dynamicDotNetTwain.IfDisableSourceAfterAcquire = true;
                dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_RGB;
                dynamicDotNetTwain.BitDepth = 24;
                dynamicDotNetTwain.Resolution = 300;
            catch (Dynamsoft.DotNet.TWAIN.TwainException exp)
                String errorstr = "";
                errorstr += "Error " + exp.Code + "\r\n" + "Description: " + exp.Message + "\r\nPosition: " + exp.TargetSite + "\r\nHelp: " + exp.HelpLink + "\r\n";

Upload Images to Google Drive

Setup a new project in the Developers Console.

developers console

Enable Drive API.

drive api

Click Credentials to create new client ID.

client id

Install the NuGet Package Manager in Visual Studio (Tools -> Extensions and Updates).


Run Package Manager Console, and type in Install-Package Google.Apis.Drive.v2 to install Drive API NuGet package. After installing successfully, check the References list.

Google references

Add the source code for uploading captured documents:

            Image image = dynamicDotNetTwain.GetImage(dynamicDotNetTwain.CurrentImageIndexInBuffer); // Get image data from memory
            string mimeType = "image/png";

            UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(
                new ClientSecrets
                    ClientId = " "CLIENT_ID_HERE",
                    ClientSecret = "CLIENT_SECRET_HERE",
                new[] { DriveService.Scope.Drive },

            // Create the service.
            var service = new DriveService(new BaseClientService.Initializer()
                HttpClientInitializer = credential,
                ApplicationName = "ScanUpload",

            File body = new File();
            body.Title = mFileTitle;
            body.Description = "image";
            body.MimeType = mimeType;

            ImageConverter imageConverter = new ImageConverter();
            byte[] byteArray = (byte[])imageConverter.ConvertTo(image, typeof(byte[])); // convert image to byte
            System.IO.MemoryStream stream = new System.IO.MemoryStream(byteArray);

            FilesResource.InsertMediaUpload request = service.Files.Insert(body, stream, mimeType);

Now, you can run the application and find the uploaded images in your Google Drive.

Google Drive

For more detailed information about Google Drive development, you can visit

Source Code

Search Blog Posts