惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
J
Java Code Geeks
博客园 - 聂微东
B
Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
WordPress大学
WordPress大学
腾讯CDC
L
LangChain Blog
Apple Machine Learning Research
Apple Machine Learning Research
Microsoft Azure Blog
Microsoft Azure Blog
D
DataBreaches.Net
The GitHub Blog
The GitHub Blog
美团技术团队
博客园 - Franky
Google DeepMind News
Google DeepMind News
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
月光博客
月光博客
The Cloudflare Blog

博客园 - 刘荣华

VS2005 疑难杂症小解(转) - 刘荣华 - 博客园 面向对象--接口与抽象类的恩恩怨怨 揭开CLR神秘面纱-----第二步 垃圾回收之(制造垃圾) 揭开CLR神秘面纱—第一步PE文件 打造自己的Javascript alert confirm对象(一) 一篇非常好的服务器控件原理讲解文章(转载) 基础中的基础,关于网页DOCTYPE(文档类型)的定义 SmtpClient学习笔记 测试中常见问题分析及对策 初识MemberShip类 周末(友情篇) 新的环境、新的起点、新的目标 由。NET分层架构所引发的思考(一) 再谈配置文件 shtm答疑 JS代码大全(二) JS代码大全(一) 关于用JS读取XML文档的问题 C#编程规范下载
利用VS2005的FileUpload控件做一个图片上传类
刘荣华 · 2006-11-21 · via 博客园 - 刘荣华

该文原创,转载请注名出处。谢谢
配合VS2005里的文件上传控件,做了一个图片上传的例子,并把图片另存为70*70大小,如要判断图片大小和类型可做在页面代码之中。有兴趣的朋友可以去实现一下。很容易的。

/*
 * 类描述:该类主要作用于上传图片.
 * 
 * 作者:刘荣华
 * 创建日期:2006/11/21
 * 
 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Configuration;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Data;

namespace UploadPicture
{
    
public class CUploadPic
    
{
        
        
public bool Upload(FileUpload upLoadName,HttpServerUtility Server,out string ImgUrl)
        
{
            
bool flag = true;
            
try
            
{   
                
/*读取保存原图的虚拟目录名称*/
                
string DuumyActuallyPic = ConfigurationManager.AppSettings["ActuallyPic"];
                
/*保存原图的路径*/
                
string pathActuallyPic = Server.MapPath("../" + DuumyActuallyPic + "/");
                
string fileName = upLoadName.FileName.Trim();
                
string NewFileName = CreatFileName(fileName);
                
string newPathActuallyPic = pathActuallyPic + NewFileName;

                
/*保存原图*/
                upLoadName.SaveAs(newPathActuallyPic);
                CreateBreviaryPic(newPathActuallyPic,upLoadName.FileName,Server, 
out ImgUrl);
            }

            
catch (Exception e)
            
{
                flag 
= false;
                
throw e;

            }

            
return flag;
        }

        
/// <summary>
        
/// 产生缩略图
        
/// </summary>
        
/// <param name="picPath"></param>

        void CreateBreviaryPic(string picPath,string FileName,HttpServerUtility Server,out string ImgUrl)
        
{
            
string HostDomain = ConfigurationManager.AppSettings["Address"];
            
/*读取保存缩略图的虚拟目录名称*/
            
string DummyBreviaryPic = ConfigurationManager.AppSettings["BreviaryPic"];
            
/*保存缩略图的路径*/
            
string pathBreviaryPic = Server.MapPath("../" + DummyBreviaryPic + "/");
            
string NewFileName = CreatFileName(FileName);

            
string newPathBreviaryPic = pathBreviaryPic + NewFileName;

            System.Drawing.Image obj 
= System.Drawing.Image.FromFile(picPath);
            
int imgHeight = obj.Height;
            
int imgWidth = obj.Width;
            
/**/
            
if (imgHeight != 70)
            
{
                imgHeight 
= 70;
            }

            
if (imgWidth != 70)
            
{
                imgWidth 
= 70;
            }


            System.Drawing.Image Breviary 
= obj.GetThumbnailImage(imgWidth, imgHeight, null, IntPtr.Zero);
            Breviary.Save(newPathBreviaryPic);

            obj.Dispose();
            Breviary.Dispose();
            
/*生成的图片连接地址=网站域名+虚拟目录名+文件名*/
            ImgUrl 
= HostDomain + "/" + DummyBreviaryPic + "/" + NewFileName;
        }

        
/// <summary>
        
/// 生成唯一的图片名
        
/// </summary>
        
/// <param name="fileName"></param>
        
/// <returns></returns>

        string CreatFileName(string fileName)
        
{
            DateTime now 
= DateTime.Now;
            
string str =
                now.Year.ToString() 
+ "_"
                
+ now.Month.ToString() + "_"
                
+ now.Day.ToString() + "_"
                
+ now.Hour.ToString() + "_"
                
+ now.Minute.ToString() + "_"
                
+ now.Second.ToString() + "_"
                
+ fileName;
            
return str;
        }

    }

}

然后在配置文件中加上

        <add key="Address" value="http://10.0.76.26"/>

    
<!--保存原图的虚拟目录名称-->
    
<add key="ActuallyPic" value="ActuallyGoodsImage"/>
    
<!--保存缩略图的虚拟目录名称-->
    
<add key="BreviaryPic" value="BreviaryImage"/>

就剩下最后一步,在IIS里对照上面的value值,建立你的虚拟目录,保存您的图片就OK
切记虚拟目录名称要和Value一样