现在的位置: 首页 > 综合 > 正文

asp.net网页生成图片

2013年10月30日 ⁄ 综合 ⁄ 共 11413字 ⁄ 字号 评论关闭

using System;
using System.Collections;
using System.Web;
using System.IO;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace WebFunction
{
    public class SceneCouponGdi
    {
        private Bitmap bmPhoto;
        private Graphics objGraphics;
        public Bitmap BmPhoto
        {
            get
            {
                return bmPhoto;

            }
            set
            {
                bmPhoto = value;
            }
        }
        public Graphics ObjGraphics
        {
            get
            {
                return objGraphics;

            }
            set
            {
                objGraphics = value;
            }
        }
        public void CreatePhoto(int imgWidth, int imgHeigth)
        {
            BmPhoto = new Bitmap(imgWidth, imgHeigth, PixelFormat.Format24bppRgb);
            BmPhoto.SetResolution(bmPhoto.HorizontalResolution, bmPhoto.VerticalResolution);
            ObjGraphics = Graphics.FromImage(BmPhoto);
            // ObjGraphics.DrawRectangle(new Pen(Color.Black), 1, 1, imgWidth - 1, imgHeigth - 1); //画一下框
            ObjGraphics.FillRectangle(new SolidBrush(Color.White), 1, 1, BmPhoto.Width - 2, BmPhoto.Height - 2);

            ObjGraphics.SmoothingMode = SmoothingMode.AntiAlias;
            ObjGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            ObjGraphics.CompositingQuality = CompositingQuality.HighQuality;
            //ObjGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //文字抗锯齿
            //ObjGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixel; //文字抗锯齿
        }

        public void CreatePlainText(int posX, int posY, string sText, String sColor, float sSize, string sFont, FontStyle fontStyle)
        {
            FontFamily fontFamily = new FontFamily(sFont);
            Font f = new Font(fontFamily, sSize, fontStyle, GraphicsUnit.Pixel);
            Brush b = new SolidBrush(ColorTranslator.FromHtml(sColor));
            Rectangle rect = new Rectangle(posX, posY, BmPhoto.Width, BmPhoto.Height);
            ObjGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            ObjGraphics.DrawString(sText, f, b, rect);
        }
        //指定方框,字会自动换行
        public void CreatePlainText(int posX, int posY, string sText, String sColor, float sSize, string sFont, FontStyle fontStyle, int width, int height)
        {
            FontFamily fontFamily = new FontFamily(sFont);
            Font f = new Font(fontFamily, sSize, fontStyle, GraphicsUnit.Pixel);
            Brush b = new SolidBrush(ColorTranslator.FromHtml(sColor));
            Rectangle rect = new Rectangle(posX, posY, width, height);
            ObjGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;
            ObjGraphics.DrawString(sText, f, b, rect);
        }
        public void CreateLine(Color sColor, int x1, int y1, int x2, int y2)
        {
            Brush b = new SolidBrush(sColor);
            Pen pen = new Pen(b);
            ObjGraphics.DrawLine(pen, x1, y1, x2, y2);
        }

        public void LoadScenePhoto(string imgPath, int posX, int posY, int width, int height)
        {

            System.Drawing.Image image = System.Drawing.Image.FromFile(imgPath);
            ObjGraphics.DrawImage(image, posX, posY, width, height);
            image.Dispose();
        }

        public static void LoadDownImg(Bitmap bitMap)
        {
            MemoryStream downStream = new MemoryStream();
            //处理JPG质量的函数
            int level = 95;
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            ImageCodecInfo ici = null;
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.MimeType == "image/jpeg")
                    ici = codec;
            }
            EncoderParameters ep = new EncoderParameters();
            ep.Param[0] = new EncoderParameter(Encoder.Quality, (long)level);

            bitMap.Save(downStream, ici, ep);
            //bitMap.Save(downStream, ImageFormat.Jpeg);
            HttpResponse response = HttpContext.Current.Response;
            response.ClearContent();
            response.ContentType = "image/jpeg";
            response.BinaryWrite(downStream.ToArray());
            bitMap.Dispose();
        }

        public void CreateWatermark(string sText1, Color sColor1, float sSize1, string sFont1, Color sBgColor, int sTransparence)
        {
            HatchBrush b1 = new HatchBrush(HatchStyle.Wave, Color.Red, Color.Yellow);
            LinearGradientBrush b2 = new LinearGradientBrush(new Point(-BmPhoto.Width - 10, 40), new Point(BmPhoto.Width + 100, 40), Color.FromArgb(sTransparence, sBgColor), Color.FromArgb(sTransparence, Color.White));

            FontFamily fontFamily = new FontFamily(sFont1);
            Font f1 = new Font(fontFamily, sSize1, FontStyle.Bold);
            Brush brushfortext1 = new SolidBrush(sColor1);
            Brush brushforbg = new SolidBrush(Color.FromArgb(sTransparence, sBgColor)); //背景色透明化, 0-255,0为完全透明
            ObjGraphics.RotateTransform(-20); //旋转
            Rectangle rect = new Rectangle(-BmPhoto.Width / 2 - 50, BmPhoto.Height - 50, BmPhoto.Width * 2, 40);
            //Rectangle rect = new Rectangle(0, BmPhoto.Height-60, BmPhoto.Width, 40);
            ObjGraphics.DrawRectangle(new Pen(brushforbg), rect); //先画一下框,可以使其边框清晰
            //ObjGraphics.FillRectangle(brushforbg, rect);
            ObjGraphics.FillRectangle(b2, rect);

            Rectangle rectfortext1 = new Rectangle(-BmPhoto.Width / 2 + 200 + BmPhoto.Width / 5, BmPhoto.Height - 45, BmPhoto.Width * 2, 60);
            //Rectangle rectfortext1 = new Rectangle((0+100), BmPhoto.Height - (60-5), BmPhoto.Width, 40);
            //for (int i = 0; i < 10; i++) //重复写10次,让其清晰
            //{
            //    ObjGraphics.DrawString(sText1, f1, brushfortext1, rectfortext1);
            //}
            ObjGraphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; //文字抗锯齿
            ObjGraphics.DrawString(sText1, f1, brushfortext1, rectfortext1);
            ObjGraphics.RotateTransform(20); //旋转回来
        }
        public Bitmap MutiPhoto(ArrayList subImage, int imgWidth, int imgHeigth)
        {
            int mutiNum = subImage.Count;
            Bitmap bmPhotoFull = new Bitmap(imgWidth, (imgHeigth + 10) * mutiNum - 10, PixelFormat.Format24bppRgb);
            bmPhotoFull.SetResolution(bmPhotoFull.HorizontalResolution, bmPhotoFull.VerticalResolution);
            Graphics ObjGraphicsFull = Graphics.FromImage(bmPhotoFull);
            ObjGraphicsFull.FillRectangle(new SolidBrush(Color.White), 0, 0, bmPhotoFull.Width, bmPhotoFull.Height);
            for (int i = 0; i < mutiNum; i++)
            {
                ObjGraphicsFull.DrawImage((Image)subImage[i], 0, (imgHeigth + 10) * i, imgWidth, imgHeigth);
                ((Image)subImage[i]).Dispose();
            }
            return bmPhotoFull;
        }
    }
}

===============================

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using WebFunction;
using BusinessFacade;
using CLibrary;
using System.IO;

public partial class scene_secret_scene_ticket_pic : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SceneCouponGdi.LoadDownImg(MutiSceneCoupon());   
    }

    public Bitmap MutiSceneCoupon()
    {
        ArrayList bitmaps = new ArrayList();
        Hashtable ht=SetInfo();
        int adultNum = Convert.ToInt32(ht["adult"]);
        if (adultNum > 0)
        {
            for (int i = 0; i < adultNum; i++)
            {
                bitmaps.Add(OneSceneCoupon(ht, i));
            }
            SceneCouponGdi sceneCouponGdi = new SceneCouponGdi();
            Bitmap MutiBitMap = sceneCouponGdi.MutiPhoto(bitmaps, 610, 229);
            return MutiBitMap;
        }
        else
        {
            Response.End();
            return null;
        }
    }
    public Bitmap OneSceneCoupon(Hashtable ht, int serialNumber)
    {
        SceneCouponGdi sceneCouponGdi = new SceneCouponGdi();
        sceneCouponGdi.CreatePhoto(610, 229);

        string backgroudImgPath = Server.MapPath(@"../img/ticket_bg.gif");
        sceneCouponGdi.LoadScenePhoto(backgroudImgPath, 302, 4, 300, 216);

        string sceneImgPath = ht["Logo"].ToString();
        sceneCouponGdi.LoadScenePhoto(sceneImgPath, 18, 65, 140, 140);

//        string sText1 = @"旅游名店城 www.yocity.cn
//            优惠价"+ht["price"].ToString()+@"元整";
//        sceneCouponGdi.CreateWatermark(sText1, Color.Red, 10, "宋体", Color.Gray, 180);

        string sText = ht["scene"].ToString();
        sceneCouponGdi.CreatePlainText(38, 14, sText, "#000066", 14, "宋体", FontStyle.Bold);

        sText = ht["sceneticket"].ToString();
        sceneCouponGdi.CreatePlainText(245, 14, sText, "#000066", 14, "宋体", FontStyle.Bold);

        sText = @"优惠价:¥" + ht["price"].ToString();
        sceneCouponGdi.CreatePlainText(479, 14, sText, "#000066", 14, "宋体", FontStyle.Bold);

        sText = @"景区优惠券 YOCITY.CN    " + ht["webmember"].ToString() + @"    有效期:" + ht["usetime"].ToString() + @"    编号:" + ht["reserveid"].ToString() + "_" + serialNumber;
        sceneCouponGdi.CreatePlainText(24, 38, sText, "#000000", 12, "宋体", FontStyle.Regular);

        sText = ht["beizhu"].ToString();
        sceneCouponGdi.CreatePlainText(174, 69, sText, "#990000", 12, "宋体", FontStyle.Regular, 395, 100);

        sceneCouponGdi.CreateLine(Color.Blue, 178, 132, 555, 132);

        sText = @"* 凭券在景区售票处享受优惠购票。(一张优惠券仅限换购一张优惠门票)
       
* 涉及订房的优惠请预先与景区确认。

* 景区景点及本网站保留对本优惠券的最终使用解释权。";
        sceneCouponGdi.CreatePlainText(174, 143, sText, "#333333", 12, "宋体", FontStyle.Regular);

        return sceneCouponGdi.BmPhoto;       
    }

    private Hashtable SetInfo()
    {
        Hashtable ht = new Hashtable();
        string webmemberid = Context.User.Identity.Name;
        string reserveid = Request["reserveid"];
        ht["reserveid"] = reserveid;
        if (reserveid == null || reserveid == "")
        {
            Response.End();
        }
        COrder _COrder = new COrder();
        _COrder.reserveID = reserveid;

        if (_COrder.Init())
        {
            ht["webmember"] = _COrder.reserve_name;
        }
        else
        {
            Response.End();
        }
        if (_COrder.web_memberid != webmemberid)
        {
            Response.Redirect("../../error.aspx");
        }
        COrderScene _COrderScene = new COrderScene();
        _COrderScene.reserveID = reserveid;
        if (_COrderScene.Init())
        {

            string priceID = _COrderScene.priceid;
            if (priceID == null || priceID == "")
            {
                Response.End();
            }
            CSceneTicket _CSceneTicket = new CSceneTicket();
            CScene _CScene = new CScene();
            _CSceneTicket.priceID = priceID;
            if (_CSceneTicket.Init())
            {
                _CScene.sceneid = _CSceneTicket.sceneid;
                _CScene.Init();
                ht["scene"] = _CScene.scene;
                ht["sceneticket"] = _CSceneTicket.tkname;
                ht["price"] = _COrderScene.sale_adult;
                ht["beizhu"] = _COrderScene.beizhu;
                ht["usetime"] = CFormat.datetimeGet(_COrderScene.godate).ToShortDateString() + "至" + CFormat.datetimeGet(_COrderScene.godate).AddDays(7).ToShortDateString();
                ht["other_name"] = _COrderScene.other_name;
                ht["adult"] = _COrderScene.adult;
                string picLogo = _CScene.logo;
                if (picLogo == string.Empty)
                {
                    ht["Logo"] = Server.MapPath(@"../img/ticket_logo.gif");
                }
                else
                {
                    string appSettingValue = System.Configuration.ConfigurationManager.AppSettings["AdminPhysicalPath"].ToString();
                    string dbPicPath = (appSettingValue + picLogo).Replace('/', '//');
                    if (File.Exists(dbPicPath))
                    {
                        ht["Logo"] = dbPicPath;
                    }
                    else
                    {
                        ht["Logo"] = Server.MapPath(@"../img/ticket_logo.gif");
                    }

                }

            }
        }
        return ht;
    }
}
 

抱歉!评论已关闭.