<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Custom Server Controls</title><link>http://forums.asp.net/19.aspx</link><description>All about building ASP.NET server controls. &lt;a href="http://aspadvice.com/SignUp/list.aspx?l=12&amp;c=17" target="_blank"&gt;Email List&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: Image resizer control</title><link>http://forums.asp.net/thread/3268127.aspx</link><pubDate>Wed, 01 Jul 2009 07:04:44 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3268127</guid><dc:creator>ervipingupta</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3268127.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=19&amp;PostID=3268127</wfw:commentRss><description>&lt;p&gt;Hi Friend,

  &lt;/p&gt;&lt;p&gt;Can you told me how to &lt;b&gt;resize&amp;nbsp; the Animated image &lt;/b&gt;???&lt;br /&gt;&lt;/p&gt;</description></item><item><title>Image resizer control</title><link>http://forums.asp.net/thread/2291469.aspx</link><pubDate>Fri, 11 Apr 2008 10:38:59 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:2291469</guid><dc:creator>Ajes</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/2291469.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=19&amp;PostID=2291469</wfw:commentRss><description>&lt;p&gt;Hey people,&lt;/p&gt;
&lt;p&gt;&amp;nbsp;I have this Custom Server Control.&lt;/p&gt;
&lt;p&gt;&amp;nbsp;&lt;pre class="coloredcode"&gt;&lt;span class="st"&gt;&amp;quot;C#&amp;quot;&lt;/span&gt; Class=&lt;span class="st"&gt;&amp;quot;ShowImage&amp;quot;&lt;/span&gt; %&amp;gt;

&lt;span class="kwd"&gt;using&lt;/span&gt; System;
&lt;span class="kwd"&gt;using&lt;/span&gt; System.Web;
&lt;span class="kwd"&gt;using&lt;/span&gt; System.Data.Odbc;
&lt;span class="kwd"&gt;using&lt;/span&gt; System.Data;
&lt;span class="kwd"&gt;using&lt;/span&gt; System.IO;
&lt;span class="kwd"&gt;using&lt;/span&gt; System.Drawing;

&lt;span class="kwd"&gt;public class&lt;/span&gt; ShowImage : IHttpHandler
{
    &lt;span class="kwd"&gt;const int&lt;/span&gt; BUFFERSIZE = 1024;

    &lt;span class="kwd"&gt;public bool&lt;/span&gt; IsReusable
    {
        &lt;span class="kwd"&gt;get&lt;/span&gt;
        {
            &lt;span class="kwd"&gt;return true&lt;/span&gt;;
        }
    }

    &lt;span class="kwd"&gt;public void&lt;/span&gt; ProcessRequest(HttpContext context)
    {
        Byte[] imgBytes = &lt;span class="kwd"&gt;null&lt;/span&gt;;
        context.Response.ContentType = &lt;span class="st"&gt;&amp;quot;image/jpeg&amp;quot;&lt;/span&gt;;
        String url = Utils.Decrypt(HttpUtility.UrlDecode(context.Request.QueryString.ToString()));
        String[] arguments = url.Split(&lt;span class="kwd"&gt;new char&lt;/span&gt;[] { &lt;span class="st"&gt;&amp;#39;&amp;amp;&amp;#39;&lt;/span&gt; });

        String imagePath = arguments[0];
        String width = arguments[1];
        String height = arguments[2];
        String cacheKey = &lt;span class="st"&gt;&amp;quot;image=&amp;quot;&lt;/span&gt; + imagePath+&lt;span class="st"&gt;&amp;quot;;width=&amp;quot;&lt;/span&gt;+width+&lt;span class="st"&gt;&amp;quot;;height=&amp;quot;&lt;/span&gt;+height;
        Object cachedImageBytes = context.Cache.Get(cacheKey);

        &lt;span class="kwd"&gt;if&lt;/span&gt; (cachedImageBytes != &lt;span class="kwd"&gt;null&lt;/span&gt;)
        {
            imgBytes = (Byte[])cachedImageBytes;
        }
        &lt;span class="kwd"&gt;else&lt;/span&gt;
        {
            context.Response.BufferOutput = &lt;span class="kwd"&gt;false&lt;/span&gt;;
            imgBytes = writeSingleImage(imagePath, Convert.ToInt32(width), Convert.ToInt32(height));
            
            &lt;span class="kwd"&gt;try&lt;/span&gt;
            {
                context.Cache.Insert(cacheKey, imgBytes, &lt;span class="kwd"&gt;null&lt;/span&gt;, DateTime.Now.AddDays(14), System.Web.Caching.Cache.NoSlidingExpiration);
            }
            &lt;span class="kwd"&gt;catch&lt;/span&gt;
            {
                &lt;span class="cmt"&gt;//just ignore, dont know what happens&lt;/span&gt;
            }
        }

        &lt;span class="kwd"&gt;if&lt;/span&gt; (imgBytes != &lt;span class="kwd"&gt;null&lt;/span&gt;)
        {
            context.Response.OutputStream.Write(imgBytes, 0, imgBytes.Length);
        }
        context.Response.End();
    }

    &lt;span class="kwd"&gt;public&lt;/span&gt; Byte[] writeSingleImage(String ImagePath, &lt;span class="kwd"&gt;int&lt;/span&gt; width, &lt;span class="kwd"&gt;int&lt;/span&gt; height)
    {
        Byte[] returnImage = &lt;span class="kwd"&gt;null&lt;/span&gt;;
        Bitmap origImage;
        MemoryStream m = &lt;span class="kwd"&gt;new&lt;/span&gt; MemoryStream();
        String filePath;

        filePath = HttpContext.Current.Server.MapPath(ImagePath);
        &lt;span class="kwd"&gt;if&lt;/span&gt;(!File.Exists(filePath))
        {
            filePath = HttpContext.Current.Server.MapPath(&lt;span class="st"&gt;&amp;quot;images/no_pix.jpg&amp;quot;&lt;/span&gt;);
        }
     
        &lt;span class="kwd"&gt;try&lt;/span&gt;
        {
            origImage = &lt;span class="kwd"&gt;new&lt;/span&gt; Bitmap(filePath);
            origImage.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
            origImage.Dispose();

            returnImage = ImageUtils.resizeImage(m.GetBuffer(), width, height);
        }
        &lt;span class="kwd"&gt;catch&lt;/span&gt;
        {   
        }
        &lt;span class="kwd"&gt;return&lt;/span&gt; returnImage;
    }
}&lt;/pre&gt;
&lt;p&gt;&amp;nbsp;Now this all work great, but i am using this a lot of places and are working on creating a Class Library with all the controls i often use. This way i can deploy a dll with changes. Does anybody know how i should create a Web Server Control in a class library from the above code. I have created a class that inherits System.Web.UI.WebControl.WebControl, but when i try to write a resized image to the outputstream, all the pages displays is unicode chars. I used this code to write the image to the stream. &lt;/p&gt;&lt;font size="2"&gt;&lt;pre class="coloredcode"&gt;Page.Response.ContentType = &lt;span class="st"&gt;&amp;quot;image/jpeg&amp;quot;&lt;/span&gt;;
Page.Response.OutputStream.Write(imgBytes, 0, imgBytes.Length);&lt;/pre&gt;&lt;pre class="coloredcode"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre class="coloredcode"&gt;Any thoughts??&lt;/pre&gt;&lt;/font&gt;</description></item></channel></rss>