• WEB C#上传图片保存成缩略图


    using System;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    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.Xml.Linq;
    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)
        {

        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpPostedFile hpf = File1.PostedFile;
            char[] splitChar = { '\\' };
            string[] FilenameArray = File1.Value.Split(splitChar);
            //Response.Write(File1.Value );
            //Response.Write("<script>alert(" +   + ")</script>");
            string Filename = FilenameArray[FilenameArray.Length - 1].ToLower();
            if (hpf.FileName.Length < 1)
            {
                Response.Write("<script>alert('请选择上传图片')</script>");
                return;
            }
            //if (hpf.ContentType != "image/pjpeg" && hpf.ContentType != "image/gif")
            //{
            //    Response.Write("<script>alert('图片格式不对')</script>");
            //}
            else
            {
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                sb.Append(DateTime.Now.Year.ToString());
                sb.Append(DateTime.Now.Month.ToString());
                sb.Append(DateTime.Now.Day.ToString());
                sb.Append(DateTime.Now.Hour.ToString());
                sb.Append(DateTime.Now.Minute.ToString());
                sb.Append(DateTime.Now.Second.ToString());
                if (File1.Value.ToLower().EndsWith("gif"))
                {
                    sb.Append(".gif");
                }
                else if (File1.Value.ToLower().EndsWith("jpg"))
                {
                    sb.Append(".jpg");
                }
                Filename=sb.ToString();

                string originalfilename = hpf.FileName;
                string strfile="D:/" + Filename;
                System.Drawing.Image image = System.Drawing.Image.FromStream(hpf.InputStream,true );
                System.Drawing.Size size = new Size(150, 150);
                System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width, size.Height);//新建bmp图片
                System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap);//新建画板

                graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;//设置高质量插值法
                graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度

                graphics.Clear(Color.White);//清空画布

                //在指定位置画图
                graphics.DrawImage(image , new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), new System.Drawing.Rectangle(0,0,image.Width ,image.Height ),System.Drawing.GraphicsUnit.Pixel);

                //保存缩略图
                try
                {
                    bitmap.Save(strfile, System.Drawing.Imaging.ImageFormat.Jpeg);
                    Response.Write("<script>alert('保存成功')</script>");

                }
                catch (Exception ex)
                {
                    Response.Write("保存缩略图失败" + ex.Message);
                }

                graphics.Dispose();
                image.Dispose();
                bitmap.Dispose();
            }
        }

    }

  • 相关阅读:
    博客测试页
    题解 P1420 【最长连号】
    计蒜客T1846AC记
    折腾笔记-计蒜客T1167AC记
    sublimetext使用教程
    北冥'sfish
    折腾笔记-计蒜客T1158-和为给定数AC记
    折腾笔记-计蒜客t1156AC记
    [题解]-整理药名
    python字符串连接的三种方法及其效率、适用场景详解
  • 原文地址:https://www.cnblogs.com/hyd309/p/1273295.html
Copyright © 2020-2023  润新知