quarta-feira, 29 de julho de 2015

Image C# Canvas

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.IO;
using System.Reflection;

namespace lapuinka_mobile
{
    public class Speedometer
    {
        public static Bitmap genImage(HttpServerUtility Server, double percentage)
        {
            String percentageStr = String.Format("{0:00.00}", percentage) + "%";

            double angle = Convert.ToDouble(percentage) * Math.PI / 200 + Math.PI / 2;

            Bitmap pg = new Bitmap(900, 900);
            Graphics gr = Graphics.FromImage(pg);

            // clear the canvas to white
            Rectangle pgRect = new Rectangle(0, 0, pg.Width, pg.Height);
            Rectangle pgRectArc = new Rectangle(0, 0, pg.Width, pg.Height * 2);
            SolidBrush solidWhite = new SolidBrush(Color.White);
            gr.Clear(Color.White);

            //System.Drawing.Image img = System.Drawing.Image.FromStream(System.Net.WebRequest.Create("http://webmail.lapuinka.com.br/mbiaviaomobile/images/lapuinkaMeterNewest.png").GetResponse().GetResponseStream());
            System.Drawing.Image img = System.Drawing.Image.FromFile(Server.MapPath("~") + "/images/lapuinkaMeterNewest.png");
            Rectangle rc = new Rectangle(0, 0, pg.Width, pg.Height);
            gr.DrawImage(img, pgRect);
            img.Dispose();


            Point endpoint = new Point(434 - (int)(Math.Cos(angle) * 208)/*circle radius*/,
                            (420 - (int)(Math.Sin(angle) * 208)));

            Point arrow1 = new Point(434 - (int)(Math.Cos(angle - 0.09) * 180)/*circle radius*/,
                            (420 - (int)(Math.Sin(angle - 0.1) * 180)));

            Point arrow2 = new Point(434 - (int)(Math.Cos(angle + 0.09) * 180)/*circle radius*/,
                            (420 - (int)(Math.Sin(angle + 0.1) * 180)));

            Font fn = new Font("Comic Sans MS", 50);
            Pen pen = new Pen(Color.Black, 8);
            gr.DrawLine(pen, new Point(434, 420), endpoint);
            gr.DrawLine(pen, arrow1, endpoint);
            gr.DrawLine(pen, arrow2, endpoint);

            //gr.DrawArc(pen, new Rectangle(0, 0, 900, 900), 0, 360);
            //gr.DrawRectangle(pen, new Rectangle(285, 515, 320, 525));
            SolidBrush lapuinkaPink = new SolidBrush(Color.FromArgb(255, 0, 102));
            gr.DrawString(percentageStr, fn, lapuinkaPink, 285, 515);

            return pg;
        }
    }
}


Postar um comentário