首页新闻找找看学习计划

如何控制整站都是缩略图

0
[已关闭问题]

网站要如何才能让上传的图片都为缩略图。是不是需要用程序来处理把上传的图片转换为缩略图啊

yzy的主页 yzy | 菜鸟二级 | 园豆:311
提问于:2009-12-30 09:59
< >
分享
其他回答(1)
0

是的,要用程序来处理,你可以上网查下缩略图算法,网上很多

一般数据设计的时候价格缩略图的字段,或者你的缩略图有特定的命名规则比如:s_图片名称.jpg

然后需要调用缩略图的地方的图片地址改为XX/s_图片名称.jpg就可以了

小火 | 园豆:242 (菜鸟二级) | 2009-12-30 10:32
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.IO;
using System.Drawing.Imaging;

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

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Graphics g = null;
        System.Drawing.Image upimage = null;
        System.Drawing.Image thumimg = null;
        System.Drawing.Image simage = null;
        Bitmap outputfile = null;

        string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
        string extension = "";

        string aaa = this.File3.PostedFile.FileName;


        if (this.File3.PostedFile.FileName.ToString() != "" && this.File3.PostedFile.ContentLength == 0)
        {
            Response.Write("<script>alert('缩略图中您选择的文件不存在!~');window.location='Default18.aspx';</script>");
            Response.End();
        }

        if (FileSize(this.File3.PostedFile, 0, 2048000) == false)
        {
            Response.Write("<script>alert('缩略图的大小不能超过2000k!~');window.location='Default18.aspx';</script>");
            Response.End();
        }

        string pathname = this.File3.PostedFile.FileName;

        int sum = pathname.LastIndexOf(".");

        string exname = pathname.Substring(sum + 1);

        switch (exname.ToLower())
        {
            case "jpg":
            case "gif":

                break;
            default:
                Response.Write("<script>alert('您选择的文件不是图片,请选择正确的图片路径!~');window.location='Default18.aspx'</script>");
                Response.End();

                break;
        }

        try
        {
            extension = Path.GetExtension(File3.PostedFile.FileName).ToUpper();
            string smallpath = Request.PhysicalApplicationPath + "\\smallimg\\";
            string bigpath = Request.PhysicalApplicationPath + "\\bigimg\\";
            int width, height, newwidth, newheight;
            System.Drawing.Image.GetThumbnailImageAbort callb = new System.Drawing.Image.GetThumbnailImageAbort(ThumbnailCallback);
            if (!Directory.Exists(smallpath))
                Directory.CreateDirectory(smallpath);
            if (!Directory.Exists(bigpath))
                Directory.CreateDirectory(bigpath);
            Stream upimgfile = File3.PostedFile.InputStream;
            string simagefile = Request.PhysicalApplicationPath + "WebSite1\\a8logo.png";  //要加水印的文件
            simage = System.Drawing.Image.FromFile(simagefile);
            upimage = System.Drawing.Image.FromStream(upimgfile); //上传的图片
            width = upimage.Width;
            height = upimage.Height;
            if (width > height)
            {
                newwidth = 500;
                newheight = (int)((double)height / (double)width * (double)newwidth);
            }
            else
            {
                newheight = 500;
                newwidth = (int)((double)width / (double)height * (double)newheight);
            }
            thumimg = upimage.GetThumbnailImage(newwidth, newheight, callb, IntPtr.Zero);
            outputfile = new Bitmap(upimage);
            g = Graphics.FromImage(outputfile);
            g.DrawImage(simage, new Rectangle(upimage.Width - ((upimage.Width / 2) + (simage.Width / 2)), upimage.Height - ((upimage.Height / 2) + (simage.Height / 2)), upimage.Width, upimage.Height), 0, 0, upimage.Width, upimage.Height, GraphicsUnit.Pixel);

            string newpath = bigpath + filename + extension;     //原始图路径
            string thumpath = smallpath + filename + extension;  //缩略图路径

            outputfile.Save(newpath);
            thumimg.Save(thumpath);
            outputfile.Dispose();

        }
        catch (Exception ex)
        {
            Response.Write(ex.Message+"321");
        }
        finally
        {
            if (g != null)
                g.Dispose();
            if (thumimg != null)
                thumimg.Dispose();
            if (upimage != null)
                upimage.Dispose();
            if (simage != null)
                simage.Dispose();
        }

        string datu = "bigimg/" + filename + "" + extension + "";

        string xiaotu = "smallimg/" + filename + "" + extension + "";

        Label1.Text = "大图:"+datu + "小图:" + xiaotu;
    }


    public bool ThumbnailCallback()
    {
        return false;
    }

    /// 判断文件尺寸
    /// </summary>
    /// <param name="file_name">要上传的文件对象</param>
    /// <param name="i">当前要上传的文件在总上传序列中的index</param>
    /// <param name="size">允许上传文件的尺寸</param>
    public bool FileSize(HttpPostedFile file_name, int i, int size)
    {
        HttpFileCollection MyFiles = HttpContext.Current.Request.Files;
        file_name = MyFiles[i];
        if (file_name.ContentLength > size)
        {
            return false;
        }
        else
        {
            return true;
        }
    }

    //修改文件名
    public string getnewfilename(string file_name)
    {
        string ExtendName = "";
        //    Guid guid = Guid.NewGuid();
        Guid guid = Guid.NewGuid();
        //   string filenamefirst=DateTime.Now.Year.ToString()+Convert.ToString(DateTime.Now.Month)+Convert.ToString(DateTime.Now.Day)+Convert.ToString(DateTime.Now.Hour)+Convert.ToString(DateTime.Now.Month)+Convert.ToString(DateTime.Now.Second+DateTime.Now.Millisecond.ToString());
        string filenamefirst = "ws_" + guid.ToString().Replace("-", "");
        ExtendName = file_name.Substring(file_name.LastIndexOf(".") + 1, file_name.Length - file_name.LastIndexOf(".") - 1).ToLower();
        string newfilename = filenamefirst + "." + ExtendName;
        return newfilename;
    }
}

 

这个是个人思路,仅供参考

追太阳的向日葵 | 园豆:29 (初学一级) | 2009-12-30 11:27
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册