RSS订阅优然探索
你的位置:首页 » 技术文章 » 正文

图片文字合成

选择字号: 超大 标准 发布时间:2011-1-23 18:56:32 | 作者:admin | 0个评论 | 人浏览

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 System.Runtime.InteropServices.ComTypes;

public partial class Default7 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        initPage();
    }
    protected void initPage()
    {
        ////string url = Server.MapPath("upload") +"\\"+ "soufunquan.jpg";

        string url = "d:/quan.jpg";

        String src = url;   //源图像文件的绝对路径
        String dest = "d:/xxx.jpg";    //生成的缩略图图像文件的绝对路径
        int thumbWidth = 624;    //要生成的缩略图的宽度
        int thumbHeight = 306;
        System.Drawing.Image image = System.Drawing.Image.FromFile(url); //利用Image对象装载源图像
        //接着创建一个System.Drawing.Bitmap对象,并设置你希望的缩略图的宽度和高度。
        int srcWidth = image.Width;
        int srcHeight = image.Height;
        //int thumbHeight=(srcHeight/srcWidth)*thumbWidth;
        Bitmap bmp = new Bitmap(thumbWidth, thumbHeight);
        //从Bitmap创建一个System.Drawing.Graphics对象,用来绘制高质量的缩小图。
        System.Drawing.Graphics gr = System.Drawing.Graphics.FromImage(bmp);
        //设置 System.Drawing.Graphics对象的SmoothingMode属性为HighQuality
        gr.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        //下面这个也设成高质量
        gr.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
        //下面这个设成High
        gr.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

 

        //把原始图像绘制成上面所设置宽高的缩小图
        System.Drawing.Rectangle rectDestination = new System.Drawing.Rectangle(0, 0, thumbWidth, thumbHeight);
        gr.DrawImage(image, rectDestination, 0, 0, srcWidth, srcHeight, GraphicsUnit.Pixel);


        SolidBrush drawBrush = new SolidBrush(Color.Red);

        Font drawFont = new Font("Arial", 10, FontStyle.Bold, GraphicsUnit.Millimeter);

        int xPos = image.Height - (image.Height - 25);

        int yPos = 3;
       
        //设置好x,y坐标,可以写多个

        gr.DrawString("zydzydyzydydydyyydydy", drawFont, drawBrush, xPos, yPos);

        gr.DrawString("123123123123123123123", drawFont, drawBrush, xPos, yPos+20);
        //保存图像,大功告成!
        bmp.Save(dest);
        //最后别忘了释放资源(译者PS:C#可以自动回收吧)
        bmp.Dispose();
        image.Dispose();


    }

}
 

标签:

猜你喜欢

发表评论

必填

选填

选填

必填,不填不让过哦,嘻嘻。

记住我,下次回复时不用重新输入个人信息

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。