Resize Images with Tridion & webcontrols

January 22, 2013 2 comments

1 .Create a .Net handler for resizing images


public class ImageResizer : IHttpHandler
{
int _width = 0;
int _height = 0;

public void ProcessRequest(HttpContext context)
{ 
Bitmap bitOutput;
Bitmap bitInput = GetImage(context);

if (SetHeightWidth(context, bitInput))
{ bitOutput = ResizeImage(bitInput, _width, _height); }
else { bitOutput = bitInput; }

context.Response.ContentType = "image/jpeg";
bitOutput.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);

return;
}

public bool IsReusable
{
get
{
return false;
}
}

/// <summary>
     /// Get the image requested via the query string. 
     /// </summary>
   public Bitmap GetImage(HttpContext context)
{

Bitmap bitOutput = null;

System.Net.WebRequest req = System.Net.WebRequest.Create(context.Request.QueryString["Image"]);
WebResponse response = req.GetResponse();
Stream stream = response.GetResponseStream();
bitOutput = new Bitmap(stream);
stream.Close();

return bitOutput;
}

/// <summary>
/// Set the height and width of the handler class.
/// </summary>
/// The context to get the query string parameters, typically current context. /// The bitmap that determines the /// <returns>True if image needs to be resized, false if original dimensions can be kept.</returns>
public bool SetHeightWidth(HttpContext context, Bitmap bitInput)
{
double inputRatio = Convert.ToDouble(bitInput.Width) / Convert.ToDouble(bitInput.Height);

if (!(String.IsNullOrEmpty(context.Request["width"])) && !(String.IsNullOrEmpty(context.Request["height"])))
{
_width = Int32.Parse(context.Request["width"]);
_height = Int32.Parse(context.Request["height"]);
return true;
}
else if (!(String.IsNullOrEmpty(context.Request["width"])))
{
_width = Int32.Parse(context.Request["width"]);
_height = Convert.ToInt32((_width / inputRatio));
return true;
}
else if (!(String.IsNullOrEmpty(context.Request["height"])))
{
_height = Int32.Parse(context.Request["height"]);
_width = Convert.ToInt32((_height * inputRatio));
return true;
}
else
{
_height = bitInput.Height;
_width = bitInput.Width;
return false;
}
}

/// <summary>
/// Resizes bitmap using high quality algorithms.
/// </summary>
/// 
private Bitmap ResizeImage(Bitmap originalImage, int newWidth, int newHeight)
{

Bitmap newImage = new Bitmap(originalImage, newWidth, newHeight);
Graphics g = Graphics.FromImage(newImage);
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
g.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.DrawImage(originalImage, 0, 0, newImage.Width, newImage.Height);
originalImage.Dispose();

return newImage;
}
}

Create a custom webcontrol that inherits from System.Web.UI.WebControls.Image & replace the image baseUrl to call you .net Handler. In this example my handler is located at /Services/ImageResizer.ashx

 public class CustomImage : System.Web.UI.WebControls.Image
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {
            base.ImageUrl = "/Services/ImageResizer.ashx?Image=" + HttpContext.Current.Request.Url.AbsoluteUri + "/" + base.ImageUrl + "&height=" + base.Height.ToString() + "&width=" + base.Width.ToString();
            base.Render(writer);
        }

    }

 

3. Than in you DWT you can use this code:

<Namespace:CustomImage src="@@Image@@" width="81" height="73" alt="@@Author@@" runat="server" />

 

 

Categories: Tridion, Uncategorized Tags: , ,

Create custom event handler

April 6, 2012 2 comments

1. Upload dll to cms server: D:\Program Files (x86)\Tridion\bin\

2. Add the green line in: D:\Program Files (x86)\Tridion\config\Tridion.ContentManager.config

   <extensions>
    <add assemblyFileName="D:\Program Files (x86)\Tridion\bin\Tridion.UGC.EventSystem.dll"/>
    <add assemblyFileName="D:\Program Files (x86)\Tridion\bin\Custom.Tridion.Events.dll"/>
   </extensions>
   

3. Restart Tridion Content Manager Service Host

Example:


    [TcmExtension("EventHandlersExtension")]
    public class EventHandlers : TcmExtension
    {
        public EventHandlers()
        {
            Subscribe();
        }

        public void Subscribe()
        {
             EventSystem.Subscribe<Component, SaveEventArgs>(SaveComponentEvent, EventPhases.TransactionCommitted);
        }

        private void SaveComponentEvent(Component subject, SaveEventArgs args, EventPhases phase)
        {
            Logging.Debug("Component Schema ID is " + subject.Schema.Id.ToString().ToUpper());
                if (subject.Schema.Id.ToString().ToUpper() == TridionConstants.GpdComponentSchemaId.ToUpper())
                {
                    Logging.Debug("Creating page");
                    Helpers.CreateInfoPage(subject, args, phase);
                }
        }
}
Categories: Tridion

Get link to page where component is added.

February 1, 2012 Leave a comment

This code can be used to get a link to a page where a component is added. It will return the first
page found.


string NavigateUrl;
   
ComponentLink ComponentLink = new ComponentLink(PublicationID);

   if (ComponentLink.GetLink(ComponentTcmID).IsResolved)
   {
      NavigateUrl = ComponentLink.GetLink(ComponentTcmID).Url;
   }
Categories: Linking API, Tridion

Create a sitemap with a Custom deployer

January 25, 2012 2 comments

This post is about creating a sitemap that will be updated after every page publish. At page publish a custom deployer will be trigered that
will the store page information into a custom table. We can then generate a custom sitemap provider that can be used to create a sitemap or
breadcrumb.

Read more…

TcmUploadAssembly Encrypted config file

January 19, 2012 Leave a comment

With this command we can create an encrypted config file to use with tcmupload (http://www.sdltridionworld.com/community/2011_extensions/AssemblyUploader2.aspx)


Run TcmUploadAssembly.exe from cmd to create an encrypted config file.

Categories: TcmUploadAssembly, Tridion