Click or drag to resize

HttpPost Class

This class allows can post image data to a server without user interaction.

Inheritance Hierarchy
SystemObject
  Atalasoft.TwainHttpPost

Namespace:  Atalasoft.Twain
Assembly:  Atalasoft.DotTwain (in Atalasoft.DotTwain.dll) Version: 11.0.0.0.0.297 (.NET 4.5.2, x86)
Syntax
public class HttpPost

The HttpPost type exposes the following members.

Constructors
  NameDescription
Public methodHttpPost
Creates a new instance of HttpPost.
Public methodHttpPost(ImageFormat)
Creates a new instance of HttpPost specifying encoder.
Top
Properties
  NameDescription
Public propertyFormData
Gets the FormDataCollection object that will be posted to the server.
Public propertySessionCookies
Gets or sets the session cookies to use for this transfer.
Public propertyTimeout
Gets or sets the seconds to wait before timing out on the transfer.
Top
Methods
  NameDescription
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetData
This method can be used to retrieve information from a server.
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodPostData
This method will send the internal FormDataCollection entries to a server for processing.
Public methodSetImageFormat(ImageFormat)
Public methodCode exampleSetImageFormat(String)
Sets the image format that will be used to post the Bitmap data in the FormDataCollection.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Top
Remarks

The data is posted as if it were being sent from an HTML Form, allowing any form handling component to be used on the server.

Examples
Posting an image (C#)
// Load the image we want to upload.
Bitmap bmp = new Bitmap(@"C:\myimage.jpg");

// We are going to save the image as JPEG on the server.
HttpPost post = new HttpPost(System.Drawing.Imaging.ImageFormat.Jpeg);
post.FormData.Add("image1", bmp, "myimage.jpg");

// Now lets add the additional information set by the client.
post.FormData.Add("comment", this.textComment.Text);
post.FormData.Add("author", this.textAuthor.Text);

// Upload the image and check the return value to see if it worked.
string retVal = post.PostData("http://www.mysite.com/handleupload.aspx");
if (retVal != "Success")
    MessageBox.Show(this, retVal, "Upload Error");

// Free the image.
bmp.Dispose();
Posting an image (Visual Basic)
' Load the image we want to upload.
Dim bmp As Bitmap = New Bitmap("C:\myimage.jpg")

' We are going to save the image as JPEG on the server.
Dim post As HttpPost = New HttpPost(System.Drawing.Imaging.ImageFormat.Jpeg)
post.FormData.Add("image1", bmp, "myimage.jpg")

' Now lets add the additional information set by the client.
post.FormData.Add("comment", Me.textComment.Text)
post.FormData.Add("author", Me.textAuthor.Text)

' Upload the image and check the return value to see if it worked.
Dim retVal As String = post.PostData("http://www.mysite.com/handleupload.aspx")
If retVal <> "Success" Then
    MessageBox.Show(this, retVal, "Upload Error")
End If

' Free the image.
bmp.Dispose()

This example will show you how to acquire an image from a web page and post it to the server.

Posting from a browser (JScript)
<!-- The main Acquisition object. -->
<object id="Twain" height="1" width="1" classid="Atalasoft.Twain.dll#Atalasoft.Twain.Acquisition" VIEWASTEXT>
</object>

<!-- Create an instance of the HttpPost object -->
<object id="PostIt" height="1" width="1" classid="Atalasoft.Twain.dll#Atalasoft.Twain.HttpPost" VIEWASTEXT>
</object>

...

<script type="text/javascript">
var count = 0;
function AcquireImage()
{
    // Acquire the image.
    var img = document.Twain.AcquireModal(false);
    if (img == null)
    {
        alert(&apos;Image was not acquired.&apos;);
        return;
    }

    // Send the image to the server.
    count++;
    var filename = &apos;image&apos; + count + &apos;.png&apos;;
    document.PostIt.FormData.AddFromBrowser(&apos;postImage&apos;, img, filename);

    var response = "";
    //response = document.PostIt.PostDataFromBrowser(&apos;http://www.atalasoft.com/Components/Twain/Demo/savepost.aspx&apos;);
    response = document.PostIt.PostData(&apos;http://localhost/TwainDemo/savepost.aspx&apos;);

    // Free the form data.
    document.PostIt.FormData.Clear();

    // Update the image.
    if (response.length > 0)
    {
        var el = document.getElementById(&apos;preview&apos;);
        el.src = response;
    } else {
        alert("PostDataFromBrowser failed");
    }
}
</script>
Examples
Posting an image (C#)
// Load the image we want to upload.
Bitmap bmp = new Bitmap(@"C:\myimage.jpg");

// We are going to save the image as JPEG on the server.
HttpPost post = new HttpPost(System.Drawing.Imaging.ImageFormat.Jpeg);
post.FormData.Add("image1", bmp, "myimage.jpg");

// Now lets add the additional information set by the client.
post.FormData.Add("comment", this.textComment.Text);
post.FormData.Add("author", this.textAuthor.Text);

// Upload the image and check the return value to see if it worked.
string retVal = post.PostData("http://www.mysite.com/handleupload.aspx");
if (retVal != "Success")
    MessageBox.Show(this, retVal, "Upload Error");

// Free the image.
bmp.Dispose();
Posting an image (Visual Basic)
' Load the image we want to upload.
Dim bmp As Bitmap = New Bitmap("C:\myimage.jpg")

' We are going to save the image as JPEG on the server.
Dim post As HttpPost = New HttpPost(System.Drawing.Imaging.ImageFormat.Jpeg)
post.FormData.Add("image1", bmp, "myimage.jpg")

' Now lets add the additional information set by the client.
post.FormData.Add("comment", Me.textComment.Text)
post.FormData.Add("author", Me.textAuthor.Text)

' Upload the image and check the return value to see if it worked.
Dim retVal As String = post.PostData("http://www.mysite.com/handleupload.aspx")
If retVal <> "Success" Then
    MessageBox.Show(this, retVal, "Upload Error")
End If

' Free the image.
bmp.Dispose()

This example will show you how to acquire an image from a web page and post it to the server.

Posting from a browser (JScript)
<!-- The main Acquisition object. -->
<object id="Twain" height="1" width="1" classid="Atalasoft.Twain.dll#Atalasoft.Twain.Acquisition" VIEWASTEXT>
</object>

<!-- Create an instance of the HttpPost object -->
<object id="PostIt" height="1" width="1" classid="Atalasoft.Twain.dll#Atalasoft.Twain.HttpPost" VIEWASTEXT>
</object>

...

<script type="text/javascript">
var count = 0;
function AcquireImage()
{
    // Acquire the image.
    var img = document.Twain.AcquireModal(false);
    if (img == null)
    {
        alert(&apos;Image was not acquired.&apos;);
        return;
    }

    // Send the image to the server.
    count++;
    var filename = &apos;image&apos; + count + &apos;.png&apos;;
    document.PostIt.FormData.AddFromBrowser(&apos;postImage&apos;, img, filename);

    var response = "";
    //response = document.PostIt.PostDataFromBrowser(&apos;http://www.atalasoft.com/Components/Twain/Demo/savepost.aspx&apos;);
    response = document.PostIt.PostData(&apos;http://localhost/TwainDemo/savepost.aspx&apos;);

    // Free the form data.
    document.PostIt.FormData.Clear();

    // Update the image.
    if (response.length > 0)
    {
        var el = document.getElementById(&apos;preview&apos;);
        el.src = response;
    } else {
        alert("PostDataFromBrowser failed");
    }
}
</script>
See Also

Reference

FormDataCollection Class
HttpPost Class