zoukankan      html  css  js  c++  java
  • Asp.NET 上传图片并生成缩略图


    后台代码:


    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.Data.SqlClient;
    using System.IO;    //注意把这个加上
    using System.Drawing;   //注意把这个加上

    public partial class PhotoPage : System.Web.UI.Page
    {
        SqlConnection sqlCon = new SqlConnection("");
        SqlCommand sqlCom = new SqlCommand();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

      //生成图片代码
        protected void btnOK_Click(object sender, EventArgs e)
        {
            string P_str_filePath = "";      //定义一个在客户端文件的路径的变量
            string P_str_fileExtName = "", P_str_mFileName, P_str_mPath;//文件的扩展名
            System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传图片;<hr color=red>");
            if (fileUp .PostedFile.FileName!="")   //fileUp是控件fileload控件的名称  这句是取控件的文件名称
            {
                P_str_filePath = fileUp.PostedFile.FileName;  //获取图片路径
                P_str_fileExtName = P_str_filePath.Substring(P_str_filePath.LastIndexOf(".")+1);  //获取扩展名
                try
                {
                    P_str_mPath = Server.MapPath("Images/Pictures/"); //获取服务器端的文件的路径
                    P_str_mFileName = P_str_filePath.Substring(P_str_filePath.LastIndexOf("\") + 1);  //获取文件的名称
                    fileUp.PostedFile.SaveAs(P_str_mPath + P_str_mFileName);  //保存到制定的路径下

                    //生成缩略图
                    string smallFileName = Server.MapPath("Images/SmallPictures/")+P_str_mFileName;      //上传缩略图的路径
                    System.Drawing . Image smallImage = System.Drawing .Image.FromStream(fileUp.PostedFile.InputStream, true);  //从文件中取出图片对象
                    double width = Double.Parse(TextBox1.Text .ToString ());  //设定的高度和宽度
                    double height = Double.Parse(TextBox2.Text .ToString ());
                    double NewWidth, NewHeight;  //新的高度和宽度
                    if (smallImage.Width > smallImage.Height)
                    {
                        NewWidth = width;
                        NewHeight = smallImage.Height * (NewWidth / smallImage.Width);
                    }
                    else
                    {
                        NewHeight = height;
                        NewWidth=smallImage.Width*(NewHeight/smallImage.Height);
                    }
                    if (NewHeight > height)
                    {
                        NewHeight = height;
                    }
                    if (NewWidth > width)
                    {
                        NewWidth = width;
                    }
                    Size size= new Size((int)NewWidth, (int)NewHeight);   //图片的大小
                    System.Drawing. Image Bitmap = new System.Drawing. Bitmap(size.Width, size.Height);   //新生成一个bitmap对象
                    Graphics graphics = Graphics.FromImage(Bitmap); //新生成的画板
                    graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;  //设置最高的质量
                    graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;        //设置高质量,低速度呈现平滑程度
                    graphics.Clear(Color.White);        //清空画布

                    //在指定位置画图

                    graphics.DrawImage(smallImage, new System.Drawing.Rectangle(0, 0, Bitmap.Width, Bitmap.Height),

                        new System.Drawing.Rectangle(0, 0,smallImage.Width,smallImage.Height),

                        System.Drawing.GraphicsUnit.Pixel);
                    //文字水印

                    System.Drawing.Graphics textGraphics = System.Drawing.Graphics.FromImage(Bitmap);

                    System.Drawing.Font font = new Font("宋体", 10);

                    System.Drawing.Brush brush = new SolidBrush(Color.Black);

                    textGraphics.DrawString(TextBox3.Text.Trim(), font, brush, 10, 10);

                    textGraphics.Dispose();

                    ///图片水印

                    //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));

                    //Graphics a = Graphics.FromImage(Bitmap);

                    //a.DrawImage(copyImage, new Rectangle(Bitmap.Width - copyImage.Width, Bitmap.Height - copyImage.Height, copyImage.Width, copyImage.Height), 

    0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);

                    //保存缩略图

                    try
                    {

                        Bitmap.Save(smallFileName, System.Drawing.Imaging.ImageFormat.Gif);
                        

                    }

                    catch (Exception ex)
                    {

                        Response.Write("保存缩略图失败:" + ex.Message);

                    }
                    string imageUrl = "";      //定义一个用来存储图片url的变量
                    Image1.ImageUrl =smallFileName .Substring( smallFileName.IndexOf("));
                    imageUrl=smallFileName .Substring( smallFileName.IndexOf("));
                    Image1.ImageUrl ="~"+ imageUrl.Replace("\", "/");


                    graphics.Dispose();

                    smallImage.Dispose();

                    Bitmap.Dispose();

     

                    //copyImage.Dispose();

                    //a.Dispose();

                    //copyImage.Dispose();

                }
                catch
                { 
                    
                }
            }
        }

  • 相关阅读:
    利用UncaughtExceptionHandler捕获未try...catch到的异常
    nodejs
    angularjs异步处理 $q.defer()
    springboot集成swagger
    面试相关
    springboot注解
    关于自动拆装箱
    sonar集成搭建
    Predicate 类
    idea快捷键
  • 原文地址:https://www.cnblogs.com/hbh123/p/5102440.html
Copyright © 2011-2022 走看看