当前位置: 首页 > 编程日记 > 正文

C# webform上传图片并生成缩略图

其实里面写的很乱,包括修改文件名什么的都没有仔细去写,主要是想记录下缩略图生成的几种方式 ,大家明白就好!

  1  void UpImgs()
  2     {
  3         if (FileUpload1.HasFile)
  4         {
  5             string fileContentType = FileUpload1.PostedFile.ContentType;
  6             if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
  7             {
  8                 string name = FileUpload1.PostedFile.FileName; // 客户端文件路径
  9 
 10                 FileInfo file = new FileInfo(name);
 11                 //string fileName = CreatFileName(file.Name)+"_"+file.Name; // 文件名称
 12                 //string fileName_s = "s_" + CreatFileName(file.Name) + "_" + file.Name; // 缩略图文件名称
 13 
 14                 string fileName = CreatFileName(file.Name); // 文件名称
 15                 string fileName_s = "s_" + CreatFileName(file.Name); // 缩略图文件名称
 16 
 17 
 18                 string webFilePath = Server.MapPath("../file/" + fileName); // 服务器端文件路径
 19                 string webFilePath_s = Server.MapPath("../file/" + fileName_s);  // 服务器端缩略图路径
 20 
 21 
 22                 string dbname = "~/file/" + fileName;//存放到<a href="http://lib.csdn.net/base/mysql" class='replace_word' title="MySQL知识库" target='_blank' style='color:#df3434; font-weight:bold;'>数据库</a>里的相对路径
 23                 string dbnames = "~/file/" + fileName_s;//存放到数据库里的相对路径
 24 
 25                 if (!File.Exists(webFilePath))
 26                 {
 27                     try
 28                     {
 29                         FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
 30                         MakeThumbnail(webFilePath, webFilePath_s, 200, 160, "Cut"); // 生成缩略图方法
 31                         //Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
 32                         Label1.Text = "提示:文件“" + dbname + "”成功上传,并生成“" + dbnames + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B";
 33 
 34 
 35                         File.Delete(webFilePath);//删除源文件,在这我把源文件删了,只保留了缩小后的图片
 36 
 37 
 38 
 39                     }
 40                     catch (Exception ex)
 41                     {
 42                         Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;
 43 
 44                     }
 45                 }
 46                 else
 47                 {
 48                     Label1.Text = "提示:文件已经存在,请重命名后上传";
 49 
 50                 }
 51             }
 52             else
 53             {
 54                 Label1.Text = "提示:文件类型不符";
 55 
 56             }
 57         }
 58     }
 59     /**//// 
 60 /// 生成缩略图
 61 /// 
 62 /// 源图路径(物理路径)
 63 /// 缩略图路径(物理路径)
 64 /// 缩略图宽度
 65 /// 缩略图高度
 66 /// 生成缩略图的方式 
 67     public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
 68     {
 69         System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);
 70 
 71         int towidth = width;
 72         int toheight = height;
 73 
 74         int x = 0;
 75         int y = 0;
 76         int ow = originalImage.Width;
 77         int oh = originalImage.Height;
 78 
 79         switch (mode)
 80         {
 81             case "HW"://指定高宽缩放(可能变形) 
 82                 break;
 83             case "W"://指定宽,高按比例 
 84                 toheight = originalImage.Height * width / originalImage.Width;
 85                 break;
 86             case "H"://指定高,宽按比例
 87                 towidth = originalImage.Width * height / originalImage.Height;
 88                 break;
 89             case "Cut"://指定高宽裁减(不变形) 
 90                 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
 91                 {
 92                     oh = originalImage.Height;
 93                     ow = originalImage.Height * towidth / toheight;
 94                     y = 0;
 95                     x = (originalImage.Width - ow) / 2;
 96                 }
 97                 else
 98                 {
 99                     ow = originalImage.Width;
100                     oh = originalImage.Width * height / towidth;
101                     x = 0;
102                     y = (originalImage.Height - oh) / 2;
103                 }
104                 break;
105             default:
106                 break;
107         }
108 
109         //新建一个bmp图片
110         System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
111 
112         //新建一个画板
113         System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
114 
115         //设置高质量插值法
116         g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
117 
118         //设置高质量,低速度呈现平滑程度
119         g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
120 
121         //清空画布并以透明背景色填充
122         g.Clear(System.Drawing.Color.Transparent);
123 
124         //在指定位置并且按指定大小绘制原图片的指定部分
125         g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
126         new System.Drawing.Rectangle(x, y, ow, oh),
127         System.Drawing.GraphicsUnit.Pixel);
128 
129         try
130         {
131             //以jpg格式保存缩略图
132             bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
133         }
134         catch (System.Exception e)
135         {
136             throw e;
137         }
138         finally
139         {
140             originalImage.Dispose();
141             bitmap.Dispose();
142             g.Dispose();
143         }
144     }
145 
146     string CreatFileName(string fileName)
147     {
148 
149         DateTime now = DateTime.Now;
150 
151         string str =
152 
153             now.Year.ToString() + 
154 
155              now.Month.ToString() + 
156 
157              now.Day.ToString() + 
158 
159              now.Hour.ToString() + 
160 
161              now.Minute.ToString() + 
162 
163              now.Second.ToString() + 
164 
165              fileName;
166 
167         return str;
168 
169     }

转载于:https://www.cnblogs.com/felix-wang/p/6255313.html

相关文章:

ios中的自动释放池

自动释放池中是否有虑重功能 1 autoreleasepool { 2 UIView *view [UIView alloc] init] autorelease]; 3 [view autorelease]; 4 } 这样写在自动释放池的队列中是两个对象还是一个对象&#xff0c;就是说把view加到自动释放池的队列时&#xff0c;队列本身是…

arch linux安装_如何从头开始安装Arch Linux

arch linux安装by Andrea Giammarchi由Andrea Giammarchi In this article, youll learn how to install Arch Linux from scratch… and in about 5 minutes. So lets get to it.在本文中&#xff0c;您将学习如何从头开始安装Arch Linux&#xff0c;大约需要5分钟。 因此&am…

CoreCRM 开发实录 —— Profile

再简单的功能&#xff0c;也需要一坨代码的支持。Profile 的编辑功能主要就是修改个人的信息。比如用户名、头像、性别、电话……虽然只是一个编辑界面&#xff0c;但添加下来&#xff0c;涉及了6个文件的修改和7个新创建的文件。各种生成的和手写的代码&#xff0c;共有934行之…

iOS KVO 的实现原理

KVO 的实现原理 一 原理 1.KVO是基于runtime机制实现的 2.当某个类的属性对象第一次被观察时&#xff0c;系统就会在运行期动态地创建该类的一个派生类&#xff0c;在这个派生类中重写基类中任何被观察属性的setter 方法。派生类在被重写的setter方法内实现真正的通知机制 …

利用UltimateAndroid框架进行快速开发

UltimateAndroid是一套集成了许多现有优秀的Android开源类库并将之组合成一个整体的Android快速开发框架。框架目前主要包含的功能有View Injection,ORM,异步网络请求和图片加载&#xff0c;自动化脚本测试,磁盘LRU等功能.同时提供了类似于TripleDes、Webview快速设置、Md5处理…

溢出内容菜单_停止过度使用溢出菜单

溢出内容菜单by Daniel Burka丹尼尔伯卡(Daniel Burka) 停止过度使用溢出菜单 (Stop the overuse of overflow menus) You know those obscure menu buttons on apps and websites that reveal even more menu options? They usually have an ellipsis “…” or an arrow ▼…

KVC 和 KVO

KVC 键值编码 全称是Key-value coding&#xff0c;翻译成键值编码。它提供了一种使用字符串而不是访问器方法去访问一个对象实例变量的机制。 1.通过key&#xff08;成员变量的名称&#xff09;设置value&#xff08;成员变量的值&#xff09; - (void)setValue:(…

datasnap的客户端回传机制

最近&#xff0c;学习XE6下的DataSnap回叫技术编译代码&#xff0c;体会如下&#xff1a;第一篇 服务器与客户端回叫 从Delphi2010开始&#xff0c;DataSnap支持回叫&#xff08;Call Back&#xff09;机制。这样&#xff0c;在调用耗时较长的方法时&#xff0c;通过回叫机制…

Block 底层1

Block 本质来讲是OC 对象&#xff0c;其内部有一个isa指针。 1 Block 的声明 一言以蔽之&#xff1a; returnType &#xff08; ^blockName&#xff09;(parameterTypes) ^returnType (parameters) {}; returnType 返回的类型 可以为Void,为Void的时候可以省略^blockName …

从零学web前端_从零到前端英雄(第2部分)

从零学web前端This article is part two of the “From Zero to Front-end Hero” series. In part one, you learned how to create layouts with HTML and CSS as well as some best practices. In part two, we will focus on learning JavaScript as a standalone language…

jdk8飞行记录器配置

jdk8提供了jmc工具,应该比visualvm厉害吧 下面贴一份tomcat的配置,自己留个备份,把下面的内容粘贴到tomcat setenv.sh就可以了 nowdaydate %Y%m%d_%H%M%S test -d ../gclogs || mkdir ../gclogsif [ -r "$CATALINA_BASE/bin/setenv_custom.sh" ]; then. "$CATAL…

petaPar培训文档

自己写的实验室程序文档&#xff0c;方便后来者。 转载于:https://www.cnblogs.com/daijkstra/p/3972167.html

Block 底层值__Block修饰符

__Block 修饰符 Block 想要改变外部的变量&#xff0c;必须要用__Block 来修饰自动变量。 根据内存地址可以看出来&#xff0c;__block 所修饰的变量&#xff0c;将外部的变量在栈中的内存地址放到了堆中。 // auto 自动变量的内存分配在栈区域 stack__block int meters 100…

体检系统前端源码_给您的前端进行健康检查

体检系统前端源码by Ewa Mitulska-Wjcik伊娃米图尔斯卡(EwaMitulska-Wjcik) 给您的前端进行健康检查 (Give your Front End a Health Check) You’ve built out all your user stories and your app is working. Now’s it’s ready to submit as done, so you can move on wi…

1-runtime的Method,IMP,Property,ivar

基础定义 objc-750 的tar包 objc-private.h 定义 typedef struct objc_class *Class; typedef struct objc_object *id;#if __OBJC2__ typedef struct method_t *Method; typedef struct ivar_t *Ivar; typedef struct category_t *Category; typedef struct property_t *ob…

【编程题目】左旋转字符串 ☆

26.左旋转字符串&#xff08;字符串&#xff09;题目&#xff1a;定义字符串的左旋转操作&#xff1a;把字符串前面的若干个字符移动到字符串的尾部。如把字符串 abcdef 左旋转 2 位得到字符串 cdefab。请实现字符串左旋转的函数。要求时间对长度为 n 的字符串操作的复杂度为 O…

10、同步机制遵循的原则_我要遵循的10条原则

10、同步机制遵循的原则by Haseeb Qureshi由Haseeb Qureshi 我要遵循的10条原则 (10 Principles I Want to Live By) I just came home from a vow of silence at a meditation center in northern California. It’s a strange feeling coming back to city life after five …

2-Runtime objc_object objc_class

一 NSObject NSObject是OC 中的基类&#xff0c;除了NSProxy其他都继承自NSObject interface NSObject <NSObject> { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wobjc-interface-ivars"Class isa OBJC_ISA_AVAILABILITY; #pragma …

Spring3.0 AOP 具体解释

一、什么是 AOP。 AOP&#xff08;Aspect Orient Programming&#xff09;&#xff0c;也就是面向切面编程。能够这样理解&#xff0c;面向对象编程&#xff08;OOP&#xff09;是从静态角度考虑程序结构&#xff0c;面向切面编程&#xff08;AOP&#xff09;是从动态角度考虑程…

通过Appium获取Android app中webview

因为要测试Android app中嵌入的web页面&#xff0c;所以需要从native切换到webview。网上查了好多帖子&#xff0c;都用到类似下面代码&#xff1a; //判断是否有 WEBVIEWSet<String> contextNames driver.getContextHandles();for (String contextName : contextNames)…

javascript原理_JavaScript程序包管理器工作原理简介

javascript原理by Shubheksha通过Shubheksha JavaScript程序包管理器工作原理简介 (An introduction to how JavaScript package managers work) A few days ago, ashley williams, one of the leaders of the Node.js community, tweeted this:几天前&#xff0c;Node.js社区…

iOS base64 MD5

网络APP 只要涉及用户隐私的数据&#xff0c;均不能以明文传输。 一 base64 编码 将任意的二进制数据转为编码为 65个字符的组成。 0-9 a-z A-Z / 一共 65 个 字符 例如&#xff1a; 1 mac 自带 base64命令 可以将base64 编码的文件可以转换 –》将桌面上1.png 图片经过…

【面试虐菜】—— Oracle知识整理《收获,不止Oracle》

普通堆表不足之处&#xff1a; 表更新有日志开销表删除有瑕疵表记录太大检索较慢索引回表读开销很大有序插入难有序读出DELETE产生的undo最多&#xff0c;redo也最多&#xff0c;因为undo也需要redo保护全局临时表&#xff1a;1 高效删除记录基于事务的全局临时表commit或者ses…

每日成长17年1月

2017年1月 1月9号 一、学习了ice ice是一个跨平台调用程序&#xff0c;与语言无关的一个中间件&#xff0c;比如&#xff0c;可以通过java的代码调用 c应用程序的接口。 1月11号 一.学习了 struts2 spring mybatis 的配置。 1.首先是web.xml的配置&#xff0c;主要配置两…

网络安全从事工作分类_那么,您想从事安全工作吗?

网络安全从事工作分类by Parisa Tabriz由Parisa Tabriz 那么&#xff0c;您想从事安全工作吗&#xff1f; (So, you want to work in security?) Every once in a while, I’ll get an email from an eager stranger asking for advice on how to have a career in security …

iOS 使用钥匙串将用户密码存入本地

在 iOS 开发中&#xff0c;用户一般注册时候&#xff0c;APP会将用户的用户名和密码直接保存到本地&#xff0c;便于用户下次直接进行登录。 这样就会牵扯到一个问题&#xff0c;用户的密码不能以明文的形式存储在本地&#xff0c;使用钥匙串进行保存用户的密码较为安全。 钥…

Leetcode: Sort List

Sort a linked list in O(n log n) time using constant space complexity. 记得Insert Sort List, 那个复杂度是O(N^2)的&#xff0c;这里要求O&#xff08;nlogn&#xff09;&#xff0c;所以想到merge sort, 需要用到Merge Two Sorted List的方法&#xff08;我写的merge函数…

[UT]Unit Test理解

Coding中有一个原则&#xff1a;Test Driven Development. UT中的一些基本概念&#xff1a; 1. 测试驱动 2. 测试桩 3. 测试覆盖 4. 覆盖率 单体测试内容&#xff1a; 1. 模块接口&#xff1a;测试模块的数据流 2. 局部数据结构&#xff1a;如变量名、初始化、类型转换等 3. 路…

gitter 卸载_最佳Gitter频道:VR和AR

gitter 卸载by Gitter通过吉特 最佳Gitter频道&#xff1a;VR和AR (Best Gitter channels on: VR & AR) Virtual reality is one of the biggest tech trends and a hot topic of 2016. Investment in that sector reached over 1 billion dollars early this year, while…

工作笔记---巡检记录

以下是工作中一些思路实现的笔记&#xff0c;业务需求是&#xff1a; 1、简易日历 2、质押物提交后的一天开始到当前系统时间之间才可以提交质押物 3、没有提交质押物的日期里面的图片以灰色图片站位&#xff0c;已经提交质押物的日期里面的图片以红色图片站位 4、图片点击之后…