Click here to Skip to main content
6,926,479 members and growing! (11,638 online)
Email Password   helpLost your password?
Web Development » Custom Controls » General     Intermediate License: The Code Project Open License (CPOL)

Image Manager for Image Gallery

By Saifi Hasan

A smart custom control
Javascript, C#2.0, Windows, .NET2.0, ASP.NET, Visual-Studio, WebForms, Dev
Posted:17 May 2007
Views:33,154
Bookmarked:35 times
printPrint Friendly   add Share
      Discuss Discuss   Broken Article?Report  
10 votes for this article.
Popularity: 2.75 Rating: 2.75 out of 5
2 votes, 20.0%
1
1 vote, 10.0%
2

3
1 vote, 10.0%
4
6 votes, 60.0%
5

Screenshot - Example.jpg

Introduction

Many image gallery-type web applications provide features to change image attributes. This permits viewing of the image with various options, e.g. the user can rorate the image, zoom the image, change grayscale, mirror the image or increase/decrease its opacity. I have developed this smart custom control which lets you do all of these operations by selecting image attributes from the context menu.

Background

This idea came to me while looking at Silverlite Telerik controls for anination. The controls use Microsoft filters, which are IE-specific.

Using the code

Complete code and assemblies are attached. You can download the ImageControl.dll and add reference to their project. Once you put the control on your page, you can assign various properties in order to control the attribute set in context menu. Here are the properties exposed:

  • ImageUrl
  • ShowRotateLeft
  • ShowRotateRight
  • ShowGrayScale
  • ShowMirror
  • ShowOpaccity
  • ShowInvert
  • ShowZoomIn
  • ShowZoomOut
  • LeftRotateImage
  • RighttRotateImage
  • ZoomInImage
  • ZoomOutImage
  • ImageHeight
  • ImageWidth
  • ContextMenuTableClass
  • ContextMenuMouseOverClass
  • ContextMenuMouseOutClass

About the code

Step 1:

In order to put the Microsoft transition filters on the image, I overrode the CreateChildControls event and added a DIV/SPAN (HtmlGenericControl) tag, as well as the default filter attributes. Then I added Image Control inside the DIV control, plus an attribute for context menu.

protected override void CreateChildControls() 
{ 
    Controls.Clear(); 
    HtmlGenericControl ogen = new HtmlGenericControl(); 
    ogen.ID =this.ClientID + "_filterDIV"; 
    ogen.Attributes.Add("style", "position:relative; width:" + _ImageWidth 
        + "; height:" + _ImageHeight + "; background:gold; padding:2px; 
        border:1px solid black; 
        filter:progid:DXImageTransform.Microsoft.gradient(
        startColorstr='BLACK, endColorstr='yellowgreen'), 
        progid:DXImageTransform.Microsoft.BasicImage(
        Rotation=0,Mirror=0,Invert=0,XRay=0,Grayscale=0,Opacity=1.00,Mask=0),
        ;"); 
    System.Web.UI.WebControls.Image oImage 
        = new System.Web.UI.WebControls.Image(); 
    oImage.ID = "filterImage"; 
    oImage.AlternateText = ""; 
    if ( Height  !=0) 
        oImage.Height = Height; 
    if (Width != 0) 
        oImage.Width = Width; 
        oImage.ImageUrl = _ImageUrl; 
        _ImageClientID = oImage.ClientID; 
        ogen.Controls.Add(oImage); 
        this.Controls.Add(ogen); 

    string strScripr = "<script type='text/javascript' 
        language="'javascript'"> document.all(
        '" + oImage.ClientID + "').oncontextmenu = function() {
        dopopup(event.x,event.y,'"+_ContextmenuDivID+"');return false; 
        } </script>"; 
    this.Page.ClientScript.RegisterStartupScript(typeof(string),
        "RefisterEvent",strScripr); 
    base.CreateChildControls(); 
} 

Step 2:

The next step is to register the required JS file and CSS file. The common Javascript and default CSS are embeded resources, so on the PreRender of the control I registered them.

 protected override void OnPreRender(EventArgs e)
 {

    /*Add JS */ 
    string scriptUrl = Page.ClientScript.GetWebResourceUrl(
        this.GetType(), "ImageControl.ImgControl.js"); 
     this.Page.ClientScript.RegisterClientScriptInclude(typeof(string), 
        "ImageControlJS", scriptUrl); 

    /*Add CSS */ 
    string includeTemplate 
        = "<link rel='stylesheet' text='text/css' href='{0}' />"; 
    string includeLocation 
        = this.Page.ClientScript.GetWebResourceUrl(this.GetType(), 
        "ImageControl.ImageControl.css"); 
    LiteralControl include 
        = new LiteralControl(String.Format(includeTemplate,includeLocation)); 
    ((System.Web.UI.HtmlControls.HtmlHead)this.Page.Header).Controls.Add(
        include); 

    /*End Add CSS */ 
    base.OnPreRender(e); 
}

Step 3:

I created the Context menu using an HTML table -- default style as hidden -- based on the various properties selected, such as ShowRotateLeft/ShowRotateRight on the Render event. One more thing: since JS changes the transition filters based on attributes selected from the context menu, we have to change the transition filter corresponding to the selected image. That is why we have to give a unique ID to the DIV element. I have given a unique name from the onInit event and generated a unique ID using this.UniqueID+"_ContexuMenuDiv".

protected override void Render(HtmlTextWriter writer)
{
writer.WriteLine(
    "<div style=\"position:absolute;z-index:500; display:'none';\" id='" 
    + _ContextmenuDivID + "' onclick=\"this.style.display='none';\">"); 
            /*start writing Context Menu table */ 
            writer.WriteLine("<table class='"+_ContextMenuTableClass+"'>");    
            if(_ShowRotateLeft) 
                writer.WriteLine(
                    "<TR><TD ONMOUSEOVER
                    =\"this.className='Mover'\" ONMOUSEOUT
                    =\"className='MOut'\" onclick
                    =\"RotateLeft(
                    '"+this.ClientID+"_filterDIV');\">Rotate Left</TD></TR>"); 
            if (_ShowRotateRight) 
                writer.WriteLine(
                    "<TR><TD ONMOUSEOVER
                    =\"this.className='Mover'\" ONMOUSEOUT
                    =\"className='MOut'\" onclick
                    =\"RotateRight(
                    '" + this.ClientID + "_filterDIV');\">
                    Rotate Right</TD></TR>"); 
            if(_ShowGrayScale) 
                writer.WriteLine(
                    "<TR><TD ONMOUSEOVER
                        =\"this.className='Mover'\" ONMOUSEOUT
                        =\"className='MOut'\" onclick
                        =\"ChangeScale('grayscale',
                        '" + this.ClientID + "_filterDIV');\">
                        GrayScale</TD></TR>"); 
            if (_ShowInvert) 
                writer.WriteLine(
                "<TR><TD ONMOUSEOVER
                =\"this.className='Mover'\" ONMOUSEOUT
                =\"className='MOut'\" onclick
                =\"ChangeScale('invert',
                '" + this.ClientID + "_filterDIV');\">Invert</TD></TR>"); 
            if (_ShowMirror) 
                writer.WriteLine(
                "<TR><TD ONMOUSEOVER
                =\"this.className='Mover'\" ONMOUSEOUT
                =\"className='MOut'\" onclick=\"ChangeScale(
                'mirror','" + this.ClientID + "_filterDIV');\">
                Mirror</TD></TR>"); 
            if (_ShowOpaccity) 
            { 
                writer.WriteLine(
                    "<TR><TD ONMOUSEOVER=\"this.className='Mover'\" ONMOUSEOUT
                    =\"className='MOut'\" onclick=\"Opac(
                    1,'" + this.ClientID + "_filterDIV');\">
                    Increase Opacity</TD></TR>"); 
                writer.WriteLine(
                "<TR><TD ONMOUSEOVER=\"this.className='Mover'\" ONMOUSEOUT
                =\"className='MOut'\" onclick=\"Opac(
                2,'" + this.ClientID + "_filterDIV');\">
                Decrease Opacity</TD></TR>"); 
            } 
            if (_ShowZoomIn) 
                writer.WriteLine(
                   "<TR><TD ONMOUSEOVER=\"this.className='Mover'\" ONMOUSEOUT
                   =\"className='MOut'\" onclick=\"ZoomIn(
                   '" + _ImageClientID + "');\">Zoom In</TD></TR>"); 
            if (_ShowZoomOut) 
                writer.WriteLine(
                   "<TR><TD ONMOUSEOVER=\"this.className='Mover'\" ONMOUSEOUT
                   =\"className='MOut'\" onclick=\"ZoomOut(
                   '" + _ImageClientID + "');\">Zoom Out</TD></TR>"); 

             writer.WriteLine("</table>");    
            /*End writing Context Menu table */        
            writer.WriteLine("</div>"); 
            base.Render(writer); 
        } 
       protected override void  OnInit(EventArgs e)          { 
           _ContextmenuDivID = this.UniqueID+"_ContexuMenuDiv"; 
           base.OnInit(e); 
}

Points of interest

Microsift provides many filters and transitions for text, as well as images. However, to use them in ASPX page, you have to be good with Javascript and have some knowledge of these filters. By using this control, you don't have to know details about these filters; you just have to drag & drop the control.

History

  • 17 May, 2007 - Original version posted

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Saifi Hasan


Member
I have done Master Degree in Computers and MCAD and working on Microsoft technologie since last 4 yrs. Currently working with TCS (India).
Location: India India

Other popular Custom Controls articles:

 
Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 8 of 8 (Total in Forum: 8) (Refresh)FirstPrevNext
GeneralWhat about zooming an image within a listview? Pinmemberostrovia23:29 6 Jan '09  
Generalplease how to implement in the system Pinmembermaroq13:08 1 Jul '07  
GeneralRe: please how to implement in the system Pinmembersaifigr819:22 3 Jul '07  
GeneralI cant open the project using VS2003 PinmemberBalaji_rcs1:53 28 Jun '07  
GeneralRe: I cant open the project using VS2003 Pinmembersaifigr819:25 3 Jul '07  
GeneralGood one...!!! PinmemberNiiiissssshhhhhuuuuu0:34 18 May '07  
Generalgood one Pinmemberankitvaid21:22 17 May '07  
Generalworking example on-line now ? PinmemberBillWoodruff16:03 17 May '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.

PermaLink | Privacy | Terms of Use
Last Updated: 17 May 2007
Editor: Genevieve Sovereign
Copyright 2007 by Saifi Hasan
Everything else Copyright © CodeProject, 1999-2010
Web10 | Advertise on the Code Project