C#图片上传服务器缩放存储功能

项目需求上需要用户上传头像。本来是用第三方的插件的。但是很多都是收费的。

不过,我需要花这钱么?是不?所以网络上搜集了些资料。。

果然。这个世界 大神是很多很多的。

用了个免费插件。

 <script src="../Scripts/ajaxFileUpload.js" type="text/javascript"></script>

这玩意儿真心强大。

图片传到服务器了。

然后问题来了。图片要缩放。本来是想裁剪的。

不过。真心很烦。。裁剪也做出来了。不过我没有使用。原因是因为。界面就比较乱了。

最后想出了。上传图片。利用这个插件传送到服务器。

然后在服务器端使用代码将照片裁剪成头像。也就是缩放。

具体代码:

  public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string action = context.Request["myaction"].ToString();
            if (action == "updata")
            {
                string msg = "";
                string result = "0";
                HttpPostedFile file = context.Request.Files[0];
                string ext = Path.GetExtension(file.FileName).ToLower();
                string newName = System.DateTime.Now.ToString("yyyyMMddHHmmss");
                string tempPath = "upload/temp/";

                string img = tempPath + newName + ext;
                string filePath = context.Server.MapPath(img);
                tempPath = context.Server.MapPath(tempPath);
                if (!Directory.Exists(tempPath))
                {
                    Directory.CreateDirectory(tempPath);
                }
                using (System.Drawing.Image originalImage = System.Drawing.Image.FromStream(file.InputStream))
                {
                    int w = originalImage.Width;
                    int h = originalImage.Height;
                    if (w > 1400 || h > 1400)
                    {
                        msg = "请您上传大小在1400*1400以内的图片";
                    }
                    else if (w < 50 || h < 50)
                    {
                        msg = "请您上传大于50*50的图片";
                    }
                    else
                    {
                        if (w > 300 || h > 300)
                        {
                            float sizeM;
                            using (System.Drawing.Image thumb = GetThumbImage(originalImage, 200, 200, out sizeM))
                            {
                                thumb.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);
                                result = "1";
                            }
                        }
                        else
                        {

                        }
                        //file.SaveAs(filePath);//保存

                    }
                }
                //result=0;失败;1;成功; msg:提示信息
                string strWrite = "../ashx/"+img;
                context.Response.Write(strWrite);
            }

        }

根据网上的一些代码修改的。这算变通实现了么?反正效果是有了。

新问题也来了。。。照片APP那边要弄成圆的。

而APP端 又不会处理IMG控件 成圆的 。所以就要求我把图片在服务器端直接画成圆的。

百度了半天。。找到了个 比较好的代码。

也就是把img图片圆角化。用了CSDN上的一个类。链接我忘记了。不过代码可以贴出来。

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

/// <summary>
/// ImageOption 的摘要说明
/// </summary>
public class ImageOperation
{
      /// <summary>
    /// 圆角生成(但是只能是一个角)
    /// </summary>
    /// <param name="image">源图片 Image</param>
    /// <param name="roundCorner">圆角位置</param>
    /// <returns>处理好的Image</returns>
    public static Image CreateRoundedCorner(Image image, RoundRectanglePosition roundCorner)
    {
        Graphics g = Graphics.FromImage(image);
        //保证图片质量
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.CompositingQuality = CompositingQuality.HighQuality;
        Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
        //构建圆角外部路径
        GraphicsPath rectPath = CreateRoundRectanglePath(rect, image.Width / 10, roundCorner);
        //圆角背景用白色填充
        Brush b = new SolidBrush(Color.White);
        g.DrawPath(new Pen(b), rectPath);
        g.FillPath(b, rectPath);
        g.Dispose();
        return image;
    }
    /// <summary>
    /// 目标图片的圆角位置
    /// </summary>
    public enum RoundRectanglePosition
    {
        /// <summary>
        /// 左上角
        /// </summary>
        TopLeft,
        /// <summary>
        /// 右上角
        /// </summary>
        TopRight,
        /// <summary>
        /// 左下角
        /// </summary>
        BottomLeft,
        /// <summary>
        /// 右下角
        /// </summary>
        BottomRight
    }
    /// <summary>
    /// 构建GraphicsPath路径
    /// </summary>
    /// <param name="rect"></param>
    /// <param name="radius"></param>
    /// <param name="rrPosition">图片圆角位置</param>
    /// <returns>返回GraphicPath</returns>
    private static GraphicsPath CreateRoundRectanglePath(Rectangle rect, int radius, RoundRectanglePosition rrPosition)
    {
        GraphicsPath rectPath = new GraphicsPath();
        switch (rrPosition)
        {
            case RoundRectanglePosition.TopLeft:
                {
                    rectPath.AddArc(rect.Left, rect.Top, radius * 2, radius * 2, 180, 90);
                    rectPath.AddLine(rect.Left, rect.Top, rect.Left, rect.Top + radius);
                    break;
                }
            case RoundRectanglePosition.TopRight:
                {
                    rectPath.AddArc(rect.Right - radius * 2, rect.Top, radius * 2, radius * 2, 270, 90);
                    rectPath.AddLine(rect.Right, rect.Top, rect.Right - radius, rect.Top);
                    break;
                }
            case RoundRectanglePosition.BottomLeft:
                {
                    rectPath.AddArc(rect.Left, rect.Bottom - radius * 2, radius * 2, radius * 2, 90, 90);
                    rectPath.AddLine(rect.Left, rect.Bottom - radius, rect.Left, rect.Bottom);
                    break;
                }
            case RoundRectanglePosition.BottomRight:
                {
                    rectPath.AddArc(rect.Right - radius * 2, rect.Bottom - radius * 2, radius * 2, radius * 2, 0, 90);
                    rectPath.AddLine(rect.Right - radius, rect.Bottom, rect.Right, rect.Bottom);
                    break;
                }
        }
        return rectPath;
    }

    /// <summary>
    /// 图片缩放
    /// </summary>
    /// <param name="b">源图片Bitmap</param>
    /// <param name="dstWidth">目标宽度</param>
    /// <param name="dstHeight">目标高度</param>
    /// <returns>处理完成的图片 Bitmap</returns>
    public static Bitmap ResizeBitmap(Bitmap b, int dstWidth, int dstHeight)
    {
        Bitmap dstImage = new Bitmap(dstWidth, dstHeight);
        System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(dstImage);
        //   设置插值模式
        g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Bilinear;
        //   设置平滑模式
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        //用Graphic的DrawImage方法通过设置大小绘制新的图片实现缩放
        g.DrawImage(b, new Rectangle(0, 0, dstImage.Width, dstImage.Height), new Rectangle(0, 0, b.Width, b.Height),GraphicsUnit.Pixel);
        g.Save();
        g.Dispose();
        return dstImage;
    }
}

这样应该就可以了。。。。。至此。上传图片显示成用户圆形头像功能。基本实现。

但是效率不是很高。一般的做法是吧IMG 控件给修改掉。这样 一劳永逸。。。

对于我而言。功能做到。怎么实现。大家有何高见?

时间: 2024-08-25 23:29:47

C#图片上传服务器缩放存储功能的相关文章

图片上传服务器压缩工具类

这个是我整理调试的图片上传工具类:只需要图片路径方可:大家可以直接使用:希望可以帮助到大家:代码如下: package com.wyy.twodimcode.push; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; impo

php图片上传服务器

原理是把图片上传到服务器的某个目录,然后在把他的名字存入数据库,或者不需要数据库这部分也行.读取的时候直接读取名字. HTML提交表格 <form method="post" action="upload_image_todb.php?name=<?php echo $username;?>" enctype="multipart/form-data"> <table> <h4>选择图片 <s

input file实现多选,限制文件上传类型,图片上传前预览功能

限制上传类型 & 多选:① accept 属性只能与 <input type="file" /> 配合使用.它规定能够通过文件上传进行提交的文件类型. ② multiple 属性规定输入字段可选择多个值. 示例: <!-- image/* 所有图片 image/png png图片 image/jpg jpg图片 image/gif gir动图 application/msword Word文档(.doc) application/vnd.openxmlform

移动端图片上传后进行压缩功能

在进行讲解上传图片压缩之前,我们先来了解下HTML5中的文件上传的基本知识点. 一: FileList对象与file对象. FileList对象表示用户选择的文件列表.在HTML4中,file控件内只允许放置一个文件,但是到HTML5中,通过添加multiple属性,file控件内允许一次放置多个文件.如下代码: <input type=”file” multiple/> 控件内的每一个用户选择的文件都是一个file对象,而FileList对象则为这些file对象的列表,代表用户选择的所有文件

测开之路一百三十七:实现图片上传和查询渲染功能

两种办法: 1.把接收的图片存到工程的某一文件夹下,数据库的img字段下存对应的路径,要用的时候根据路径去找图片 2.把接收的图片转成二进制存到img字段下,要用的时候再把二进制转回图片 这里采用第一种: 必须的元素 <form action="/post_feedback/" enctype="multipart/form-data" method="POST" class="form-horizontal">&

基于layUI的图片上传前预览功能的2种实现方式

上传页面采用了layui 的上传模块,要实现的功能:选择文件后---点击文件名,页面中间弹窗内预览图片,先看效果图: 预览功能的实现具体有2种方式:第一种是用blob+FileReader,第二种是layUI自带的. 1. 用layUI 自带的参数实现图片预览: layui.use('upload', function(){ ... choose: function(obj){ var files = this.files = obj.pushFile(); //将每次选择的文件追加到文件队列

JSP图片上传服务器

String operator = request.getParameter("operator"); //页面跳转的标识 String succeed="0";//记录是否上传成功 String fileName = "";//文件名字 String fileExt = ""; String filename1=""; if(operator!=null){ succeed="1";

Java - MultipartFile图片上传服务器,并且指定大小压缩

1 /*** 2 * 上传图片到服务器 并压缩 3 * 4 * @param myFile 文件 5 * @param request 6 * @return 7 */ 8 private Boolean UploadFile(MultipartFile myFile, int width, int height, HttpServletRequest request) { 9 Boolean sta = false; 10 InputStream is = null; 11 FileOutpu

图片上传服务器的而终极解决方案 【转载】

/** * 动态发布图片压缩 * * @param source_image 原图image * @param maxSize 限定的图片大小 * * @return 返回处理后的图片 */ - (NSData *)resetSizeOfImageData:(UIImage *)source_image maxSize:(NSInteger)maxSize; 先调整分辨率,分辨率可以自己设定一个值,大于的就缩小到这分辨率,小余的就保持原本分辨率.然后在根据最终大小来设置压缩比,比如传入maxSi