0

在这里,我有一个用于从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 文件)上使用此代码?

在这里,我们有一个代码用于设置WebUserControlsDesktopUserControlsClassesGeneric HandlersServer ControlsWebParts...

注意:此代码可以在 String 中创建 Text 的属性并在标签 ( lbl_NewsText)中显示 Text

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

1 回答 1

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);
    }
}
于 2013-09-18T14:30:15.100 回答