0

次のコードは、Percent (グラフを青で表示する割合)、Max (最大値)、およびガロン値 (数値) を受け取って温度計スタイルの進行状況メーターを作成するハンドラーです。として Web フォームに追加されたグラフを出力します<img src="Thermometer.ashx" alt="" />

このグラフは、節約された水のガロン数として進行状況を示すことを目的としているため、水樽の画像を青色で塗りつぶすとよいでしょう。そうするために、内部が透明なバレル画像を 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));
                }
            }
        }
    } 
}

アップデート:

解決方法を示したこの投稿を見つけました: GDI+ で画像をオーバーレイする

これが私の解決策です:

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

ただし、オリジナルは非常にシャープで鮮明ですが、pngレイヤーの品質は悪いです(少しぼやけています)。元の画質を維持する方法はありますか?

ご協力いただきありがとうございます。

4

2 に答える 2

0

Jpegに変更TempImage.Save(context.Response.OutputStream, ImageFormat.gif);すると、品質の問題が解決しました。

于 2010-11-19T22:04:13.907 に答える
0

これを実現する最善の方法は、イメージ全体を C# で作成して送信することだと思います。「温度計」上のバレルの正確な位置を取得できます。バレルが既に (半) 透明な PNG である場合は、サーモスタットのすぐ上で .DrawImage を実行し、テキストを追加して、送信する前に必要なことを行うことができます。最終的な画像を ResponseStream に直接渡します。

あなたのための他のいくつかの提案:

Handler ですべてを直接処理しているため、独自の内部使い捨てリソースを破棄する必要があります。ブロックを「使用する」のが最も簡単な方法です。少なくとも次の 2 つが必要です。

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))
{
    ...
}
于 2010-11-20T00:43:56.930 に答える