Neodynamic ImageDraw 2.0 (or greater) for ASP.NET (WebControl)
Microsoft .NET Framework 2.0 (or greater)
Microsoft Visual Studio 2005 or Visual Web Developer 2005 Express Edition
In the following Step-By-Step Guide we're going to create a simple ASP.NET WebForm that lets users to upload image files which will be resized on-the-fly by Neodynamic ImageDraw component.
Follow these steps:
<h1>
Image Upload
</h1>
<p>
<asp:FileUpload ID="FileUpload1" runat="server" Width="338px" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="FileUpload1"
Display="Dynamic" ErrorMessage="Please specify an image file." SetFocusOnError="True"
ValidationGroup="Group1"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="FileUpload1"
ErrorMessage="Please specify an image file." SetFocusOnError="True" ValidationExpression="^.+\.((jpg)|(gif)|(jpeg)|(png)|(bmp))$"
Display="Dynamic" ValidationGroup="Group1"></asp:RegularExpressionValidator>
</p>
<p>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload, Resize and Save"
ValidationGroup="Group1" />
</p>
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If Me.IsValid AndAlso Me.FileUpload1.HasFile Then
'Create an ImageElement to wrap up the uploaded image
Dim uploadedImage As Neodynamic.WebControls.ImageDraw.ImageElement
uploadedImage = Neodynamic.WebControls.ImageDraw.ImageElement.FromBinary(Me.FileUpload1.FileBytes)
'Create Resize imaging action to apply on the uploaded image
'NOTE: You may apply any of the ImageDraw built-in imaging actions
Dim actResize As New Neodynamic.WebControls.ImageDraw.Resize()
actResize.Width = 150
actResize.LockAspectRatio = Neodynamic.WebControls.ImageDraw.LockAspectRatio.WidthBased
uploadedImage.Actions.Add(actResize)
'Composite the output image by using ImageDraw class
Dim imgDraw As New Neodynamic.WebControls.ImageDraw.ImageDraw()
'Add uploaded image
imgDraw.Elements.Add(uploadedImage)
'Output image settings...
'For example: save the image in JPEG format always
imgDraw.ImageFormat = Neodynamic.WebControls.ImageDraw.ImageDrawFormat.Jpeg
imgDraw.JpegCompressionLevel = 90
'Now, save the output image on disk
Dim fileName As String = "C:\Temp\" + System.IO.Path.GetFileNameWithoutExtension(Me.FileUpload1.FileName) + ".jpg"
imgDraw.Save(fileName)
End If
End Sub
protected void Button1_Click(object sender, EventArgs e)
{
if (this.IsValid && this.FileUpload1.HasFile)
{
//Create an ImageElement to wrap up the uploaded image
Neodynamic.WebControls.ImageDraw.ImageElement uploadedImage;
uploadedImage = Neodynamic.WebControls.ImageDraw.ImageElement.FromBinary(this.FileUpload1.FileBytes);
//Create Resize imaging action to apply on the uploaded image
//NOTE: You may apply any of the ImageDraw built-in imaging actions
Neodynamic.WebControls.ImageDraw.Resize actResize = new Neodynamic.WebControls.ImageDraw.Resize();
actResize.Width = 150;
actResize.LockAspectRatio = Neodynamic.WebControls.ImageDraw.LockAspectRatio.WidthBased;
uploadedImage.Actions.Add(actResize);
//Composite the output image by using ImageDraw class
Neodynamic.WebControls.ImageDraw.ImageDraw imgDraw = new Neodynamic.WebControls.ImageDraw.ImageDraw();
//Add uploaded image
imgDraw.Elements.Add(uploadedImage);
//Output image settings...
//For example: save the image in JPEG format always
imgDraw.ImageFormat = Neodynamic.WebControls.ImageDraw.ImageDrawFormat.Jpeg;
imgDraw.JpegCompressionLevel = 90;
//Now, save the output image on disk
string fileName = @"C:\Temp\" + System.IO.Path.GetFileNameWithoutExtension(this.FileUpload1.FileName) + ".jpg";
imgDraw.Save(fileName);
}
}
We provide best-in-class customer service and support directly from members of our dev team! If we are available when you contact us, you will get a response in few minutes; otherwise the maximum turnaround is 24hs in most cases.