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

推荐订阅源

D
Darknet – Hacking Tools, Hacker News & Cyber Security
博客园_首页
T
The Blog of Author Tim Ferriss
罗磊的独立博客
The Register - Security
The Register - Security
IT之家
IT之家
D
Docker
爱范儿
爱范儿
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
博客园 - Franky
L
LangChain Blog
J
Java Code Geeks
博客园 - 三生石上(FineUI控件)
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
K
Kaspersky official blog
Latest news
Latest news
博客园 - 聂微东
酷 壳 – CoolShell
酷 壳 – CoolShell
Cyberwarzone
Cyberwarzone
人人都是产品经理
人人都是产品经理
C
CERT Recently Published Vulnerability Notes
The Hacker News
The Hacker News
T
Tailwind CSS Blog
S
SegmentFault 最新的问题
MongoDB | Blog
MongoDB | Blog
Scott Helme
Scott Helme
L
LINUX DO - 热门话题
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
D
DataBreaches.Net
Project Zero
Project Zero
H
Hackread – Cybersecurity News, Data Breaches, AI and More
G
Google Developers Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
C
Check Point Blog
S
Schneier on Security
NISL@THU
NISL@THU
T
Tor Project blog
P
Proofpoint News Feed
N
News and Events Feed by Topic
Last Week in AI
Last Week in AI
N
Netflix TechBlog - Medium
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
U
Unit 42
C
Cybersecurity and Infrastructure Security Agency CISA
T
The Exploit Database - CXSecurity.com
L
LINUX DO - 最新话题
Help Net Security
Help Net Security
Blog — PlanetScale
Blog — PlanetScale

博客园 - MSDI

报This application is currently offline……解决方法 用IIS建立高安全性Web服务器 关于缩略图的实现方式 - MSDI - 博客园 ASP返回操作必须使用可更新的查询错误4种可能 "此页的状态信息无效,可能已损坏。”的解决办法 - MSDI - 博客园 Windows 2003 的默认上传文件大小和大小限制的问题 微软Silverlight和Adobe Flash短兵相接 Returnil Virtual System Personal/Business Beta 1.70.6160 DES对称加解密方法 数据库开发总结(ADO.NET小结) 核心对象的连载(String对象) 核心对象的连载(Date对象) TD的noWrap属性使用注意事项 核心对象的连载(Math对象) 核心对象的连载(Array对象) ASP.NET2.0教程(第二章) ASP.NET2.0教程(第一章) ASP.NET2.0教程(前言) GDI+ 中发生一般性错误的解决方法
asp.net上传图片并生成缩略图
MSDI · 2006-09-28 · via 博客园 - MSDI

前台代码:

<%@ Page language="c#" Codebehind="TestWebForm.aspx.cs" AutoEventWireup="false" Inherits="Example.TestWebForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
    
<HEAD>
        
<title>在ASP.NET里轻松实现缩略图</title>
        
<meta content="Microsoft Visual Studio.NET 7.0" name="GENERATOR">
        
<meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
        
<meta content="JavaScript" name="vs_defaultClientScript">
        
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    
</HEAD>
    
<body MS_POSITIONING="GridLayout">
        
<asp:label id="Label1" runat="server"></asp:label>
        
<form id="Form1" method="post" encType="multipart/form-data" runat="server">
            
<INPUT type="file" name="file" width="600"><br>
            
<br>
            
<asp:Button id="Button1" runat="server"></asp:Button></form>
    
</body>
</HTML>

后台代码:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO; 
using System.Drawing.Imaging;

namespace Example
{
    
/// <summary>
    
/// TestWebForm 的摘要说明。
    
/// </summary>

    public class TestWebForm : System.Web.UI.Page
    
{
        
protected System.Web.UI.WebControls.Label Label1;
        
protected System.Web.UI.WebControls.Button Button1;

        
private void Page_Load(object sender, System.EventArgs e)
        
{
            
// 在此处放置用户代码以初始化页面
            Label1.Text = "<h3>在ASP.NET里轻松实现缩略图</h3>"
            Button1.Text 
= "上载并显示缩略图"

        }


        
Web 窗体设计器生成的代码

        
private void Button1_Click(object sender, System.EventArgs e)
        
{
            HttpFileCollection MyFileColl 
= HttpContext.Current.Request.Files;   //获取客户端上载的文件(多部件 MIME 格式)集合
            HttpPostedFile MyPostedFile = MyFileColl[0];  //获取第一个文件

            
if (MyPostedFile.ContentType.ToString().ToLower().IndexOf("image"< 0
            

                Response.Write(
"无效的图形格式。"); 
                
return
            }
 
            GetThumbNail(MyPostedFile.FileName, 
100100
                MyPostedFile.ContentType.ToString(), 
false, MyPostedFile.InputStream); 
        }


        
/// <summary>
        
/// 得到图片的类型
        
/// </summary>
        
/// <param name="strContentType"></param>
        
/// <returns></returns>

        private System.Drawing.Imaging.ImageFormat GetImageType(object strContentType) 
        

            
if ((strContentType.ToString().ToLower()) == "image/pjpeg"
            

                
return System.Drawing.Imaging.ImageFormat.Jpeg; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/gif"
            

                
return System.Drawing.Imaging.ImageFormat.Gif; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/bmp"
            

                
return System.Drawing.Imaging.ImageFormat.Bmp; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/tiff"
            

                
return System.Drawing.Imaging.ImageFormat.Tiff; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-icon"
            

                
return System.Drawing.Imaging.ImageFormat.Icon; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-png"
            

                
return System.Drawing.Imaging.ImageFormat.Png; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-emf"
            

                
return System.Drawing.Imaging.ImageFormat.Emf; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-exif"
            

                
return System.Drawing.Imaging.ImageFormat.Exif; 
            }
 
            
else if ((strContentType.ToString().ToLower()) == "image/x-wmf"
            

                
return System.Drawing.Imaging.ImageFormat.Wmf; 
            }
 
            
else 
            

                
return System.Drawing.Imaging.ImageFormat.MemoryBmp; 
            }
 
        }
 

        
/// <summary>
        
/// 得到缩略图
        
/// </summary>
        
/// <param name="strFileName">文件名</param>
        
/// <param name="iWidth">宽度</param>
        
/// <param name="iheight">高度</param>
        
/// <param name="strContentType"></param>
        
/// <param name="blnGetFromFile">是否从文件创建Image对象</param>
        
/// <param name="ImgStream"></param>

        private void GetThumbNail(string strFileName, int iWidth, int iheight, 
            
string strContentType, bool blnGetFromFile, System.IO.Stream ImgStream) 
        

            System.Drawing.Image oImg;
        
            
if (blnGetFromFile) 
            

                oImg 
= System.Drawing.Image.FromFile(strFileName); 
            }
 
            
else 
            

                oImg 
= System.Drawing.Image.FromStream(ImgStream); 
            }
 
            oImg 
= oImg.GetThumbnailImage(iWidth, iheight, null, IntPtr.Zero);   //GetThumbnailImage方法是返回此Image对象的缩略图
            string strGuid = System.Guid.NewGuid().ToString().ToUpper();  //初始化 Guid 类的新实例
            string strFileExt = strFileName.Substring(strFileName.LastIndexOf("."));  //得到图片的后缀
            Response.ContentType = strContentType; 
            MemoryStream MemStream 
= new MemoryStream(); //创建其支持存储区为内存的流
            oImg.Save(MemStream, GetImageType(strContentType)); 
            MemStream.WriteTo(Response.OutputStream);  
//WriteTo方法是将此内存流的整个内容写入另一个流中
        }
 
    }

}