将通用处理程序转换为服务器控件

时间:2013-08-08 04:32:37

标签: asp.net

此处我有一个代码,用于从 fn 查询字符串调整图像大小,该图像从 App_Data / Users 并使用我的徽标进行编辑,并将其调整为低质量和低尺寸的图像并返回最终图像。

using System;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;

public class UsersImageThumb : IHttpHandler {

public void ProcessRequest(HttpContext context)
{
    {
        try
        {
            Image t = Image.FromFile(context.Server.MapPath("~/App_Data/Users/") + context.Request["fn"]);
            Image t1 = Image.FromFile(context.Server.MapPath("~/favicon.png"));
            Bitmap b = new Bitmap(150, 200 * t.Height / t.Width);
            using (Graphics gr = Graphics.FromImage(b))
            {
                /* Some codes that can help us to have better image at final
                gr.CompositingQuality = CompositingQuality.HighSpeed;
                gr.InterpolationMode = InterpolationMode.HighQualityBicubic;
                gr.CompositingMode = CompositingMode.SourceCopy;
                */
                gr.DrawImage(t, 0, 0, 150, 200 * t.Height / t.Width);
                gr.DrawImage(t1, 10, 10, t1.Width, t1.Height);
                /* Some codes that can help us to have better image at final
                gr.DrawString("شهر برفی", new Font("Tahoma", 10, FontStyle.Bold), Brushes.White, 10, 42);
                int x1, y1, x2, y2;
                Random rg = new Random();
                for (int i = 0; i < 100; i++)
                {
                    x1 = rg.Next(0, 250);
                    x2 = rg.Next(0, 250);
                    y1 = rg.Next(0, 250 * t.Height / t.Width);
                    y2 = rg.Next(0, 250 * t.Height / t.Width);
                    gr.DrawLine(Pens.White, x1, y1, x2, y2);
                }
                */
                using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
                {
                    b.Save(memoryStream, ImageFormat.Jpeg);
                    memoryStream.WriteTo(context.Response.OutputStream);
                }
            }
        }
        catch
        {
            context.Response.Write("Image Not Found");
        }
    }
}

public bool IsReusable {
    get {
        return false;
    }
}
}

我的问题:

  • 如何在服务器控件(DLL文件)上使用此代码?

此处我们有一个代码,用于设置 WebUserControls DesktopUserControls 通用处理程序的属性, 服务器控件 WebParts ...

note: This code can create property of Text in String and shows Text in a lable (lbl_NewsText)

 private string _Text;
    public string Text
    {
        get
        {
            return _Text;
        }
        set
        {
            _Text = lbl_NewsText.Text = value;
        }
    }

1 个答案:

答案 0 :(得分:0)

非常简单。我创建了一个新的服务器控件并使用我的代码。但是我们没有任何查询字符串,因此我们大多数都为此创建属性。这段代码可以帮到你。

private string _fn;
public string fn
{
    get
    {
        return _fn;
    }
    set
    {
        _fn = value;
    }
}

当控件呈现时,我使用该代码!

Image t = Image.FromFile(fn);
Image t1 = Image.FromFile(LogoDestinationProperty);
Bitmap b = new Bitmap(150, 200 * t.Height / t.Width);
using (Graphics gr = Graphics.FromImage(b))
{

    gr.DrawImage(t, 0, 0, 150, 200 * t.Height / t.Width);
    gr.DrawImage(t1, 10, 10, t1.Width, t1.Height);

    using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
    {
        b.Save(memoryStream, ImageFormat.Jpeg);
        output.Write(context.Response.OutputStream);
    }
}