将背景图形添加到http处理程序图像

时间:2010-11-19 15:17:14

标签: c# gdi+ system.drawing ashx ihttphandler

以下代码是一个处理程序,它接受百分比(图表显示为蓝色的百分比),最大值(最大值)和加仑值(数字)以创建温度计样式的进度表。它输出一个图表,该图表作为<img src="Thermometer.ashx" alt="" />添加到Web表单中。

由于此图表旨在显示节省的加仑水数量,因此将蓝色填充水桶图像会很好。为此,我尝试将具有透明内部的桶形图像添加到Web表单并尝试将其放置在温度计图像的前面,但这没有用,因为似乎无法将图像叠加在使用创建的图像之上处理程序。

我的问题是:是否可以通过代码添加桶形图像,以便作为蓝色温度计的轮廓?或者是否需要在代码中从头开始创建桶形图像?我担心后者,因为我可能无法编码。

请参阅下文,如果有什么我可以做,请告诉我。

谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Net;
using System.Drawing.Imaging;
using System.Web.UI.HtmlControls;
namespace rainbarrel
{
    public class Thermometer : IHttpHandler
    {
        public bool IsReusable
        {
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            int Percent = 25;
            int Max = 20000;
            bool Gallon = true; 
            float Pixels = (float)Percent * 2.15f;
            Bitmap TempImage = new Bitmap(200, 250);
            Graphics TempGraphics = Graphics.FromImage(TempImage);
            TempGraphics.FillRectangle(Brushes.White, new Rectangle(0, 0, 200, 250));
            Pen TempPen = new Pen(Color.Black);
            BarrelFill(TempImage, Percent);
            bool DrawText = true;
            float Amount = Max;
            for (float y = 20.0f; y < 235.0f; y += 10.75f)
            {
                TempGraphics.DrawLine(TempPen, 119, y, 125, y);
                if (DrawText)
                {
                    Font TempFont = new Font("Arial", 8.0f, FontStyle.Regular);
                    if (Gallon)
                    {
                        TempGraphics.DrawString(Amount.ToString() + " Gal", TempFont, Brushes.Black, 130, y);
                    }
                    else
                    {
                        TempGraphics.DrawString(Amount.ToString(), TempFont, Brushes.Black, 130, y);
                    }
                    DrawText = false;
                }
                else DrawText = true;
                Amount -= ((Max / 100) * 5.0f);
            }

            string etag = "\"" + Percent.GetHashCode() + "\"";
            string incomingEtag = context.Request.Headers["If-None-Match"];

            context.Response.Cache.SetExpires(DateTime.Now.ToUniversalTime().AddDays(1));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
            context.Response.Cache.SetMaxAge(new TimeSpan(7, 0, 0, 0));
            context.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            context.Response.Cache.SetETag(etag);

            if (String.Compare(incomingEtag, etag) == 0)
            {
                context.Response.StatusCode = (int)HttpStatusCode.NotModified;
                context.Response.End();
            }
            else
            {
                context.Response.ContentType = "image/Gif";
                TempImage.Save(context.Response.OutputStream, ImageFormat.Gif);
                TempImage.MakeTransparent();
            }
        }

        private void BarrelFill(Bitmap TempImage, int Percent)
        {
            if (Percent == 100)
            {
                FillRectangle(TempImage, 60, 20, 235);
            }
            else
            {
                FillRectangle(TempImage, 60, (int)(235.0f - ((float)Percent * 2.15f)), 235);
            }
        }

        private void FillRectangle(Bitmap TempImage, int x, int y1, int y2)
        {
            int MaxDistance = 50;
            for (int i = x - MaxDistance; i < x + MaxDistance; ++i)
            {
                for (int j = y1; j < y2; ++j)
                {
                    float Distance = (float)Math.Abs((i - x));
                    int BlueColor = (int)(255.0f * (1.0 - (Distance / (2.0f * MaxDistance))));
                    TempImage.SetPixel(i, j, Color.FromArgb(30, 144, BlueColor));
                }
            }
        }
    } 
}

更新

我在这里发现这篇文章向我展示了如何解决: overlaying images with GDI+

这是我的解决方案:

Image Barrel = Image.FromFile("C:\\Inetpub\\wwwroot\\barrel\\barrel_trans.png");
            TempGraphics.DrawImage(Barrel, -5, 0, 120, 240);
            Barrel.Dispose();

然而,png层的质量很差(略微模糊),尽管原始图层非常清晰。有没有办法保留原始图像的质量?

非常感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

TempImage.Save(context.Response.OutputStream, ImageFormat.gif);更改为Jpeg解决了质量问题。

答案 1 :(得分:0)

我认为实现这一目标的最佳方法是在C#中创建整个图像并将其发送下来。您可以在“温度计”上获得桶的精确定位,如果桶已经是(半)透明PNG,您可以在恒温器上方做一个.DrawImage,添加文本,并在发回之前做任何您想做的事情最终图像直接进入ResponseStream。

为您提出的其他一些建议:

由于您直接在Handler中处理所有内容,因此您应该处置自己的内部可支配资源。 “使用”块是最简单的方法。你至少需要这两个:

using (Graphics TempGraphics = GetGraphicsFromBitmap(TempImage))
{
    for (float y = 20.0f; y < 235.0f; y += 10.75f)
    {
        TempGraphics.DrawLine(TempPen, 119, y, 125, y);
        ...
    }
}

using (Font TempFont = new Font("Arial", 8.0f, FontStyle.Regular))
{
    ...
}