Send comments on this topic. | Back to Introduction - All Topics | Help Version 15.12.21
Using the ImageViewer control on the server side.

Perform the following steps to create a project that uses the features of the LEADTOOLS ImageViewer Web Control from the server side:

  1. Start Visual Studio 2005.

  2. Choose File->New->Web Site… from the menu.

  3. In the New Web Site dialog box, choose "ASP.NET Web Site" in the Templates List, and choose either "Visual C#" or "Visual Basic" in the Language drop-down list box.

  4. In the Location drop-down list box choose "File System". Enter “C:\Inetpub\wwwroot\UsingImageViewer_Server” into the Project Name field, and then click OK.

    If desired, type a new location for your project or select a directory by clicking Browse, and then click OK.

  5. In the Solution Explorer pane, right-click on the project node, and select "Add Reference…" from the context menu. In the Add Reference dialog box, click the .NET tab and select "System.Windows.Forms". Click OK to close the Add Reference dialog box.

  6. Repeat the previous step but this time in the Add Reference dialog box select the Browse tab. Browse to the "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\Dotnet\Win32" folder and select the following DLL:

    • Leadtools.Web.dll.

    Now click OK to close the Add Reference dialog box.

  7. In the Solution Explorer pane right-click on the "Default.aspx" file and choose the View Designer from the context menu.

  8. Go to the toolbox (View->Toolbox) and drag and drop an instance of the ImageViewer onto the web form. If you do not have the ImageViewer in your toolbox, select "Tools->Choose Toolbox Items..." from the menu. Click Browse and then select "Leadtools.Web.dll" from the "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\Dotnet\Win32" folder and then click Open and then click OK.

  9. Go to the toolbox (View-Toolbox) and from the Standard tab drag and drop 7 Labels, 4 Text Boxes, 4 drop-down list boxes, and 3 Buttons onto the web form.

  10. Change the properties of the dropped controls as follows:

    Control Property New Value
    Label1
    • ID
    • Text
    • _lblImageUrl
    • Image Url
    TextBox1
    • ID
    • _tbImageUrl
    Label2
    • ID
    • Text
    • _lblImageWidth
    • Image Width
    TextBox2
    • ID
    • _tbImageWidth
    Label3
    • ID
    • Text
    • _lblImageHeight
    • Image Height
    TextBox3
    • ID
    • _tbImageHeight
    Button1
    • ID
    • Text
    • _btnGetImageSize
    • Get Image Size
    Button2
    • ID
    • Text
    • _btnViewImage
    • View Image
    Label4
    • ID
    • Text
    • _lblSizeMode
    • SizeMode
    DropDownList1
    • ID
    • AutoPostBack
    • _ddlSizeMode
    • True
    Label5
    • ID
    • Text
    • _lblScaleFactor
    • ScaleFactor
    DropDownList2
    • ID
    • AutoPostBack
    • _ddlScaleFactor
    • True
    Label6
    • ID
    • Text
    • _lblHorizontalAlignMode
    • HorizontalAlignMode
    DropDownList3
    • ID
    • AutoPostBack
    • _ddlHorizontalAlignMode
    • True
    Label7
    • ID
    • Text
    • _lblVerticalAlignMode
    • VerticalAlignMode
    DropDownList4
    • ID
    • AutoPostBack
    • _ddlVerticalAlignMode
    • True
    Button3
    • ID
    • Text
    • _btnGetRealScaleFactors
    • Get Real Scale Factors
    TextBox4
    • ID
    • _tbRealScaleFactors
  11. Switch to the "Default.aspx" code view (right-click default.aspx in the Solution Explorer, then select View Code), and add the following lines at the beginning of the file:

    Visual Basic

    
    Imports System.Windows.Forms
    Imports System.Web.Hosting
    Imports System.Xml
    Imports System.Drawing
    
    Imports System.IO
    Imports Leadtools.Web.Controls
    Imports Leadtools.Web.Handlers
    
    

    C#

    
    using System.Windows.Forms;
    using System.Web.Hosting;
    using System.Xml;
    using System.IO;
    using System.Drawing;
    
    using Leadtools.Web.Controls;
    using Leadtools.Web.Handlers;
    
  12. Update the Page_Load event handler to be as follows:

    Visual Basic

    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
       If (Not IsPostBack) Then
          ' Initialize the ImageViewer control.
          ImageViewer1.ResourcesPath = "Resources"
          ImageViewer1.ScrollBarSize = SystemInformation.VerticalScrollBarWidth
          ' Fill in the Combo Boxes
          BindDropDownListToEnum(_ddlSizeMode, GetType(ImageViewerSizeMode), ImageViewer1.SizeMode)
          BindDropDownListToEnum(_ddlHorizontalAlignMode, GetType(ImageViewerAlignMode), ImageViewer1.HorizontalAlignMode)
          BindDropDownListToEnum(_ddlVerticalAlignMode, GetType(ImageViewerAlignMode), ImageViewer1.VerticalAlignMode)
          Dim scaleFactors() As Integer = {1000, 800, 600, 400, 200, 100, 75, 50, 25, 10}
          Dim factor1 As Integer = CInt(ImageViewer1.ScaleFactor)
          For i As Integer = 0 To scaleFactors.Length - 1
             _ddlScaleFactor.Items.Add(scaleFactors(i).ToString())
             If (factor1 = scaleFactors(i)) Then
                _ddlScaleFactor.SelectedIndex = i
             End If
          Next
       End If
    End Sub
    

    C#

    
    protected void Page_Load(object sender, EventArgs e)
    {
       if(!IsPostBack)
       {
          // Initialize the ImageViewer control.
          ImageViewer1.ResourcesPath = "Resources";
          ImageViewer1.ScrollBarSize = SystemInformation.VerticalScrollBarWidth;
       
          // Fill in the Drop-down list boxes
          BindDropDownListToEnum(_ddlSizeMode, typeof(ImageViewerSizeMode), ImageViewer1.SizeMode);
          BindDropDownListToEnum(_ddlHorizontalAlignMode, typeof(ImageViewerAlignMode), ImageViewer1.HorizontalAlignMode);
          BindDropDownListToEnum(_ddlVerticalAlignMode, typeof(ImageViewerAlignMode), ImageViewer1.VerticalAlignMode);
       
          int[] scaleFactors = { 1000, 800, 600, 400, 200, 100, 75, 50, 25, 10 };
          int factor1 = (int)(ImageViewer1.ScaleFactor);
          for(int i = 0; i < scaleFactors.Length; i++)
          {
             _ddlScaleFactor.Items.Add(scaleFactors[i].ToString());
             if(factor1 == scaleFactors[i])
                _ddlScaleFactor.SelectedIndex = i;
          }
       }
    }
    
  13. double-click on the _btnGetImageSize, _btnViewImage, _ddlSizeMode, _ddlScaleFactor, _ddlHorizontalAlignMode _ddlVerticalAlignMode, and _btnGetRealScaleFactors controls and update the event handlers as follows:

    Visual Basic

    
    Protected Sub _btnGetImageSize_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _btnGetImageSize.Click
       Dim queryString As String = String.Format("url={0}", _tbImageUrl.Text)
       Dim sbResponse As StringWriter = New StringWriter()
       Dim wr As HttpWorkerRequest = New SimpleWorkerRequest("", queryString, sbResponse)
       Dim context As HttpContext = New HttpContext(wr)
       Dim ii As ImageInformation = New ImageInformation()
       ii.ProcessRequest(context)
       Dim xmlInfo As XmlDocument = New XmlDocument()
       If (sbResponse.ToString().Length > 0) Then
          xmlInfo.LoadXml(sbResponse.ToString())
          Dim width As XmlNode = xmlInfo.SelectSingleNode("descendant::Width")
          Dim height As XmlNode = xmlInfo.SelectSingleNode("descendant::Height")
          If (Not width Is Nothing) Then
             _tbImageWidth.Text = width.InnerXml
          End If
          If (Not height Is Nothing) Then
             _tbImageHeight.Text = height.InnerXml
          End If
       End If
    End Sub
    Protected Sub _btnViewImage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _btnViewImage.Click
       ImageViewer1.ImageSize = New Size(Convert.ToInt32(_tbImageWidth.Text), Convert.ToInt32(_tbImageHeight.Text))
       ImageViewer1.ImageUrl = _tbImageUrl.Text
    End Sub
    Protected Sub _ddlSizeMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ddlSizeMode.SelectedIndexChanged
       ImageViewer1.SizeMode = GetDropDownListSelectedItem(_ddlSizeMode, GetType(ImageViewerSizeMode))
    End Sub
    Protected Sub _ddlScaleFactor_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ddlScaleFactor.SelectedIndexChanged
       ImageViewer1.ScaleFactor = Convert.ToInt32(_ddlScaleFactor.SelectedItem.Value)
    End Sub
    Protected Sub _ddlHorizontalAlignMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ddlHorizontalAlignMode.SelectedIndexChanged
       ImageViewer1.HorizontalAlignMode = GetDropDownListSelectedItem(_ddlHorizontalAlignMode, GetType(ImageViewerAlignMode))
    End Sub
    Protected Sub _ddlVerticalAlignMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles _ddlVerticalAlignMode.SelectedIndexChanged
       ImageViewer1.VerticalAlignMode = GetDropDownListSelectedItem(_ddlVerticalAlignMode, GetType(ImageViewerAlignMode))
    End Sub
    Protected Sub _btnGetRealScaleFactors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles _btnGetRealScaleFactors.Click
       _tbRealScaleFactors.Text = String.Format("Horizontal:{0}, Vertical:{1}", ImageViewer1.HorizontalRealScaleFactor, ImageViewer1.VerticalRealScaleFactor)
    End Sub
    

    C#

    
    protected void _btnGetImageSize_Click(object sender, EventArgs e)
    {
       string queryString = string.Format("url={0}", _tbImageUrl.Text);
       StringWriter sbResponse = new StringWriter();
       HttpWorkerRequest wr = new SimpleWorkerRequest("", queryString, sbResponse);
       HttpContext context = new HttpContext(wr);
       ImageInformation ii = new ImageInformation();
       ii.ProcessRequest(context);
       XmlDocument xmlInfo = new XmlDocument();
       if(sbResponse.ToString().Length > 0)
       {
          xmlInfo.LoadXml(sbResponse.ToString());
          XmlNode width = xmlInfo.SelectSingleNode("descendant::Width");
          XmlNode height = xmlInfo.SelectSingleNode("descendant::Height");
          if(width != null)
          {
             _tbImageWidth.Text = width.InnerXml;
          }
       
          if(height != null)
          {
             _tbImageHeight.Text = height.InnerXml;
           }
       }
    }
    protected void _btnViewImage_Click(object sender, EventArgs e)
    {
       ImageViewer1.ImageSize = new Size(Convert.ToInt32(_tbImageWidth.Text), Convert.ToInt32(_tbImageHeight.Text));
       ImageViewer1.ImageUrl = _tbImageUrl.Text;
    }
    protected void _ddlSizeMode_SelectedIndexChanged(object sender, EventArgs e)
    {
      ImageViewer1.SizeMode = (ImageViewerSizeMode)GetDropDownListSelectedItem(_ddlSizeMode, typeof(ImageViewerSizeMode));
    }
    protected void _ddlScaleFactor_SelectedIndexChanged(object sender, EventArgs e)
    {
      ImageViewer1.ScaleFactor = Convert.ToInt32(_ddlScaleFactor.SelectedItem.Value);
    }
    protected void _ddlHorizontalAlignMode_SelectedIndexChanged(object sender, EventArgs e)
    {
      ImageViewer1.HorizontalAlignMode = (ImageViewerAlignMode)GetDropDownListSelectedItem(_ddlHorizontalAlignMode, typeof(ImageViewerAlignMode));
    }
    protected void _ddlVerticalAlignMode_SelectedIndexChanged(object sender, EventArgs e)
    {
      ImageViewer1.VerticalAlignMode = (ImageViewerAlignMode)GetDropDownListSelectedItem(_ddlVerticalAlignMode, typeof(ImageViewerAlignMode));
    }
    
    protected void _btnGetRealScaleFactors_Click(object sender, EventArgs e)
    {
       _tbRealScaleFactors.Text = string.Format("Horizontal:{0}, Vertical:{1}", ImageViewer1.HorizontalRealScaleFactor, ImageViewer1.VerticalRealScaleFactor);
    }
    
  14. Add the following functions to the end of the "Default.aspx.cs" file (for C#) or before the "End Class" statement (for Visual Basic):

    Visual Basic

    
    Private Sub BindDropDownListToEnum(ByVal ddl As DropDownList, ByVal t As Type, ByVal selected As Object)
       Dim a As Array = [Enum].GetValues(t)
       ddl.DataSource = a
       ddl.DataBind()
       Dim selectedValue As String = selected.ToString()
       For i As Integer = 0 To ddl.Items.Count - 1
          If (ddl.Items(i).Value = selectedValue) Then
             ddl.SelectedIndex = i
             Return
          End If
       Next
    End Sub
    Private Function GetDropDownListSelectedItem(ByVal ddl As DropDownList, ByVal t As Type) As Object
       Return [Enum].Parse(t, ddl.SelectedItem.Value)
    End Function
    

    C#

    
    private void BindDropDownListToEnum(DropDownList ddl, Type t, object selected)
    {
       Array a = Enum.GetValues(t);
       ddl.DataSource = a;
       ddl.DataBind();
       string selectedValue = selected.ToString();
       for(int i = 0; i < ddl.Items.Count; i++)
       {
          if(ddl.Items[i].Value == selectedValue)
          {
             ddl.SelectedIndex = i;
             return;
          }
       }
    }
    private object GetDropDownListSelectedItem(DropDownList ddl, Type t)
    {
       return Enum.Parse(t, ddl.SelectedItem.Value);
    }
    
  15. From the path "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Bin\Dotnet\Win32", copy the Leadtools.Codecs.Bmp, Leadtools.Codecs.Gif, Leadtools.Codecs.Png and Leadtools.Codecs.Cmp DLLs to the project's Bin folder.

  16. Inside the project folder create a folder named "Resources" and copy all of the java script files and images found in the "C:\Program Files\LEAD Technologies\LEADTOOLS 15\Examples\DotNet\Resources\Web" folder to it.

  17. Run the project and play with it as follows:

    • Type a path to one of the Internet Browser supported formats (gif, bmp, png or jpeg) in the ImageUrl TextBox.

    • Click GetImageSize to update the ImageWidth and ImageHeight Text Boxes by the image width and height.

    • Click ViewImage to load the image into the ImageViewer control.

    • Change the selected items in the drop-down list boxes and notice the differences in the displayed image.