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.
Prerequisites
- Download Dynamic .NET TWAIN SDK.
- 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:
dynamicDotNetTwain.OpenSourceManager();
for (lngNum = 0; lngNum < dynamicDotNetTwain.SourceCount; lngNum++)
{
cmbSource.Items.Add(dynamicDotNetTwain.SourceNameItems(Convert.ToInt16(lngNum)));
}
Scan and display documents:
try
{
dynamicDotNetTwain.SelectSourceByIndex(Convert.ToInt16(cmbSource.SelectedIndex));
dynamicDotNetTwain.IfShowUI = false;
dynamicDotNetTwain.OpenSource();
dynamicDotNetTwain.IfDisableSourceAfterAcquire = true;
dynamicDotNetTwain.PixelType = Dynamsoft.DotNet.TWAIN.Enums.TWICapPixelType.TWPT_RGB;
dynamicDotNetTwain.BitDepth = 24;
dynamicDotNetTwain.Resolution = 300;
dynamicDotNetTwain.AcquireImage();
}
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";
MessageBox.Show(errorstr);
}
Upload Images to Google Drive
Setup a new project in the Developers Console.
Enable Drive API.
Click Credentials to create new 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.
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 },
"user",
CancellationToken.None).Result;
// 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);
request.Upload();
Now, you can run the application and find the uploaded images in your Google Drive.
For more detailed information about Google Drive development, you can visit https://developers.google.com/drive/web/quickstart/quickstart-cs.