生成带版本的缩略图

1、前台代码:

 <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <p>
<input id="uploadFile" type="file" runat="server" />
<asp:Label id="lblErrInfo" runat="server" forecolor="Red"></asp:Label>
</p>
<p>
    宽:<asp:TextBox id="txtWidth" runat="server" Width="40px">200</asp:TextBox>
    高:<asp:TextBox id="txtHeight" runat="server" Width="40px">150</asp:TextBox>
 
</p>
<p>
添加信息:<asp:TextBox id="txtAddInfo" runat="server">林轶真帅!</asp:TextBox>
</p>
<p>
信息位置:left:<asp:TextBox id="txtLeft" runat="server" Width="40px">40</asp:TextBox>
 right:<asp:TextBox id="txtRight" runat="server" Width="40px">100</asp:TextBox>
</p>
<p>
 
<input id="button" type="button" value="上传生成缩略图" onServerClick="Button_Click" runat="server" />
</p>
<p><asp:Image id="ImgPreview" runat="server"></asp:Image>
</p>
    </div>
    </form>
</body>
</html>

2、后台代码:

using System;
using System.Data;
using System.Configuration;
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.IO;
using System.Drawing;
using System.Drawing.Imaging;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //让图片控件默认为不可见的。
            ImgPreview.Visible = false;
        }
    }
    //生成带文字的图片。
    void GetThumbnailImage(int width, int height, string strInfo, int left, int right)
    {
        //得到要处理的图片。
        string file = uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('//') + 1);
        //把上传的图片文件的格式改为.JPG的。
        string newfile =  uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('//') + 1) + ".jpg";
        //将要在图片上显示的文字赋值给一个变量。
        string strAdd = strInfo;
        //得到要处理的图片。
        System.Drawing.Image oldimage = System.Drawing.Image.FromFile(Server.MapPath(file));
        //根据设定的高宽来生成新图片。
        System.Drawing.Image thumbnailImage =
        oldimage.GetThumbnailImage(width, height, new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
        //清除内容。
        Response.Clear();
        Bitmap output = new Bitmap(thumbnailImage);
        Graphics g = Graphics.FromImage(output);
        //在图片是上添加文字的字体、大小、颜色、位置。
        g.DrawString(strAdd, new Font("Courier New", 14), new SolidBrush(Color.Black), left, right);
        //将新图片保存。
        output.Save(Server.MapPath(newfile), System.Drawing.Imaging.ImageFormat.Jpeg);
        //保存类型为图片类型。
        Response.ContentType = "image/gif";
        //将图片控件设为可见。
        ImgPreview.Visible = true;
        //在图片控件上显示新图片。
        ImgPreview.ImageUrl = newfile;
    }
    bool ThumbnailCallback()
    {
        return true;
    }

    //生成带文字的图片。
    protected void Button_Click(object sender, EventArgs e)
    {
        int width, height, left, right;

        //需要在图片上添加的内容。
        string strAddInfo = txtAddInfo.Text;
        //生成图片的宽度。
        width = Int32.Parse(txtWidth.Text);
        //生成图片的高度。
        height = Int32.Parse(txtHeight.Text);
        //设置文字显示的位置。
        left = Int32.Parse(txtLeft.Text);
        //设置文字显示的位置。
        right = Int32.Parse(txtRight.Text);

        //判断上传文件是否存在。
        if (!(uploadFile.PostedFile.ContentLength > 0))
        {
            lblErrInfo.Text = "没有选择文件";
        }
        else
        {

            //得到上传文件的路径。
            string path = Server.MapPath( uploadFile.PostedFile.FileName.Substring(uploadFile.PostedFile.FileName.LastIndexOf('//') + 1));
            //判断文件路径下是否存在相同的文件名。
            if (File.Exists(path))
            {
                lblErrInfo.Text = "已经有同名文件";
            }
            else
            {
                //将上传的文件进行保存。
                uploadFile.PostedFile.SaveAs(path);
                //调用方法,根据原图片生成新的图片。
                GetThumbnailImage(width, height, strAddInfo, left, right);
            }
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值