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

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

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

用了个免费插件。

 <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 控件给修改掉。这样 一劳永逸。。。

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

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

  1. 一、简单的图片上传并预览功能input[file]

    一.简单的图片上传并预览功能input[file] <!DOCTYPE html> <html lang="en"> <head> <me ...

  2. ios中摄像头/相册获取图片压缩图片上传服务器方法总结

    本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下.     这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...

  3. android拍照选择图片上传服务器自定义控件

    做android项目的时候总免不了遇到图片上传功能,虽然就是调用android系统的拍照和相册选择功能,但是总面部了把一大推代码写在activity里,看上去一大推代码头都昏了.不如把这些功能都集成一 ...

  4. php图片上传服务器

    原理是把图片上传到服务器的某个目录,然后在把他的名字存入数据库,或者不需要数据库这部分也行.读取的时候直接读取名字. HTML提交表格 <form method="post" ...

  5. IOS 视频.图片上传服务器

    //上传视频 AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];    manager.requestSerializer. ...

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

    限制上传类型 & 多选:① accept 属性只能与 <input type="file" /> 配合使用.它规定能够通过文件上传进行提交的文件类型. ② mu ...

  7. Fit项目图片上传和云存储的调通

    项目中关于动作的说明需要相应的配图,这样可以更直观的说明动作要点.本篇主要为项目中动作的新增和编辑做准备,确定适合场景的上传操作逻辑以及图片的存储和加载的方法. 一 上传方案 a) 本来所用的模板中是 ...

  8. CKfinder for java详解二:缩略图及图片上传的缩放

    我们找到 <thumbs><enabled>true</enabled><url>�SE_URL%_thumbs/</url><dir ...

  9. vue图片上传及java存储图片(亲测可用)

    1.前言 在使用elementui的upload组件时,我一直无法做到上传的图片和其他数据一起提交.单纯的上传文件,java的存储图片的方式也有局限性. 我知道的后端保存图片有两种方式:一种是直接存储 ...

随机推荐

  1. int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    1:int.Parse(一个参数)        此参数必须满足: 1 只能是字符串: 2 只能是 “整型” 字符串,即各种整型ToString()之后的形式,也不能为浮点型. 2:int.TryPa ...

  2. Oracle 11g r2 安装

    Help Center:http://docs.oracle.com/cd/E11882_01/install.112/e24326/toc.htm#i1011296 前提:linux需要安装图形化介 ...

  3. 转载:JAVA的静态变量、静态方法、静态类

    静态变量和静态方法都属于静态对象,它与非静态对象的差别需要做个说明. (1)Java静态对象和非静态对象有什么区别? 比对如下: 静态对象                                ...

  4. 配置点云库PCL时遇到的问题

    配置PCL基本参照PCL中国官网教程 http://www.pclcn.org/study/shownews.php?lang=cn&id=34 配置点云库时遇到的问题(基于win8 64位, ...

  5. 学习笔记之vector向量容器

    今天复习到vector向量容器,里面包括vector向量容器的一些优点以及具体的使用方法及代码,分享给大家. Vector向量容器不但能够像数组一样对元素进行随机访问,还可以在尾部插入元素,是一种简单 ...

  6. MySQL服务 - 客户端工具mysql及mysqladmin使用介绍

    mysql客户端: mysql工具是MySQL官方提供的连接工具,用户可以通过mysql连接到mysqld上进行一系列的SQL操作.mysql工具有两种模式:交互模式和命令行模式.交互模式指令需要连接 ...

  7. xcode中使用xib添加autolayout中constrain to margins的不同

    在使用xcode7 在storyboard中添加autolayout中发现 如果添加在view 直接添加到viewcontroller的view 上 constrain to margins    只 ...

  8. Mysql的row_format

    在mysql中, 若一张表里面不存在varchar.text以及其变形.blob以及其变形的字段的话,那么张这个表其实也叫静态表,即该表的row_format是fixed,就是说每条记录所占用的字节一 ...

  9. Python 2.7.x 和 3.x 版本的重要区别

    许多Python初学者都会问:我应该学习哪个版本的Python.对于这个问题,我的回答通常是“先选择一个最适合你的Python教程,教程中使用哪个版本的Python,你就用那个版本.等学得差不多了,再 ...

  10. I/O 请求数据包

    MSDN原文:https://msdn.microsoft.com/zh-cn/library/windows/hardware/hh439638(v=vs.85).aspx 发送到设备驱动程序的大部 ...